티스토리 뷰

IT/JAVA

AES256 암복호화

김보야 2016. 3. 29. 17:59

개발환경

JDK 1.6




설치파일

commons-codec-1.10.jar


local_policy.jar


US_export_policy.jar



Apache Commons Codec(commons-codec-1.10.jar)

http://commons.apache.org/proper/commons-codec/download_codec.cgi


Java JCE Policy(local_policy.jar, US_export_policy.jar)

※ jdk버전에 맞게 아래에서 다운로드


jdk6

http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html


jdk7

http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html


jdk8

http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html




설치경로

프로젝트 라이브러리 경로/commons-codec-1.10.jar

JDK경로/jre/lib/security/local_policy.jar

JDK경로/jre/lib/security/US_export_policy.jar




소스

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
32
33
34
35
package com.boya.common.secure;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
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 AES256Crypter {
    
    public static byte[] byteIv = { 0x000x000x000x000x000x000x000x000x000x000x000x000x000x000x000x00 };
    public static String strKey = "abcdefghijklmnopqrstuvwxyz012345";
    
    public static String encrypt(String strData) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        byte[] byteText = strData.getBytes("UTF-8");
        AlgorithmParameterSpec aps = new IvParameterSpec(byteIv);
        SecretKeySpec sks = new SecretKeySpec(strKey.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, sks, aps);
        return Base64.encodeBase64String(cipher.doFinal(byteText));
    }
    
    public static String decrypt(String strData) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        byte[] byteText = Base64.decodeBase64(strData);
        AlgorithmParameterSpec aps = new IvParameterSpec(byteIv);
        SecretKeySpec sks = new SecretKeySpec(strKey.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, sks, aps);
        return new String(cipher.doFinal(byteText), "UTF-8");
    }
}
cs


'IT > JAVA' 카테고리의 다른 글

Unsupported major.minor version  (0) 2017.09.20
자바 메소드명, 클래스명, 줄번호, 파일명 가져오기  (0) 2017.09.14
자바 파일 복사  (0) 2017.03.27
SDK, JRE, JDK  (0) 2016.03.28
JAVA SE, EE, ME  (0) 2016.03.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
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
글 보관함