使用对称加密aes对文件进行zip加密解密

首先引入 org.bouncycastle.jar ,AesZip.jar

下载路径:http://download.csdn.net/download/yingfeng612/9933192    ,   http://download.csdn.net/download/yingfeng612/9933208


package com.prac.zip;

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;

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="/app/1_admin_work.jpg";
        String desFile="/app/1_admin_work_encrypter.zip";
        FileUtil.zipFile(srcFile, desFile,"123456abc");
                           
        String srczipfile="/app/work.zip";
        String deszipfile="/app/work_encrypter.zip";
        FileUtil.encryptZipFile(srczipfile, deszipfile,"123456abc");
                           
        String decrypterSrcZipFile="/app/1_admin_work_encrypter.zip";
        String extractFileName="\\app\\"+"1_admin_work.jpg";
        String decrypterDesFile="/app/1_admin_work2.jpg";
        FileUtil.decrypterZipFile(decrypterSrcZipFile,extractFileName, decrypterDesFile,"123456abc");
    }
}

上文中的AesZip.jar也可以自己下载源码打包,路径:https://code.google.com/archive/p/winzipaes/downloads ,手工将源码打包即可使用






  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值