java aes 解压_通过Java生成加密压缩文件(支持AES压缩和解压zip文件)

本文介绍了如何使用Java结合winzipaes工具包实现AES加密压缩和解压zip文件。通过下载winzipaes源码和依赖包,将源码打包成Jar,然后在Java代码中调用相关方法进行文件的加密压缩、加密解压以及解密抽取文件操作。

通过Java对文件进行压缩加密我一般都使用winzipaes工具包,该工具包支持AES压缩和解压zip文件,首先通过http://code.google.com/p/winzipaes/downloads/list可以下载对应版本的源码,然后通过http://www.bouncycastle.org/latest_releases.html地址下载winzipaes依赖包,手工将winzipaes源码打成Jar包即可使用。

具体使用方法如下:

import java.io.File;

import java.io.IOException;

import java.util.zip.DataFormatException;

import de.idyl.winzipaes.AesZipFileDecrypter;

import de.idyl.winzipaes.AesZipFileEncrypter;

import de.idyl.winzipaes.impl.AESDecrypterBC;

import de.idyl.winzipaes.impl.AESEncrypterBC;

import de.idyl.winzipaes.impl.ExtZipEntry;

/**

*

* @author Jerry.tao

*

*/

public class FileUtil {

/**

* 压缩单个文件并加密

* @param srcFile 待压缩的文件

* @param desFileName 生成的目标文件

* @param passWord 压缩文件加密密码

* @throws IOException

*/

public static void zipFile(String srcFile,String desFile,String passWord) throws IOException{

AesZipFileEncrypter.zipAndEncrypt(new File(srcFile),new File(desFile), passWord, new AESEncrypterBC());

}

/**

* 给指定的压缩文件进行加密

* @param srcZipFile 待加密的压缩文件

* @param desFile 加密后的目标压缩文件

* @param passWord 压缩文件加密密码

* @throws IOException

*/

public static void encryptZipFile(String srcZipFile,String desFile,String passWord) throws IOException{

AesZipFileEncrypter.zipAndEncryptAll(new File(srcZipFile), new File(desFile), passWord, new AESEncrypterBC());

}

/**

* 解密抽取压缩文件中的某个文件

* @param srcZipFile 加密的压缩文件

* @param extractFileName 抽取压缩文件中的某个文件的名称

* @param desFile 解压后抽取后生成的目标文件

* @param passWord 解压密码

* @throws IOException

* @throws DataFormatException

*/

public static void decrypterZipFile(String srcZipFile,String extractFileName,String desFile,String passWord)throws IOException, DataFormatException{

AesZipFileDecrypter zipFile = new AesZipFileDecrypter( new File(srcZipFile),new AESDecrypterBC());

ExtZipEntry entry = zipFile.getEntry(extractFileName);

zipFile.extractEntry( entry, new File(desFile),passWord);

}

public static void main(String[] args) throws Exception {

String srcFile="doc/zip_test/1_admin_work.xlsx";

String desFile="doc/zip_test/1_admin_work_Encrypter.zip";

FileUtil.zipFile(srcFile, desFile,"123456");

String srcZipFile="doc/zip_test/work.zip";

String desZipFile="doc/zip_test/work_Encrypter.zip";

FileUtil.encryptZipFile(srcZipFile, desZipFile,"123456");

String decrypterSrcZipFile="doc/zip_test/work_Encrypter.zip";

String extractFileName="1.xlsx";

String decrypterDesFile="doc/zip_test/1_decrypter.xlsx";

FileUtil.decrypterZipFile(decrypterSrcZipFile,extractFileName, decrypterDesFile,"123456");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值