1.引入hutool-all的maven依赖
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.28</version>
</dependency>
2.编写代码
package com.study.utils;import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.NumberUtil;import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import java.io.*;
import java.security.Key;
import java.security.SecureRandom;
import java.util.List;/*** @Auther lmy* @date 2024-06-14 2:05* @Description AES文件加密解密(增强版)*/
public class FileAES {/*** 根据参数生成KEY*/public static Key getKey(String strKey) {Key key;try {KeyGenerator generator = KeyGenerator.getInstance("AES");generator.init(new SecureRandom(strKey.getBytes()));key = generator.generateKey();generator = null;} catch (Exception e) {throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);}return key;}/*** 文件采用AES算法解密文件*/public static void decrypt(String sourceDir, String targetDir, Key key) throws Exception {System.out.println("开始解密......");System.out.println("资源目录:" + sourceDir);System.out.println("输出目录:" + targetDir);long startL = System.currentTimeMillis();Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE, key);List<String> fileNames = FileUtil.listFileNames(sourceDir);System.out.println("资源文件总数量:" + fileNames.size());System.out.println("-----------------start-----------------");int i = 1;for (String sourceFileName : fileNames) {long start = System.currentTimeMillis();System.out.println("当前进度:" + i++ + "/" + fileNames.size());String sourceStr = sourceDir + "/" + sourceFileName;long size = FileUtil.size(new File(sourceStr));System.out.println("源文件-名称:" + sourceFileName + ",size:" + size / (1024 * 1024) + "(MB)" + ", " + NumberUtil.div(size, (1024 * 1024 * 1024)) + "(GB)");String prefix = FileUtil.getPrefix(sourceFileName);String suffix = FileUtil.getSuffix(sourceFileName);byte[] bytes = HexUtil.decodeHex(prefix);byte[] result = cipher.doFinal(bytes);String decryptName = new String(result);String encFileStr = targetDir + "/" + decryptName + "." + suffix;InputStream is = new FileInputStream(sourceStr);OutputStream out = new FileOutputStream(encFileStr);CipherOutputStream cos = new CipherOutputStream(out, cipher);byte[] buffer = new byte[1024 * 1024];int r;while ((r = is.read(buffer)) >= 0) {cos.write(buffer, 0, r);}cos.close();out.close();is.close();long end = System.currentTimeMillis();System.out.println("输出文件-名称:" + decryptName + "." + suffix);System.out.println("小计耗时:" + (end - start) / 1000 + "s");System.out.println();}System.out.println("------------------end----------------");System.out.println("解密完成,总计耗时:" + (System.currentTimeMillis() - startL) / 1000 + "s");}public static void encrypt(String sourceDir, String targetDir, Key key) throws Exception {System.out.println("开始加密......");System.out.println("资源目录:" + sourceDir);System.out.println("输出目录:" + targetDir);long startL = System.currentTimeMillis();Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE, key);List<String> fileNames = FileUtil.listFileNames(sourceDir);System.out.println("资源文件总数量:" + fileNames.size());System.out.println("-----------------start-----------------");int i = 1;for (String sourceFileName : fileNames) {long start = System.currentTimeMillis();System.out.println("当前进度:" + i++ + "/" + fileNames.size());String sourceStr = sourceDir + "/" + sourceFileName;long size = FileUtil.size(new File(sourceStr));System.out.println("源文件-名称:" + sourceFileName + ",size:" + size / (1024 * 1024) + "(MB)" + ", " + NumberUtil.div(size, (1024 * 1024 * 1024)) + "(GB)");String prefix = FileUtil.getPrefix(sourceFileName);String suffix = FileUtil.getSuffix(sourceFileName);byte[] result = cipher.doFinal(prefix.getBytes());String encryptHexName = HexUtil.encodeHexStr(result);String encFileStr = targetDir + "/" + encryptHexName + "." + suffix;InputStream is = new FileInputStream(sourceStr);OutputStream out = new FileOutputStream(encFileStr);CipherOutputStream cos = new CipherOutputStream(out, cipher);byte[] buffer = new byte[1024 * 1024];int r;while ((r = is.read(buffer)) >= 0) {cos.write(buffer, 0, r);}cos.close();out.close();is.close();long end = System.currentTimeMillis();System.out.println("输出文件-名称:" + encryptHexName + "." + suffix);System.out.println("小计耗时:" + (end - start) / 1000 + "s");System.out.println();}System.out.println("------------------end----------------");System.out.println("加密完成,总计耗时:" + (System.currentTimeMillis() - startL) / 1000 + "s");}public static void main(String[] args) throws Exception {Key key = getKey("密钥123456");String sourceDir = "C:\\Users\\xzq\\Desktop\\befor";String targetDir = "C:\\Users\\xzq\\Desktop\\after";String decDir = "C:\\Users\\xzq\\Desktop";//加密
// encrypt("C:\\Users\\xzq\\Desktop\\befor", "C:\\Users\\xzq\\Desktop\\after", key);//解密decrypt("C:\\Users\\xzq\\Desktop\\after", "C:\\Users\\xzq\\Desktop", key);}
}