-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptUtils.java
More file actions
246 lines (203 loc) · 8.31 KB
/
Copy pathEncryptUtils.java
File metadata and controls
246 lines (203 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package codingTest;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.Mac;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
/**
* <b>암호화유틸</b>
* @author 박서찬
* @date 2018. 7. 24.
* @version 0.1 : 최초작성
* <hr>
* <pre>
* 해쉬(SHA256, HMAC) 및 암호화(AES128, AES256), 인코딩(Base64) 등의 유틸제공
*
* <b>History:</b>
* ====================================================================
* 버전 : 작성일 : 작성자 : 작성내역
* --------------------------------------------------------------------
* 0.1 2018. 7. 24 박서찬 최초작성
* ====================================================================
* </pre>
*/
public class EncryptUtils {
public static String getHashingSHA256(String source, String salt) {
String result = "";
try {
byte[] a = source.getBytes();
byte[] b = salt.getBytes();
byte[] bytes = new byte[a.length + b.length];
System.arraycopy(a, 0, bytes, 0, a.length);
System.arraycopy(b, 0, bytes, a.length, b.length);
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(bytes);
byte[] byteData = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; ++i) {
sb.append(Integer.toString((byteData[i] & 0xFF) + 256, 16).substring(1));
}
result = sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
public static class HexaString {
public static String encode(byte[] in) {
return Hex.encodeHexString(in);
}
public static byte[] decode(String in) throws DecoderException {
return Hex.decodeHex(in);
}
}
public static class B64 {
public static byte[] encode(byte[] in) {
return Base64.encodeBase64(in);
}
public static byte[] decode(byte[] in) {
try {
return Base64.decodeBase64(in);
} catch (Exception e) {
return null;
}
}
}
public static class AES128 {
final private static String ALGORITHM = "AES";
final private static String OPMODE = "AES/CBC/PKCS5PADDING";
public static byte[] encrypt(byte[] in, String key)
throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, NoSuchPaddingException, InvalidAlgorithmParameterException, BadPaddingException, DecoderException
{
SecretKeySpec keySpec = new SecretKeySpec(HexaString.decode(key), ALGORITHM);
Cipher cipher = Cipher.getInstance(OPMODE);
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
return cipher.doFinal(in);
}
public static byte[] decrypt(byte[] in, String key)
throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, NoSuchPaddingException, InvalidAlgorithmParameterException, BadPaddingException, DecoderException
{
SecretKeySpec keySpec = new SecretKeySpec(HexaString.decode(key), ALGORITHM);
Cipher cipher = Cipher.getInstance(OPMODE);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
return cipher.doFinal(in);
}
}
public static class AES256 {
final private static String ALGORITHM = "AES";
final private static String OPMODE = "AES/CBC/PKCS5PADDING";
// 암호화
public static String encrypt(String str, String key) throws java.io.UnsupportedEncodingException,
NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
IllegalBlockSizeException,
BadPaddingException {
String iv = key.substring(0, 16);
byte[] keyBytes = new byte[16];
byte[] b = key.getBytes("UTF-8");
int len = b.length;
if (len > keyBytes.length) {
len = keyBytes.length;
}
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);
Cipher c = Cipher.getInstance(OPMODE);
c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes()));
byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
String enStr = new String(B64.encode(encrypted));
return enStr;
}
//복호화
public static String decrypt(String str, String key) throws java.io.UnsupportedEncodingException,
NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
IllegalBlockSizeException,
BadPaddingException {
String iv = key.substring(0, 16);
byte[] keyBytes = new byte[16];
byte[] b = key.getBytes("UTF-8");
int len = b.length;
if (len > keyBytes.length) {
len = keyBytes.length;
}
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);
Cipher c = Cipher.getInstance(OPMODE);
c.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes("UTF-8")));
byte[] byteStr = B64.decode(str.getBytes());
return new String(c.doFinal(byteStr),"UTF-8");
}
}
public static class HMACSHA256 {
final private static String ALGORITHM = "HmacSHA256";
public static String generate(String in, String apikey)
throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, DecoderException
{
byte[] key = HexaString.decode(apikey);
SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
Mac mac = Mac.getInstance(ALGORITHM);
mac.init(keySpec);
byte[] out = mac.doFinal(in.getBytes("UTF-8"));
return HexaString.encode(out);
}
}
public static class HMACSHA512 {
final private static String ALGORITHM = "HmacSHA512";
public static String generate(String in, String apikey)
throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, DecoderException
{
byte[] key = HexaString.decode(apikey);
SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
Mac mac = Mac.getInstance(ALGORITHM);
mac.init(keySpec);
byte[] out = mac.doFinal(in.getBytes("UTF-8"));
return HexaString.encode(out);
}
}
public static void main(String args[]) throws InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException, DecoderException {
//문자길이 짝수만 가능
String hmacKey = "9a2a2519a27a4066a5720510fe3e4cb5c4";
System.out.println("hmac256:"+HMACSHA256.generate("1537338648758", hmacKey));
System.out.println("hmac512:"+HMACSHA512.generate("1537338648758", hmacKey));
String userPwd = "123456";
String salt = "hashSalt";
//example SHA256
String hashValue = getHashingSHA256(userPwd, salt);
System.out.println(hashValue);
try {
//example AES256
String key = "1234567890123456789";
String encVal = AES256.encrypt("931226838113", key);
System.out.println(encVal);
String decVal = AES256.decrypt(encVal, key);
System.out.println(decVal);
} catch (Exception e) {
e.printStackTrace();
}
try {
String localGln = "KOEXKR";
salt = "7137366B446573456C637045303837635057466D564F33785162513550373044";
System.out.println("Local GLN:"+localGln);
System.out.println("salt:"+salt);
//"Qcrf9aCFKpwXrLzwHC3bHGIpcNdKbO63";
String encVal = HMACSHA256.generate("1537269765170", salt);
System.out.println("HMAC256:"+encVal);
} catch (Exception e) {
e.printStackTrace();
}
}
}