티스토리 뷰
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);
}
}
[출처] java AES256 암호화 복호화 소스 |작성자 루티
'program' 카테고리의 다른 글
Selenium을 이용한 UI 테스트 (0) | 2014.12.22 |
---|---|
Hudson CI(지속적 통합) 서버로 PHP 개발 환경 구축해보기 (0) | 2014.12.22 |
java AES 128 (0) | 2014.12.08 |
[오라클] GRANT , 오라클 권한 부여의 모든 것 (0) | 2014.10.27 |
ORA-08002: sequence is not yet defined in this session. (0) | 2014.10.24 |