mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-11-11 22:00:44 +00:00
更新 pom组件版本 新增安全秘钥接口 新增接口关闭 新增encryption接入方法 新增basic接入方法 新增User-Agent接入方法 支持ftp多用户 支持basic多用户 修复file接入协议错误 修复ssl伪证书错误 美化RUL报错提醒
This commit is contained in:
79
server/src/main/java/cn/keking/utils/AESUtil.java
Normal file
79
server/src/main/java/cn/keking/utils/AESUtil.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package cn.keking.utils;
|
||||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* AES加密解密工具类(目前AES比DES和DES3更安全,速度更快,对称加密一般采用AES)
|
||||
*/
|
||||
public class AESUtil {
|
||||
private static final String aesKey = ConfigConstants.getaesKey();
|
||||
|
||||
/**
|
||||
* AES解密
|
||||
*/
|
||||
public static String AesDecrypt(String url) {
|
||||
if (!aesKey(aesKey)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
byte[] raw = aesKey.getBytes(StandardCharsets.UTF_8);
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
|
||||
byte[] encrypted1 = Base64.getDecoder().decode(url);//先用base64解密
|
||||
byte[] original = cipher.doFinal(encrypted1);
|
||||
return new String(original, StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
if (e.getMessage().contains("Given final block not properly padded. Such issues can arise if a bad key is used during decryption")) {
|
||||
return "Keyerror";
|
||||
}else if (e.getMessage().contains("Input byte array has incorrect ending byte")) {
|
||||
return "byteerror";
|
||||
}else if (e.getMessage().contains("Illegal base64 character")) {
|
||||
return "base64error";
|
||||
}else if (e.getMessage().contains("Input length must be multiple of 16 when decrypting with padded cipher")) {
|
||||
return "byteerror";
|
||||
}else {
|
||||
System.out.println("ace错误:"+e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AES加密
|
||||
*/
|
||||
public static String aesEncrypt(String url) {
|
||||
if (!aesKey(aesKey)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
byte[] raw = aesKey.getBytes(StandardCharsets.UTF_8);
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
|
||||
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
|
||||
byte[] encrypted = cipher.doFinal(url.getBytes(StandardCharsets.UTF_8));
|
||||
return new String(Base64.getEncoder().encode(encrypted));//此处使用BASE64做转码功能,同时能起到2次加密的作用。
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean aesKey(String aesKey) {
|
||||
if (aesKey == null) {
|
||||
System.out.print("Key为空null");
|
||||
return false;
|
||||
}
|
||||
// 判断Key是否为16位
|
||||
if (aesKey.length() != 16) {
|
||||
System.out.print("Key长度不是16位");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user