Files
elink-online-common/src/main/java/com/eactive/eai/util/CryptoUtil.java
T
2026-03-03 14:09:55 +09:00

70 lines
2.5 KiB
Java

package com.eactive.eai.util;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
//TOUSE
//kbank 어댑터에서 업무데이터의 암호화 필드에 대한 암복호화
//OAuth2.0 의 ClientID를 통해 등록된 key를 조회하여 사용
public class CryptoUtil {
public static final String defaultCharset = "utf-8";
private CryptoUtil() {
}
public static String encryptAES(String key, String text) {
return encrypt(key, text, "AES");
}
public static String decryptAES(String key, String base64EncodedText) {
return decrypt(key, base64EncodedText, "AES");
}
public static String encrypt(String key, String text, String algoritm) throws IllegalArgumentException {
try {
SecretKeySpec secureKey = new SecretKeySpec(key.getBytes(), algoritm);
Cipher cipher = Cipher.getInstance(algoritm);
cipher.init(Cipher.ENCRYPT_MODE, secureKey);
byte[] encryptData = cipher.doFinal(text.getBytes(defaultCharset));
return new String(Base64.getEncoder().encode(encryptData));
} catch (Exception e) {
throw new IllegalArgumentException(e.toString());
}
}
public static String decrypt(String key, String text, String algoritm) throws IllegalArgumentException {
try {
SecretKeySpec secureKey = new SecretKeySpec(key.getBytes(), algoritm);
byte[] encryptedData = Base64.getDecoder().decode(text.getBytes(defaultCharset));
Cipher cipher = Cipher.getInstance(algoritm);
cipher.init(Cipher.DECRYPT_MODE, secureKey);
byte[] plainText = cipher.doFinal(encryptedData);
return new String(plainText);
} catch (Exception e) {
throw new IllegalArgumentException(e.toString());
}
}
public static void main(String[] args) {
String key = "ONAuROzlqWB4VuBwO5C/FA==";
String text = "Hello AES 한글";
// System.out.println("Original Text: " + text);
long startEncryptTime = System.nanoTime();
String enc = encryptAES(key, text);
long endEncryptTime = System.nanoTime();
long encryptDuration = endEncryptTime - startEncryptTime;
// System.out.println("Encrypted Text: " + enc);
// System.out.println("Encryption took: " + encryptDuration + " nanoseconds");
long startDecryptTime = System.nanoTime();
String dec = decryptAES(key, enc);
long endDecryptTime = System.nanoTime();
long decryptDuration = endDecryptTime - startDecryptTime;
// System.out.println("Decrypted Text: " + dec);
// System.out.println("Decryption took: " + decryptDuration + " nanoseconds");
}
}