반응형
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
public class decPw {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 16자리 임의 값 입력 필수
String AESKey = "aaaaaaaaaaaaaaaa";
String pwd;
System.out.println("Enter the encryption value :");
// 사용자에게 값 입력 받아서 복호화
pwd = sc.next();
String decPwd = decrypt(pwd, AESKey);
System.out.println("----decode result----\n" + decPwd);
}
private static String encrypt(String input, String key) {
byte[] crypted = null;
SecretKeySpec sks = new SecretKeySpec(key.getBytes(), "AES");
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, sks);
crypted = cipher.doFinal(input.getBytes());
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
String str = encoder.encode(crypted);
return new String(str);
}
private static String decrypt(String input, String key) {
byte[] output = null;
BASE64Decoder decoder = new BASE64Decoder();
SecretKeySpec sks = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, sks);
output = cipher.doFinal(decoder.decodeBuffer(input));
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new String (output);
}
}
반응형
'프로그래밍 노트 > Spring, Java, Kotlin' 카테고리의 다른 글
thymeleaf 와 tailwind 로 자바 서블릿 프로젝트 구성하기 (0) | 2023.11.15 |
---|---|
Springboot에서 messages.properties로 다국어 사용하는 방법 (reload까지) (2) | 2022.08.17 |
SpringBoot 2.6에서 swagger 3.0 사용 (11) | 2021.12.15 |
JAVA로 API 개발할 때 DTO파일 읽어서 필요하거나 필요 없는 필드 확인하기 (0) | 2021.08.03 |
Java 파일 복사 프로세스 (eml파일 헤더 변조하여 복사) (0) | 2020.11.03 |