「java压缩加密」java文件加密与解密

博主:adminadmin 2022-12-26 18:51:08 118

今天给各位分享java压缩加密的知识,其中也会对java文件加密与解密进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何用java创建一个加密的压缩包

下面的示例代码演示如何创建zip压缩包。

首先需要由需要压缩的文件创建一个InputStream对象,然后读取文件内容写入到ZipOutputStream中。

ZipOutputStream类接受FileOutputStream作为参数。创建号ZipOutputStream对象后需要创建一个zip entry,然后写入。

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

/**

*

* @author outofmemory.cn

*/

public class Main {

/**

* Creates a zip file

*/

public void createZipFile() {

try {

String inputFileName = "test.txt";

String zipFileName = "compressed.zip";

//Create input and output streams

FileInputStream inStream = new FileInputStream(inputFileName);

ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(zipFileName));

// Add a zip entry to the output stream

outStream.putNextEntry(new ZipEntry(inputFileName));

byte[] buffer = new byte[1024];

int bytesRead;

//Each chunk of data read from the input stream

//is written to the output stream

while ((bytesRead = inStream.read(buffer)) 0) {

outStream.write(buffer, 0, bytesRead);

}

//Close zip entry and file streams

outStream.closeEntry();

outStream.close();

inStream.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

new Main().createZipFile();

}

java对zip文件进行加密

这个我不会。

对zip文件进行加密,我使用的超级加密3000.

超级加密 3000采用先进的加密算法,使你的文件和文件夹加密后,真正的达到超高的加密强度,让你的加密数据无懈可击。

如何用java 将文件加密压缩为zip文件.

用java加密压缩zip文件:

package com.ninemax.demo.zip.decrypt;

import java.io.File;

import java.io.IOException;

import java.util.List;

import java.util.zip.DataFormatException;

import org.apache.commons.io.FileUtils;

import de.idyl.winzipaes.AesZipFileDecrypter;

import de.idyl.winzipaes.AesZipFileEncrypter;

import de.idyl.winzipaes.impl.AESDecrypter;

import de.idyl.winzipaes.impl.AESDecrypterBC;

import de.idyl.winzipaes.impl.AESEncrypter;

import de.idyl.winzipaes.impl.AESEncrypterBC;

import de.idyl.winzipaes.impl.ExtZipEntry;

/**

* 压缩指定文件或目录为ZIP格式压缩文件

* 支持中文(修改源码后)

* 支持密码(仅支持256bit的AES加密解密)

* 依赖bcprov项目(bcprov-jdk16-140.jar)

*

* @author zyh

*/

public class DecryptionZipUtil {

/**

* 使用指定密码将给定文件或文件夹压缩成指定的输出ZIP文件

* @param srcFile 需要压缩的文件或文件夹

* @param destPath 输出路径

* @param passwd 压缩文件使用的密码

*/

public static void zip(String srcFile,String destPath,String passwd) {

AESEncrypter encrypter = new AESEncrypterBC();

AesZipFileEncrypter zipFileEncrypter = null;

try {

zipFileEncrypter = new AesZipFileEncrypter(destPath, encrypter);

/**

* 此方法是修改源码后添加,用以支持中文文件名

*/

zipFileEncrypter.setEncoding("utf8");

File sFile = new File(srcFile);

/**

* AesZipFileEncrypter提供了重载的添加Entry的方法,其中:

* add(File f, String passwd)

* 方法是将文件直接添加进压缩文件

*

* add(File f, String pathForEntry, String passwd)

* 方法是按指定路径将文件添加进压缩文件

* pathForEntry - to be used for addition of the file (path within zip file)

*/

doZip(sFile, zipFileEncrypter, "", passwd);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

zipFileEncrypter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 具体压缩方法,将给定文件添加进压缩文件中,并处理压缩文件中的路径

* @param file 给定磁盘文件(是文件直接添加,是目录递归调用添加)

* @param encrypter AesZipFileEncrypter实例,用于输出加密ZIP文件

* @param pathForEntry ZIP文件中的路径

* @param passwd 压缩密码

* @throws IOException

*/

private static void doZip(File file, AesZipFileEncrypter encrypter,

String pathForEntry, String passwd) throws IOException {

if (file.isFile()) {

pathForEntry += file.getName();

encrypter.add(file, pathForEntry, passwd);

return;

}

pathForEntry += file.getName() + File.separator;

for(File subFile : file.listFiles()) {

doZip(subFile, encrypter, pathForEntry, passwd);

}

}

/**

* 使用给定密码解压指定压缩文件到指定目录

* @param inFile 指定Zip文件

* @param outDir 解压目录

* @param passwd 解压密码

*/

public static void unzip(String inFile, String outDir, String passwd) {

File outDirectory = new File(outDir);

if (!outDirectory.exists()) {

outDirectory.mkdir();

}

AESDecrypter decrypter = new AESDecrypterBC();

AesZipFileDecrypter zipDecrypter = null;

try {

zipDecrypter = new AesZipFileDecrypter(new File(inFile), decrypter);

AesZipFileDecrypter.charset = "utf-8";

/**

* 得到ZIP文件中所有Entry,但此处好像与JDK里不同,目录不视为Entry

* 需要创建文件夹,entry.isDirectory()方法同样不适用,不知道是不是自己使用错误

* 处理文件夹问题处理可能不太好

*/

ListExtZipEntry entryList = zipDecrypter.getEntryList();

for(ExtZipEntry entry : entryList) {

String eName = entry.getName();

String dir = eName.substring(0, eName.lastIndexOf(File.separator) + 1);

File extractDir = new File(outDir, dir);

if (!extractDir.exists()) {

FileUtils.forceMkdir(extractDir);

}

/**

* 抽出文件

*/

File extractFile = new File(outDir + File.separator + eName);

zipDecrypter.extractEntry(entry, extractFile, passwd);

}

} catch (IOException e) {

e.printStackTrace();

} catch (DataFormatException e) {

e.printStackTrace();

} finally {

try {

zipDecrypter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 测试

* @param args

*/

public static void main(String[] args) {

/**

* 压缩测试

* 可以传文件或者目录

*/

// zip("M:\\ZIP\\test\\bb\\a\\t.txt", "M:\\ZIP\\test\\temp1.zip", "zyh");

// zip("M:\\ZIP\\test\\bb", "M:\\ZIP\\test\\temp2.zip", "zyh");

unzip("M:\\ZIP\\test\\temp2.zip", "M:\\ZIP\\test\\temp", "zyh");

}

}

压缩多个文件时,有两个方法(第一种没试):

(1) 预先把多个文件压缩成zip,然后调用enc.addAll(inZipFile, password);方法将多个zip文件加进来。

(2)针对需要压缩的文件循环调用enc.add(inFile, password);,每次都用相同的密码。

求解 java 对压缩文件zip 加密 !

所谓多zip加密,实际上就是一种对zip字节流的一种对称加密~其中解密的密码,就是对称加密中的密钥

加密后的字节保存为一个.zip的文件,打开之前必须输入密码(密钥),通过密钥将文件的字节转换成为普通的zip字节,就能读取出来了~

最好是写一个加密的输入输出流,将zip的输入输出流包装一下

请教:java导出文件后进行压缩并加密

用io流读文件,在文件头部固定长度,加入你的自定义字符,然后这个文件应该就损坏了。

解密:判断这个文件头部是否存在自定义字符,如果存在,则根据加密规则,反向删除该字符,回复文件原本字节顺序及长度。

这是我自己的思路,网上应该有更好的,现成的东西可以直接调用的

java 字符串加密 加密结果的长度如何压缩

1. 可以考虑引入字符A~Z,加上0~9,形成36进制(更进一步可以大小写敏感,加上a~z,形成62进制);

2. 将上述字符串 除以36 取余;作为个位数;上述字符串 除以36 取整,作为结果,重复本步取余计算。

3. 获得结果。

4. 反向解析,将上述结果,分别按位数 乘以 36 ;

5. 最后合并相加,获得原字符串。

关于java压缩加密和java文件加密与解密的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-26,除非注明,否则均为首码项目网原创文章,转载请注明出处。