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 |
Tags
- spring batch 코드
- tomcat
- spring boot
- spring bean
- IntelliJ
- spring-mvc
- docker
- spring boot tomcat
- static inner class
- spring-webmvc
- batch
- Spring
- ORM
- JUnit
- spring pid
- Spring Data REST
- Data REST
- SuperTypeToken
- ApplicationPidFileWriter
- JPA
- spring jpa
- Spring Batch
- 톰캣
- spring camp
- 세미나
- REST API
- JPA mapping
- 코드 리뷰
- spring-webmvc #코드읽기
- Spring Data JPA
Archives
- Today
- Total
woniper
[java] AES256 암호화 본문
https://github.com/Woniper/JavaExample/tree/master/AES256
1. commons-codec-1.9.jar 추가
2. AES256 구현
package com.woniper.aes; import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class AES256Util { private String iv; private Key keySpec; public AES256Util(String key) throws UnsupportedEncodingException { this.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); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); this.keySpec = keySpec; } // 암호화 public String aesEncode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{ Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes())); byte[] encrypted = c.doFinal(str.getBytes("UTF-8")); String enStr = new String(Base64.encodeBase64(encrypted)); return enStr; } //복호화 public String aesDecode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); c.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes("UTF-8"))); byte[] byteStr = Base64.decodeBase64(str.getBytes()); return new String(c.doFinal(byteStr),"UTF-8"); } }
3. TEST
package com.woniper.aes; public class MainAES { public static void main(String[] args) throws Exception { String key = "aes256-test-key!!"; // key는 16자 이상 AES256Util aes256 = new AES256Util(key); String text = "암호화되지 않은 문자입니다."; String encText = aes256.aesEncode(text); String decText = aes256.aesDecode(encText); System.out.println("암호화할 문자 : " + text); System.out.println("암호화된 문자 : " + encText); System.out.println("복호화된 문자 : " + decText); } }
4. 출력 결과
'Java' 카테고리의 다른 글
java8 stream match (0) | 2016.12.03 |
---|---|
SuperTypeToken #2 TypeToken(ModelMapper)과 ParameterizedTypeReference(Spring) 차이 (0) | 2016.11.21 |
SuperTypeToken #1 TypeToken(ModelMapper)과 ParameterizedTypeReference(Spring) 사용법 (0) | 2016.11.12 |
HashMap.put의 return 객체는 무엇일까? (2) | 2016.08.05 |
[AWS : Amazon Web Service] Java S3 사용 (8) | 2014.07.13 |
Comments