티스토리 뷰

program

java AES256 암호화 복호화 소스

littlecarbb 2014. 12. 8. 12:21

 

1. AES256Cipher.java

 

package test.aes256;

 

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidAlgorithmParameterException;
import org.apache.commons.codec.binary.Base64;

//고급 암호화 표준(AES, Advanced Encryption Standard)
//암호화와 복호화 과정에서 동일한 키를 사용하는 대칭 키 알고리즘
public class AES256Cipher {
 
 private static volatile AES256Cipher INSTANCE;
 
 final static String secretKey   = "12345678901234567890123456789012"; //32bit
 static String IV                = ""; //16bit
 
 public static AES256Cipher getInstance(){
     if(INSTANCE==null){
         synchronized(AES256Cipher.class){
             if(INSTANCE==null)
                 INSTANCE=new AES256Cipher();
         }
     }
     return INSTANCE;
 }
 
 private AES256Cipher(){
     IV = secretKey.substring(0,16);
 }
 
  //암호화
  public static String AES_Encode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
      byte[] keyData = secretKey.getBytes();
 
   SecretKey secureKey = new SecretKeySpec(keyData, "AES");
  
   Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
   c.init(Cipher.ENCRYPT_MODE, secureKey, new IvParameterSpec(IV.getBytes()));
  
   byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
   String enStr = new String(Base64.encodeBase64(encrypted));
  
   return enStr;
  }
 
  //복호화
  public static String AES_Decode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
    byte[] keyData = secretKey.getBytes();
    System.out.println(new String(keyData) );
    SecretKey secureKey = new SecretKeySpec(keyData, "AES");
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, secureKey, new IvParameterSpec(IV.getBytes("UTF-8")));
  
    byte[] byteStr = Base64.decodeBase64(str.getBytes());
  
    return new String(c.doFinal(byteStr),"UTF-8");
  }
 
  //키생서
  public static byte[] generationAES256_KEY() throws NoSuchAlgorithmException{
   KeyGenerator kgen = KeyGenerator.getInstance("AES");
   kgen.init(256);
   SecretKey key = kgen.generateKey();
  
   return key.getEncoded();
  
  }
}

 

2. AES256CipherTest

JUNIT 4를 설치하고 테스트 하자. JUNIT를 설치하기 싫으면 아래의 assertThat를 다 지우자.

 

package test.aes256;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class AES256CipherTest {

 /**
  * @param args
  * @throws BadPaddingException
  * @throws IllegalBlockSizeException
  * @throws InvalidAlgorithmParameterException
  * @throws NoSuchPaddingException
  * @throws NoSuchAlgorithmException
  * @throws UnsupportedEncodingException
  * @throws InvalidKeyException
  */
 public static void main(String[] args) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
  
    String id = "testid";
    String custrnmNo = "111111";
    String custNm = "테스트";
  
  
    AES256Cipher aes256 = AES256Cipher.getInstance();
   
    String enId = aes256.AES_Encode(id);    
    String enYyyymmdd = aes256.AES_Encode(custrnmNo);    
    String enCustNm = aes256.AES_Encode(custNm);   
   
    String desId = aes256.AES_Decode(enId);    
    String desYyyymmdd = aes256.AES_Decode(enYyyymmdd);    
    String desCustNm = aes256.AES_Decode(enCustNm);
   
    assertThat(id, is(desId));
    assertThat(custrnmNo, is(desYyyymmdd));    
    assertThat(custNm, is(desCustNm));
   
    byte[] tmp = aes256.generationAES256_KEY();
    System.out.println(new String(tmp) );
   
    System.out.println(enId);
    System.out.println(desId);

 }

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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
글 보관함