From 6159b94c90854c34a52b5031ef60f51ccc2cc818 Mon Sep 17 00:00:00 2001 From: Jie Zheng <201507802@qq.com> Date: Mon, 13 Jan 2025 18:30:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DEncryptUtils=E5=A4=9A?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E7=8E=AF=E5=A2=83=E4=B8=AD=E5=85=B1=E4=BA=AB?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E4=B8=AA=20Cipher=E5=AE=9E=E4=BE=8B=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E3=80=90=20Cipher=20not=20initialized=E3=80=91?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20close=20https://github.com/elunez?= =?UTF-8?q?/eladmin/issues/865?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/me/zhengjie/utils/EncryptUtils.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/eladmin-common/src/main/java/me/zhengjie/utils/EncryptUtils.java b/eladmin-common/src/main/java/me/zhengjie/utils/EncryptUtils.java index 4f334aac..191fdbbe 100644 --- a/eladmin-common/src/main/java/me/zhengjie/utils/EncryptUtils.java +++ b/eladmin-common/src/main/java/me/zhengjie/utils/EncryptUtils.java @@ -27,20 +27,15 @@ import java.nio.charset.StandardCharsets; * @author Zheng Jie * @date 2018-11-23 */ - public class EncryptUtils { 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 DESKeySpec getDesKeySpec(String source) throws Exception { - if (source == null || source.length() == 0){ + if (source == null || source.isEmpty()) { return null; } - cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); String strKey = "Passw0rd"; return new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8)); } @@ -49,18 +44,19 @@ public class EncryptUtils { * 对称加密 */ public static String desEncrypt(String source) throws Exception { + Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = getDesKeySpec(source); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); cipher.init(Cipher.ENCRYPT_MODE, secretKey, IV); - return byte2hex( - cipher.doFinal(source.getBytes(StandardCharsets.UTF_8))).toUpperCase(); + return byte2hex(cipher.doFinal(source.getBytes(StandardCharsets.UTF_8))).toUpperCase(); } /** * 对称解密 */ public static String desDecrypt(String source) throws Exception { + Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); byte[] src = hex2byte(source.getBytes(StandardCharsets.UTF_8)); DESKeySpec desKeySpec = getDesKeySpec(source); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); @@ -76,7 +72,6 @@ public class EncryptUtils { for (byte b : inStr) { stmp = Integer.toHexString(b & 0xFF); if (stmp.length() == 1) { - // 如果是0至F的单位字符串,则添加0 out.append("0").append(stmp); } else { out.append(stmp); @@ -87,7 +82,7 @@ public class EncryptUtils { private static byte[] hex2byte(byte[] b) { int size = 2; - if ((b.length % size) != 0){ + if ((b.length % size) != 0) { throw new IllegalArgumentException("长度不是偶数"); } byte[] b2 = new byte[b.length / 2];