admin管理员组

文章数量:1633030

引入hutool工具包

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>${hutool.version}</version>
</dependency>

2.编写工具类

package com.sdjictec.bfile.common.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.symmetric.AES;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.*;
import java.io.*;
import java.security.NoSuchAlgorithmException;


public class AESFileUtil {


    private static final Logger LOGGER = LoggerFactory.getLogger(AESFileUtil.class);
    private static AES initAES(String key) {
        AES aes =  new AES(Mode.CBC, Padding.ISO10126Padding, key.getBytes(),key.getBytes());
        return aes;
    }

    

    /**
     * AES 加密
     * @param encryptPath
     * @param decryptPath
     * @param sKey
     * @return
     */
    public static boolean encryptFile(String encryptPath, String decryptPath, String sKey) throws NoSuchPaddingException, NoSuchAlgorithmException {
        File encryptFile = null;
        File decryptfile = null;
        BufferedOutputStream bufferedOutputStream = null;
        BufferedInputStream bufferedInputStream = null;
        try {
            encryptFile = new File(encryptPath);
            if(!encryptFile.exists()) {
                throw  new NullPointerException("Encrypt file is empty");
            }
            decryptfile = new File(decryptPath);
            if(decryptfile.exists()) {
                decryptfile.delete();
            }
            decryptfile.createNewFile();
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(decryptfile));
            bufferedInputStream = new BufferedInputStream(new FileInputStream(encryptFile));

            AES aes = initAES(sKey);
            aes.encrypt(bufferedInputStream,bufferedOutputStream,true);
        } catch (IOException e) {
            FileUtil.del(decryptfile.getAbsolutePath());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return false;
        }
        return true;
    }

    /**
     * AES 解密
     * @param encryptFile
     * @param decryptFile
     * @param mKey
     * @return
     */
    public static String decryptFile(File encryptFile,  File decryptFile, String mKey) throws NoSuchPaddingException, NoSuchAlgorithmException {
        LOGGER.info("解密密钥>>>>>>>>>>>>>>>>>>>>> {}",mKey);
        BufferedOutputStream outputStream = null;
        BufferedInputStream inputStream = null;
        //CipherInputStream inputStream = null;
        String md5Hex = null;
        try {
            if (!encryptFile.exists()) {
                throw new NullPointerException("Decrypt file is empty");
            }
            if (decryptFile.exists()) {
                decryptFile.delete();
            }
            decryptFile.createNewFile();
            AES aes = initAES(mKey);
            outputStream = new BufferedOutputStream(new FileOutputStream(decryptFile));
            inputStream = new BufferedInputStream(new FileInputStream(encryptFile));
            aes.decrypt(inputStream,outputStream,true);
            //解密后文件MD5 值,用于判断与原文件是否一致
            md5Hex = DigestUtil.md5Hex(decryptFile);
        } catch (Exception e) {
            LOGGER.info("解密失败>>>>>>>>{}",e.getMessage());
        } finally {
            LOGGER.info("删除原加密文件>>>>>>>{}",encryptFile.getAbsolutePath());
        }
        return md5Hex;
    }

}

调用

//加密
AESFileUtil.encryptFile(原文件地址, 生成加密文件地址, 自己定义16位密钥);

//解密
AESFileUtil.decryptFile(加密文件地址,解密文件生成地址,16位密钥与加密时一致);

hutool目前支持以下填充方式

本文标签: 加密解密文件工具hutool