-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp3.java
351 lines (297 loc) · 13.2 KB
/
tmp3.java
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package com.kt.aicare.common.util;
import com.kt.aicare.api.common.mapper.CommonMapper;
import com.kt.aicare.api.common.model.Common;
import com.kt.aicare.common.Constant;
import com.kt.aicare.common.token.TokenProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.HashMap;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
@Component
public class SecurityUtils {
@Value("${token.aeskey}")
private String aeskey;
private final String[] ENCRYPT_ALGORITHM = {"AES"};
private final String[] ENCRYPT_MODE = {"ECB", "CBC"};
private final String[] ENCRYPT_PADDING = {"PKCS5Padding","NoPadding"};
private String sPublicKey = null;
private String sPrivateKey = null;
private final TokenProvider tokenProvider;
public SecurityUtils(TokenProvider tokenProvider) {
this.tokenProvider = tokenProvider;
}
/**
* AccessToken 파싱 및 복호화 처리
*
* @param request
* @return Map<String, String>
*/
public HashMap<String, String> parseAccessToken(HttpServletRequest request) {
HashMap<String, String> result = new HashMap<>();
// String accessToken = tokenProvider.extract(request, "Bearer");
//
// String loginIdEnc = String.valueOf(tokenProvider.getClaimsFromToken(accessToken).get("loginId"));
// String loginIdDec = getDecrypt(loginIdEnc, 0, aeskey);
// String loginPartEnc = String.valueOf(tokenProvider.getClaimsFromToken(accessToken).get("loginPart"));
// String loginPartDec = getDecrypt(loginPartEnc, 0, aeskey);
// String chgLoginPartEnc = String.valueOf(tokenProvider.getClaimsFromToken(accessToken).get("chgLoginPart"));
// String chgLoginPartDec = getDecrypt(chgLoginPartEnc, 0, aeskey);
// String localOrgEnc = String.valueOf(tokenProvider.getClaimsFromToken(accessToken).get("localOrg"));
// String localOrgDec = getDecrypt(localOrgEnc, 0, aeskey);
String loginIdDec = "mng0000001";
String loginPartDec = "KDD0000003";
String chgLoginPartDec = "";
String localOrgDec = "";
result.put("loginId", loginIdDec);
result.put("loginPart", loginPartDec);
result.put("chgLoginPart", chgLoginPartDec);
result.put("localOrg", localOrgDec);
return result;
}
/**
* 패스워드 유효성 검사
*
* @param id
* @param pw
* @return HashMap<String, String>
*/
public HashMap<String, String> checkPassword(String id, String pw) {
HashMap<String, String> result = new HashMap<>();
String rtCd = "0000";
String rtVal = "";
int pwlen = pw.length();
if (pw.indexOf(id) > 0) {
rtCd = "0001";
rtVal = "ID를 비밀번호에 사용하시면 안됩니다.";
}
if (pwlen > pw.trim().length()) {
rtCd = "0002";
rtVal = "비밀번호에 공백을 사용하시면 안됩니다.";
}
String[] checkPw = { "abcd", "1234", "zxcv", "qwer" };
for (String s : checkPw) {
if (pw.indexOf(s) > 0) {
rtCd = "0003";
rtVal = "단순한 비밀번호는 사용할 수 없습니다.";
break;
}
}
if (StringUtils.hasText((rtVal))) {
result.put("rtCd", rtCd);
result.put("rtVal", rtVal);
return result;
}
String pwPattern = "^(?=.*[0-9])"; // 숫자
String pwPattern1 = "^(?=.*[a-zA-Z])"; // 영문
String pwPattern2 = "^(?=.*[~`!@#$%\\^%*()-])"; // 특수문자
String pwPattern3 = "(.)\\1\\1\\1"; // 반복된 문자
Matcher numMatcher = Pattern.compile(pwPattern).matcher(pw);
Matcher engMatcher = Pattern.compile(pwPattern1).matcher(pw);
Matcher spcMatcher = Pattern.compile(pwPattern2).matcher(pw);
Matcher repMatcher = Pattern.compile(pwPattern3).matcher(pw);
if (repMatcher.find()) {
rtCd = "0004";
rtVal = "연속된 숫자는 사용할수 없습니다.";
result.put("rtCd", rtCd);
result.put("rtVal", rtVal);
return result;
}
// 패스워드 패턴 [숫자,영문,특수문자] 모두 조합한 9자 이상 16자리 이하
boolean isNumMatcher = numMatcher.find(); // 숫자 패턴 확인하여 true 반환
boolean isEngMatcher = engMatcher.find(); // 영문 패턴 확인하여 true 반환
boolean isSpcMatcher = spcMatcher.find(); // 특수문자 패턴 확인하여 true 반환
if(isNumMatcher && isEngMatcher && isSpcMatcher && pwlen >= 9 && pwlen <=16) { // 3가지 패턴이 존재하고, 9자 이상, 16자 이하인 경우
rtCd = "0000";
rtVal = "정상";
} else {
if(pwlen < 9) { // 패스워드의 길이가 9자 미만인 경우
rtCd = "0007";
rtVal = "비밀번호는 9자리 이상 사용해야 합니다.";
}
if(pwlen > 16) { // 패스워드의 길이가 16자 초과인 경우
rtCd = "0006";
rtVal = "비밀번호는 16자리 이하 사용해야 합니다.";
}
if(!isNumMatcher || !isEngMatcher || !isSpcMatcher) { // 3가지 문자가 조합되지 않는 경우
rtCd = "0005";
rtVal = "비밀번호는 영문,숫자,특수문자를 조합 해야합니다.";
}
}
result.put("rtCd", rtCd);
result.put("rtVal", rtVal);
return result;
}
/**
* WEB UI 용 RSA 복호화
*/
public String webDecrypt(CommonMapper commonMapper, String value) throws Exception {
this.getKey(commonMapper);
byte[] bPrivateKey = Base64.decodeBase64(sPrivateKey.getBytes());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(bPrivateKey);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
byte[] encryptBytes = hexStringToByteArray(value);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bPlain = cipher.doFinal(encryptBytes);
return new String(bPlain);
}
/**
* DB에 있는 공개키, 비공개키 값을 조회하고 없으면 생성
*/
private void getKey(CommonMapper commonMapper) {
// 공통코드에 등록된 Security Code 정보 가져오기
Common common = new Common();
common.setComnGrpCd(Constant.COMN_GRP_CD_SECURITY_CODE);
common.setComnCd(Constant.COMN_CD_SPUBLICKEY);
Common keyInfo = commonMapper.getCommonGroupCode(common);
sPublicKey = keyInfo.getAddCd1();
common.setComnCd(Constant.COMN_CD_SPRIVATEKEY);
keyInfo = commonMapper.getCommonGroupCode(common);
sPrivateKey = keyInfo.getAddCd1();
}
public static String byteArrayToHexString(byte[] hexByte) {
StringBuilder strbuf = new StringBuilder(hexByte.length * 2);
for (byte b : hexByte) {
if (((int) b & 0xff) < 0x10) {
strbuf.append("0");
}
strbuf.append(Long.toString((int) b & 0xff, 16));
}
return strbuf.toString();
}
private byte[] hexStringToByteArray(String hexString) {
if (hexString == null || hexString.length() % 2 != 0) {
return new byte[]{};
}
byte[] bytes = new byte[hexString.length() / 2];
for (int i = 0; i < hexString.length(); i += 2) {
byte value = (byte) Integer.parseInt(hexString.substring(i, i+2), 16);
bytes[(int) Math.floor(i/2)] = value;
}
return bytes;
}
public String getEncrypt(String decryptValue, int encodingMode, String encryptKey) {
return getEncrypt(decryptValue, 0, 0, 0, encodingMode, encryptKey, "");
}
private String getEncrypt(String decryptValue, int algorithmMode, int encryptMode, int paddingMode, int encodingMode, String encryptKey, String iv) {
if(StringUtils.hasText(decryptValue)){
return decryptValue;
}
String sEncryptValue;
try {
SecretKeySpec spec = new SecretKeySpec(encryptKey.getBytes(), ENCRYPT_ALGORITHM[algorithmMode]);
Cipher cipher = Cipher.getInstance(getTransform(algorithmMode, encryptMode, paddingMode));
if (encryptMode == 1) {
cipher.init(Cipher.ENCRYPT_MODE, spec, new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)));
sEncryptValue = byteArrayToHexString(cipher.doFinal(decryptValue.getBytes(StandardCharsets.UTF_8)));
} else {
cipher.init(Cipher.ENCRYPT_MODE, spec);
sEncryptValue = Base64.encodeBase64String((cipher.doFinal(decryptValue.getBytes(StandardCharsets.UTF_8))));
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new SecurityException(e);
}
return sEncryptValue;
}
public String getDecrypt(String encryptValue, int encodingMode, String encryptKey) {
return getDecrypt(encryptValue, 0, 0, 0, encodingMode, encryptKey, "");
}
private String getDecrypt(String encryptValue, int algorithmMode, int encryptMode, int paddingMode, int encodingMode, String encryptKey, String iv) {
if(StringUtils.hasText(encryptValue)){
return encryptValue;
}
String sDecryptValue;
try {
SecretKeySpec skeySpec = new SecretKeySpec(encryptKey.getBytes(), ENCRYPT_ALGORITHM[algorithmMode]);
Cipher cipher = Cipher.getInstance(getTransform(algorithmMode, encryptMode, paddingMode));
if (encryptMode == 1) {
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)));
sDecryptValue = new String(cipher.doFinal(hexStringToByteArray(encryptValue)), StandardCharsets.UTF_8);
} else {
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
sDecryptValue = new String(cipher.doFinal(Base64.decodeBase64(encryptValue)), StandardCharsets.UTF_8);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new SecurityException(e);
}
return sDecryptValue;
}
private String getTransform(int algorithmMode, int encryptMode, int paddingMode) {
return ENCRYPT_ALGORITHM[algorithmMode] + "/" +
ENCRYPT_MODE[encryptMode] + "/" +
ENCRYPT_PADDING[paddingMode];
}
/**
* salt 이용한 sha-256 암호화
*/
public String getSha256(String pwd, String salt) throws NoSuchAlgorithmException, UnsupportedEncodingException {
byte[] byteSalt = salt.getBytes();
byte[] bytePwd = pwd.getBytes();
byte[] bytes = new byte[bytePwd.length + byteSalt.length];
//bytes 합치기
System.arraycopy(bytePwd, 0, bytes, 0, bytePwd.length);
System.arraycopy(byteSalt, 0, bytes, bytePwd.length, byteSalt.length);
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(bytes);
byte[] b = md.digest();
StringBuilder sb = new StringBuilder();
for (byte value : b) {
sb.append(Integer.toString((value & 0xFF) + 256, 16).substring(1));
}
return sb.toString();
}
/**
* salt 생성
*/
public String getSalt() throws NoSuchAlgorithmException {
Random random = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[20];
random.nextBytes(salt);
StringBuilder sb = new StringBuilder();
for (byte b : salt) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
/**
* HASH 생성
*/
public String generateHASH(String userPasswd, String bodyJsonString, String SALT, String TIMESTAMP) {
String HASH = "";
try {
String data = bodyJsonString + SALT;
String key = userPasswd + "_" + TIMESTAMP;
// calculate Hmac
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
sha256_HMAC.init(secret_key);
// byte array to hex
byte[] byteArray = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder(byteArray.length * 2);
for (byte b : byteArray)
sb.append(String.format("%02x", b));
HASH = sb.toString();
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
log.error(e.getMessage(), e);
}
return HASH.toUpperCase();
}
}