|
@@ -8,7 +8,9 @@ import javax.crypto.Cipher;
|
8
|
8
|
import javax.crypto.spec.IvParameterSpec;
|
9
|
9
|
import javax.crypto.spec.SecretKeySpec;
|
10
|
10
|
|
|
11
|
+import org.apache.commons.codec.binary.Hex;
|
11
|
12
|
import org.apache.tomcat.util.codec.binary.Base64;
|
|
13
|
+import org.springframework.util.Assert;
|
12
|
14
|
|
13
|
15
|
public class CryptoUtil {
|
14
|
16
|
public static String decrypt(String ciphertext, String passphrase) {
|
|
@@ -61,4 +63,51 @@ public class CryptoUtil {
|
61
|
63
|
System.arraycopy(derivedBytes, keySize * 4, resultIv, 0, ivSize * 4);
|
62
|
64
|
return derivedBytes;
|
63
|
65
|
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+ /**
|
|
69
|
+ * AES, CBC, PKCS5Padding, encodeHexString
|
|
70
|
+ * Key 길이가 128, 256 이냐에 따라 알고리즘은 AES-128, AES-256 이 됨
|
|
71
|
+ */
|
|
72
|
+ public static String doDecrypt(String encryptString, String strKey) {
|
|
73
|
+ Assert.notNull(encryptString, "The encryptString must not be null!");
|
|
74
|
+ Assert.notNull(strKey, "The strKey must not be null!");
|
|
75
|
+
|
|
76
|
+ if ("".equals(encryptString)) {
|
|
77
|
+ return "";
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ SecretKeySpec key = new SecretKeySpec(strKey.getBytes(), "AES");
|
|
81
|
+
|
|
82
|
+ try {
|
|
83
|
+ // Hash 변환
|
|
84
|
+ byte[] encryptCombineBytes = Hex.decodeHex(encryptString.toCharArray());
|
|
85
|
+
|
|
86
|
+ int encryptSize = encryptCombineBytes.length;
|
|
87
|
+ // 암호화 문자열에 iv 가 포함되어 있지 않을 경우 0.
|
|
88
|
+ int ivSize = 0;
|
|
89
|
+ byte[] ivBytes = new byte[16];
|
|
90
|
+ System.arraycopy(encryptCombineBytes, 0, ivBytes, 0, ivBytes.length);
|
|
91
|
+ encryptSize -= 16;
|
|
92
|
+ // 암호화 문자열에 iv 가 포함되어 있을 경우 IV_SIZE
|
|
93
|
+ ivSize = 16;
|
|
94
|
+
|
|
95
|
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
|
|
96
|
+
|
|
97
|
+ // encryptBytes 분리
|
|
98
|
+ byte[] encryptBytes = new byte[encryptSize];
|
|
99
|
+ System.arraycopy(encryptCombineBytes, ivSize, encryptBytes, 0, encryptSize);
|
|
100
|
+
|
|
101
|
+ // Decrypt
|
|
102
|
+ Cipher cipherDecrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
103
|
+ cipherDecrypt.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
|
|
104
|
+ byte[] decryptBytes = cipherDecrypt.doFinal(encryptBytes);
|
|
105
|
+
|
|
106
|
+ String decryptResultString = new String(decryptBytes);
|
|
107
|
+
|
|
108
|
+ return decryptResultString;
|
|
109
|
+ } catch (Exception ex) {
|
|
110
|
+ throw new RuntimeException("Decrypt Exception!", ex);
|
|
111
|
+ }
|
|
112
|
+ }
|
64
|
113
|
}
|