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工具给文件加密解密 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1729147462a1187839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论