Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- OPENDART
- XML무료뷰어
- #알뜰폰
- xmlviewer
- apple여의도
- json #paramquery
- productivity Power Tools
- 여의도IFC
- 유심무료
- advil
- vs2017
- applestore
- VS2015
- 알뜰유심
- xml뷰어
- 유니콘
- 금융감독원API
- VLOOKUP
- 유니콘광고차단
- 광고차단
- VS2019
- 유니콘앱
- 애드빌용법
- AWS
- 소스비교
- 애드빌
- IFC
- 알뜰요금제
- SK7MOBILE
- 알뜰
Archives
- Today
- Total
체크개발자's Blog
[JAVA] AES128 CBC 암복호화 예제 본문
자바의 AES128 CBC 암호화 예제입니다.
참고로 CBC 암호화는 IV 라는 벡터값이 하나가 더 들어갑니다.
public Key getAESKey() throws Exception {
String iv;
Key keySpec;
String key = "1234567890123456";
iv = key.substring(0, 16);
byte[] keyBytes = new byte[16];
byte[] b = key.getBytes("UTF-8");
int len = b.length;
if (len > keyBytes.length) {
len = keyBytes.length;
}
System.arraycopy(b, 0, keyBytes, 0, len);
keySpec = new SecretKeySpec(keyBytes, "AES");
return keySpec;
}
// 암호화
public String encAES(String str) throws Exception {
Key keySpec = getAESKey();
String iv = "0987654321654321";
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes("UTF-8")));
byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
String enStr = new String(Base64.encodeBase64(encrypted));
return enStr;
}
// 복호화
public String decAES(String enStr) throws Exception {
Key keySpec = getAESKey();
String iv = "0987654321654321";
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes("UTF-8")));
byte[] byteStr = Base64.decodeBase64(enStr.getBytes("UTF-8"));
String decStr = new String(c.doFinal(byteStr), "UTF-8");
return decStr;
}
출처 : https://www.fun25.co.kr/blog/java-aes128-cbc-encrypt-decrypt-example/?page=7
'프로그래밍 > JAVA' 카테고리의 다른 글
AES/CBC/PKCS5Padding (0) | 2017.08.23 |
---|---|
[Spring] 스프링 파일업로드/ file upload/ 파일업로드 한글깨짐 (0) | 2017.08.07 |
[javascript] 현재 날짜 가져오기 (0) | 2017.07.21 |
File 경로 조치 (\, /) (0) | 2017.07.06 |
JAVA 파일 암호화 (0) | 2017.05.26 |
Comments