1. 修复中铝 e 办同步解密方法错误的bug

(cherry picked from commit e1c59ce4f7)
This commit is contained in:
chenbowen
2025-08-22 11:37:42 +08:00
committed by chenbowen
parent a6bd4823bc
commit 02d2241bea

View File

@@ -4,7 +4,14 @@ import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.DES;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Map;
import java.util.TreeMap;
@@ -17,41 +24,69 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_LOGIN
public class SyncVerifyUtil {
public static String decrypt(String ciphertext, String key, String type) {
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
if ("AES".equalsIgnoreCase(type)) {
byte[] aesKey = new byte[16];
System.arraycopy(keyBytes, 0, aesKey, 0, Math.min(keyBytes.length, aesKey.length));
AES aes = SecureUtil.aes(aesKey);
return aes.decryptStr(ciphertext);
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
byte[] result = cipher.doFinal(Base64.getDecoder().decode(ciphertext.getBytes()));
return new String(result, StandardCharsets.UTF_8);
} catch (Exception e) {
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
}
} else if ("DES".equalsIgnoreCase(type)) {
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
byte[] desKey = new byte[8];
System.arraycopy(keyBytes, 0, desKey, 0, Math.min(keyBytes.length, desKey.length));
DES des = SecureUtil.des(desKey);
return des.decryptStr(ciphertext);
byte[] encryptedBytes = Base64.getDecoder().decode(ciphertext);
return new String(des.decrypt(encryptedBytes), StandardCharsets.UTF_8);
} else {
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
}
}
/**
* 对称加密
* 生成与原始代码兼容的密钥
*/
private static SecretKeySpec getSecretKey(String password) {
try {
KeyGenerator kg = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
kg.init(128, random);
SecretKey secretKey = kg.generateKey();
return new SecretKeySpec(secretKey.getEncoded(), "AES");
} catch (NoSuchAlgorithmException ex) {
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
}
}
/**
* 对称加密Base64 格式输出)
* @param plaintext 明文内容
* @param key 密钥
* @param type 加密类型,支持 AES、DES
* @return 密文(Hex 格式)
* @return 密文(Base64 格式)
*/
public static String encrypt(String plaintext, String key, String type) {
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
if ("AES".equalsIgnoreCase(type)) {
byte[] aesKey = new byte[16];
System.arraycopy(keyBytes, 0, aesKey, 0, Math.min(keyBytes.length, aesKey.length));
AES aes = SecureUtil.aes(aesKey);
return aes.encryptHex(plaintext);
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
byte[] byteContent = plaintext.getBytes(StandardCharsets.UTF_8);
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));
byte[] result = cipher.doFinal(byteContent);
return Base64.getEncoder().encodeToString(result);
} catch (Exception e) {
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
}
} else if ("DES".equalsIgnoreCase(type)) {
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
byte[] desKey = new byte[8];
System.arraycopy(keyBytes, 0, desKey, 0, Math.min(keyBytes.length, desKey.length));
DES des = SecureUtil.des(desKey);
return des.encryptHex(plaintext);
byte[] encrypted = des.encrypt(plaintext.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encrypted);
} else {
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
}