mirror of https://github.com/elunez/eladmin
fix: 修复EncryptUtils多线程环境中共享同一个 Cipher实例导致【 Cipher not initialized】的问题
close https://github.com/elunez/eladmin/issues/865pull/875/head
parent
feb2f01f89
commit
5f158e8f69
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue