e77ef3f4 by chentao

更换加密方法

1 parent 69deb321
package com.lego;
import com.lego.common.utils.EncryptUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* @author chentao
* @date 2025/8/7
*/
public class ManageApplication {
public static void main(String[] args) {
String path1 = "f:/1.mp4";
String path2 = "f:/2.mp4";
try{
FileInputStream inputStream = new FileInputStream(path1);
FileOutputStream outputStream = new FileOutputStream(path2);
byte[] arr = new byte[inputStream.available()];
inputStream.read(arr);
byte[] decrypt = EncryptUtils.decryptByAes(arr);
outputStream.write(decrypt);
inputStream.close();
outputStream.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
......@@ -12,6 +12,7 @@ import cn.hutool.crypto.asymmetric.SM2;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* 安全相关工具类
......@@ -72,6 +73,18 @@ public class EncryptUtils {
return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8);
}
public static byte[] encryptByAes(byte[] data, String password) {
if (StrUtil.isBlank(password)) {
throw new IllegalArgumentException("AES需要传入秘钥信息");
}
// aes算法的秘钥要求是16位、24位、32位
int[] array = {16, 24, 32};
if (!ArrayUtil.contains(array, password.length())) {
throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位");
}
return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encrypt(data);
}
/**
* AES加密
*
......@@ -82,6 +95,10 @@ public class EncryptUtils {
return encryptByAes(data, AES_SECRET);
}
public static byte[] encryptByAes(byte[] data) {
return encryptByAes(data, AES_SECRET);
}
/**
* AES加密
*
......@@ -118,6 +135,18 @@ public class EncryptUtils {
* @param password 秘钥字符串
* @return 解密后字符串
*/
public static byte[] decryptByAes(byte[] data, String password) {
if (StrUtil.isBlank(password)) {
throw new IllegalArgumentException("AES需要传入秘钥信息");
}
// aes算法的秘钥要求是16位、24位、32位
int[] array = {16, 24, 32};
if (!ArrayUtil.contains(array, password.length())) {
throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位");
}
return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).decrypt(data);
}
public static String decryptByAes(String data, String password) {
if (StrUtil.isBlank(password)) {
throw new IllegalArgumentException("AES需要传入秘钥信息");
......@@ -143,6 +172,13 @@ public class EncryptUtils {
return "";
}
public static byte[] decryptByAes(byte[] data) {
if(Objects.nonNull(data)) {
return decryptByAes(data, AES_SECRET);
}
return null;
}
/**
* sm4加密
*
......
......@@ -211,8 +211,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
UploadResult uploadResult;
try {
if(".pdf".equals(suffix) || ".mp4".equals(suffix) || ".json".equals(suffix) || ".ldr".equals(suffix)) {
String temp = EncryptUtils.encryptByAes(StrUtil.str(file.getBytes(), StandardCharsets.UTF_8));
byte[] encryptBytes = StrUtil.bytes(temp, CharsetUtil.CHARSET_UTF_8);
byte[] encryptBytes = EncryptUtils.encryptByAes(file.getBytes());
uploadResult = storage.uploadSuffix(encryptBytes, suffix, file.getContentType());
}
else{
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!