fix: 修复EncryptUtils多线程环境中共享同一个 Cipher实例导致【 Cipher not initialized】的问题

close https://github.com/elunez/eladmin/issues/865
pull/875/head
Jie Zheng 2025-01-13 18:28:17 +08:00
parent feb2f01f89
commit 5f158e8f69
1 changed files with 5 additions and 10 deletions

View File

@ -27,20 +27,15 @@ import java.nio.charset.StandardCharsets;
* @author Zheng Jie * @author Zheng Jie
* @date 2018-11-23 * @date 2018-11-23
*/ */
public class EncryptUtils { public class EncryptUtils {
private static final String STR_PARAM = "Passw0rd"; private static final String STR_PARAM = "Passw0rd";
private static Cipher cipher;
private static final IvParameterSpec IV = new IvParameterSpec(STR_PARAM.getBytes(StandardCharsets.UTF_8)); private static final IvParameterSpec IV = new IvParameterSpec(STR_PARAM.getBytes(StandardCharsets.UTF_8));
private static DESKeySpec getDesKeySpec(String source) throws Exception { private static DESKeySpec getDesKeySpec(String source) throws Exception {
if (source == null || source.length() == 0){ if (source == null || source.isEmpty()) {
return null; return null;
} }
cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
String strKey = "Passw0rd"; String strKey = "Passw0rd";
return new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8)); return new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8));
} }
@ -49,18 +44,19 @@ public class EncryptUtils {
* *
*/ */
public static String desEncrypt(String source) throws Exception { public static String desEncrypt(String source) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = getDesKeySpec(source); DESKeySpec desKeySpec = getDesKeySpec(source);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec); SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, IV); cipher.init(Cipher.ENCRYPT_MODE, secretKey, IV);
return byte2hex( return byte2hex(cipher.doFinal(source.getBytes(StandardCharsets.UTF_8))).toUpperCase();
cipher.doFinal(source.getBytes(StandardCharsets.UTF_8))).toUpperCase();
} }
/** /**
* *
*/ */
public static String desDecrypt(String source) throws Exception { public static String desDecrypt(String source) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
byte[] src = hex2byte(source.getBytes(StandardCharsets.UTF_8)); byte[] src = hex2byte(source.getBytes(StandardCharsets.UTF_8));
DESKeySpec desKeySpec = getDesKeySpec(source); DESKeySpec desKeySpec = getDesKeySpec(source);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
@ -76,7 +72,6 @@ public class EncryptUtils {
for (byte b : inStr) { for (byte b : inStr) {
stmp = Integer.toHexString(b & 0xFF); stmp = Integer.toHexString(b & 0xFF);
if (stmp.length() == 1) { if (stmp.length() == 1) {
// 如果是0至F的单位字符串则添加0
out.append("0").append(stmp); out.append("0").append(stmp);
} else { } else {
out.append(stmp); out.append(stmp);
@ -87,7 +82,7 @@ public class EncryptUtils {
private static byte[] hex2byte(byte[] b) { private static byte[] hex2byte(byte[] b) {
int size = 2; int size = 2;
if ((b.length % size) != 0){ if ((b.length % size) != 0) {
throw new IllegalArgumentException("长度不是偶数"); throw new IllegalArgumentException("长度不是偶数");
} }
byte[] b2 = new byte[b.length / 2]; byte[] b2 = new byte[b.length / 2];