import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
/**
* 암/복호화
* @since 2024.10.24
*/
public class AesUtil {
private final static String SALT = "CSUZKNWMJAA4ODP8OSWPWIV9VUM8RIR6RABMNFYH6TJ90XUCUN0GFIW5RFC7L0IP";
private final static String PASSWORD = "MY_PASSWORD_KEY";
private final static String IV = "7QVRN84YNJJSLHX4N9Y5GPGC0QKSW7HJ";
private final static String INSTANCE = "PBKDF2WithHmacSHA1";
private final static String CIPHER_INSTANCE = "AES/CBC/PKCS5Padding";
private final static String ALGORITHM = "AES";
/**
* 암호화
*/
public static String encrypt(String license) {
String encryptString = "";
try {
Key key = generateKey();
byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, license.getBytes(StandardCharsets.UTF_8));
encryptString = new String(Base64.encodeBase64(encrypted));
} catch (Exception e) {
throw new IllegalStateException(e);
}
return encryptString;
}
/**
* 복호화
*/
public static String decrypt(String decrypedLicense) {
String decryptString = "";
try {
Key key = generateKey();
byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, Base64.decodeBase64(decrypedLicense));
decryptString = new String(decrypted, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new IllegalStateException(e);
}
return decryptString;
}
// 키 생성
private static Key generateKey() throws NoSuchAlgorithmException, InvalidKeySpecException, DecoderException {
SecretKeyFactory factory = SecretKeyFactory.getInstance(INSTANCE);
KeySpec spec = new PBEKeySpec(PASSWORD.toCharArray(), hex(SALT), 1000, 128);
return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), ALGORITHM);
}
// 암/복호화 처리
private static byte[] doFinal(int mode, Key key, byte[] bytes) throws DecoderException
, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException
, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException {
Cipher cipher = Cipher.getInstance(CIPHER_INSTANCE);
cipher.init(mode, key, new IvParameterSpec(hex(IV)));
return cipher.doFinal(bytes);
}
// toHex
public static byte[] hex(String str) throws DecoderException {
return Hex.decodeHex(str.toCharArray());
}
}
간만에 끄적이다 만들어서 올려보는 자바 암복호화 소스
여기에 적힌 SALT와 IV는 즉석으로 문자열을 생성해 만든것이므로 어디에도 쓰이는 경우가 없다
참고로 쓰려면 다르게 값을 주는 것이 좋을 수도 있다
'Dev Back > JAVA' 카테고리의 다른 글
| [JAVA] Field injection is not recommended (0) | 2025.02.21 |
|---|---|
| [adoc] 한글이 깨지는 이슈 (1) | 2024.07.03 |
| [JAVA] 접근 제어자의 종류와 사용법 (0) | 2024.06.26 |
| [JAVA] AES256 암/복호화 (0) | 2024.06.14 |
| [JAVA] @Valid (0) | 2024.06.11 |