Notice
Recent Posts
Recent Comments
Link
투케이2K
18. (spring/스프링) cipher 사용해 aes 128 암호화 인코딩 , 디코딩 수행 실시 본문
[개발 환경 설정]
개발 툴 : inteli j
개발 언어 : spring
[소스코드]
/** ======= [aes128 인코딩 , 디코딩 선언 방법] ======= **/
aes128EncodeData = getAES128encode("hello");
aes128DecodeData = getAES128decode(aes128EncodeData);
/** ======= [aes128 비밀키 전역 변수 선언 실시] ======= **/
public static String aes128SecretKey = "0123456789abcdef"; //TODO [aes128 = 16 byte / aes192 = 24 byte / aes256 = 32 byte]
public static byte[] aes128ivBytes = new byte[16]; // TODO [일반 사용 방식]
//public static byte[] aes128ivBytes = "0123456789abcdef".getBytes(); // TODO [16 byte Enter IV (Optional) 지정 방식]
String aes128EncodeData = "";
String aes128DecodeData = "";
/** ======= [aes128 비밀키 사용해 인코딩 수행] ======= **/
public static String getAES128encode(String encodeData) {
String result = "";
try {
System.out.println("\n");
System.out.println("=======================================");
System.out.println("[ModuleApiController] : [getAES128encode] : [aes128 비밀키 사용해 인코딩 수행 실시]");
System.out.println("[secretKey] : " + String.valueOf(aes128SecretKey));
System.out.println("[encodeData] : " + String.valueOf(encodeData));
System.out.println("=======================================");
System.out.println("\n");
byte[] textBytes = encodeData.getBytes("UTF-8");
AlgorithmParameterSpec ivSpec = new IvParameterSpec(aes128ivBytes);
SecretKeySpec newKey = new SecretKeySpec(aes128SecretKey.getBytes("UTF-8"), "AES");
Cipher cipher = null;
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
System.out.println("\n");
System.out.println("=======================================");
System.out.println("[ModuleApiController] : [getAES128encode] : [aes128 비밀키 사용해 인코딩 수행 결과]");
System.out.println("[secretKey] : " + String.valueOf(aes128SecretKey));
System.out.println("[결과] : " + String.valueOf(Base64Utils.encodeToString(cipher.doFinal(textBytes))));
System.out.println("=======================================");
System.out.println("\n");
// TODO [리턴 데이터 반환 실시]
return Base64Utils.encodeToString(cipher.doFinal(textBytes));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/** ======= [aes128 비밀키 사용해 디코딩 수행] ======= **/
public static String getAES128decode(String decodeData) {
String result = "";
try {
System.out.println("\n");
System.out.println("=======================================");
System.out.println("[ModuleApiController] : [getAES128decode] : [aes128 비밀키 사용해 디코딩 수행 실시]");
System.out.println("[secretKey] : " + String.valueOf(aes128SecretKey));
System.out.println("[decodeData] : " + String.valueOf(decodeData));
System.out.println("=======================================");
System.out.println("\n");
// TODO [인풋으로 들어온 base64 문자열 데이터를 가지고 디코딩 수행 실시]
byte[] textBytes = Base64Utils.decode(decodeData.getBytes());
AlgorithmParameterSpec ivSpec = new IvParameterSpec(aes128ivBytes);
SecretKeySpec newKey = new SecretKeySpec(aes128SecretKey.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
System.out.println("\n");
System.out.println("=======================================");
System.out.println("[ModuleApiController] : [getAES128decode] : [aes128 비밀키 사용해 디코딩 수행 결과]");
System.out.println("[secretKey] : " + String.valueOf(aes128SecretKey));
System.out.println("[결과] : " + String.valueOf(new String(cipher.doFinal(textBytes), "UTF-8")));
System.out.println("=======================================");
System.out.println("\n");
// TODO [리턴 데이터 반환 실시]
return new String(cipher.doFinal(textBytes), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
[인코딩 결과출력]
[디코딩 결과출력]
[온라인에서 aes 인코딩, 디코딩 확인방법]
https://blog.naver.com/kkh0977/222447254921
반응형
'Spring' 카테고리의 다른 글
Comments