@@ -4,7 +4,14 @@ import cn.hutool.crypto.SecureUtil;
|
|||||||
import cn.hutool.crypto.symmetric.AES;
|
import cn.hutool.crypto.symmetric.AES;
|
||||||
import cn.hutool.crypto.symmetric.DES;
|
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.nio.charset.StandardCharsets;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
@@ -17,41 +24,69 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_LOGIN
|
|||||||
public class SyncVerifyUtil {
|
public class SyncVerifyUtil {
|
||||||
|
|
||||||
public static String decrypt(String ciphertext, String key, String type) {
|
public static String decrypt(String ciphertext, String key, String type) {
|
||||||
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
|
||||||
if ("AES".equalsIgnoreCase(type)) {
|
if ("AES".equalsIgnoreCase(type)) {
|
||||||
byte[] aesKey = new byte[16];
|
try {
|
||||||
System.arraycopy(keyBytes, 0, aesKey, 0, Math.min(keyBytes.length, aesKey.length));
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||||
AES aes = SecureUtil.aes(aesKey);
|
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
|
||||||
return aes.decryptStr(ciphertext);
|
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)) {
|
} else if ("DES".equalsIgnoreCase(type)) {
|
||||||
|
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
||||||
byte[] desKey = new byte[8];
|
byte[] desKey = new byte[8];
|
||||||
System.arraycopy(keyBytes, 0, desKey, 0, Math.min(keyBytes.length, desKey.length));
|
System.arraycopy(keyBytes, 0, desKey, 0, Math.min(keyBytes.length, desKey.length));
|
||||||
DES des = SecureUtil.des(desKey);
|
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 {
|
} else {
|
||||||
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
|
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 plaintext 明文内容
|
||||||
* @param key 密钥
|
* @param key 密钥
|
||||||
* @param type 加密类型,支持 AES、DES
|
* @param type 加密类型,支持 AES、DES
|
||||||
* @return 密文(Hex 格式)
|
* @return 密文(Base64 格式)
|
||||||
*/
|
*/
|
||||||
public static String encrypt(String plaintext, String key, String type) {
|
public static String encrypt(String plaintext, String key, String type) {
|
||||||
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
|
||||||
if ("AES".equalsIgnoreCase(type)) {
|
if ("AES".equalsIgnoreCase(type)) {
|
||||||
byte[] aesKey = new byte[16];
|
try {
|
||||||
System.arraycopy(keyBytes, 0, aesKey, 0, Math.min(keyBytes.length, aesKey.length));
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||||
AES aes = SecureUtil.aes(aesKey);
|
byte[] byteContent = plaintext.getBytes(StandardCharsets.UTF_8);
|
||||||
return aes.encryptHex(plaintext);
|
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)) {
|
} else if ("DES".equalsIgnoreCase(type)) {
|
||||||
|
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
||||||
byte[] desKey = new byte[8];
|
byte[] desKey = new byte[8];
|
||||||
System.arraycopy(keyBytes, 0, desKey, 0, Math.min(keyBytes.length, desKey.length));
|
System.arraycopy(keyBytes, 0, desKey, 0, Math.min(keyBytes.length, desKey.length));
|
||||||
DES des = SecureUtil.des(desKey);
|
DES des = SecureUtil.des(desKey);
|
||||||
return des.encryptHex(plaintext);
|
byte[] encrypted = des.encrypt(plaintext.getBytes(StandardCharsets.UTF_8));
|
||||||
|
return Base64.getEncoder().encodeToString(encrypted);
|
||||||
} else {
|
} else {
|
||||||
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
|
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user