Skip to content

Commit 113d002

Browse files
fix[backend](cypherUtil): make key|iv derivation be local instead of static
1 parent 11fc5da commit 113d002

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

backend/src/main/java/com/park/utmstack/util/CipherUtil.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,32 @@
1414

1515
public class CipherUtil {
1616
private static final String CLASSNAME = "CipherUtil";
17-
private static SecretKeySpec secretKey;
18-
private static IvParameterSpec iv;
1917
private static final String CIPHER_INSTANCE = "AES/CBC/PKCS5Padding";
18+
private static final int ITERATION_COUNT = 65536;
19+
private static final int KEY_LENGTH = 128;
2020

21-
private static void setKey(String myKey) throws Exception {
22-
final String ctx = CLASSNAME + ".";
21+
private static class CryptoContext {
22+
final SecretKeySpec secretKey;
23+
final IvParameterSpec iv;
24+
25+
CryptoContext(SecretKeySpec secretKey, IvParameterSpec iv) {
26+
this.secretKey = secretKey;
27+
this.iv = iv;
28+
}
29+
}
30+
31+
private static CryptoContext getCryptoContext(String myKey) throws Exception {
32+
final String ctx = CLASSNAME + ".getCryptoContext";
2333
try {
2434
byte[] salt = myKey.getBytes(StandardCharsets.UTF_8);
2535
MessageDigest sha = MessageDigest.getInstance("SHA-1");
2636
salt = sha.digest(salt);
27-
KeySpec spec = new PBEKeySpec(myKey.toCharArray(), salt, 65536, 128); // AES-256
37+
KeySpec spec = new PBEKeySpec(myKey.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
2838
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
2939
byte[] key = f.generateSecret(spec).getEncoded();
30-
secretKey = new SecretKeySpec(key, "AES");
31-
iv = new IvParameterSpec(Arrays.copyOf(salt, 16));
40+
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
41+
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOf(salt, 16));
42+
return new CryptoContext(secretKey, iv);
3243
} catch (Exception e) {
3344
throw new Exception(ctx + ": " + e.getMessage());
3445
}
@@ -37,9 +48,9 @@ private static void setKey(String myKey) throws Exception {
3748
public static String encrypt(String str, String secret) {
3849
final String ctx = CLASSNAME + ".encrypt";
3950
try {
40-
setKey(secret);
51+
CryptoContext context = getCryptoContext(secret);
4152
Cipher cipher = Cipher.getInstance(CIPHER_INSTANCE);
42-
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
53+
cipher.init(Cipher.ENCRYPT_MODE, context.secretKey, context.iv);
4354
return Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
4455
} catch (Exception e) {
4556
throw new RuntimeException(ctx + ": " + e.getMessage());
@@ -49,9 +60,9 @@ public static String encrypt(String str, String secret) {
4960
public static String decrypt(String str, String secret) {
5061
final String ctx = CLASSNAME + ".decrypt";
5162
try {
52-
setKey(secret);
63+
CryptoContext context = getCryptoContext(secret);
5364
Cipher cipher = Cipher.getInstance(CIPHER_INSTANCE);
54-
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
65+
cipher.init(Cipher.DECRYPT_MODE, context.secretKey, context.iv);
5566
return new String(cipher.doFinal(Base64.getDecoder().decode(str)));
5667
} catch (Exception e) {
5768
throw new RuntimeException(ctx + ": " + e.getMessage());

0 commit comments

Comments
 (0)