본문 바로가기
Study/Java

[Java] AES256 암호화 및 복호화

by 오늘만 사는 여자 2020. 11. 10.
728x90
반응형

AES(Advanced Encryption Standard AES) 란?

- 한국어로 변역하면 고급 암호화 표준이며, 대칭키를 쓰는 블럭 암호이다. 높은 안전성과 속도로 인해 인기를 얻어 전 세계적으로 사용되고 있다.

 

현재 AES는 레인달 알고리즘을 가지고 만들었으며, 엄밀하게는 레인달 알고리즘의 여러 가능성 중, 암호화 블럭 크기가 128BIT이며 암호화 키의 길이가 128, 192, 256BIT인 세가지 종류가 AES표준으로 지정되었다. AES-128, AES-192, AES-256으로 불린다.

 


Example

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;


 
/*
 참고 : http://www.imcore.net/encrypt-decrypt-aes256-c-objective-ios-iphone-ipad-php-java-android-perl-javascript-python/
 
 jce patch 적용 되어있어야 하며, commons-codec-1.10.jar 필요 
*/
 
public class AES256Util {

 public static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

 
 /**
  * 일반 문자열을 지정된 키를 이용하여 AES256 으로 암호화
  * @param  String - 암호화 대상 문자열
  * @param  String - 문자열 암호화에 사용될 키
  * @return String - key 로 암호화된  문자열 
  * @exception 
  */
 public static String strEncode(String str, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
  
  byte[] textBytes = str.getBytes("UTF-8");
  AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
  SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
  Cipher cipher = null;
  cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
  return Base64.encodeBase64String(cipher.doFinal(textBytes));
 }

 
 
 /**
  * 암호화된 문자열을 지정된 키를 이용하여 AES256 으로 복호화
  * @param  String - 복호화 대상 문자열
  * @param  String - 문자열 복호화에 사용될 키
  * @return String - key 로 복호화된  문자열 
  * @exception 
  */ 
 public static String strDecode(String str, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
  
  byte[] textBytes = Base64.decodeBase64(str);
  //byte[] textBytes = str.getBytes("UTF-8");
  AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
  SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
  return new String(cipher.doFinal(textBytes), "UTF-8");
 }
 
}

사용법

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 org.apache.commons.codec.binary.Base64;


public class aes256EncTest {

 public static void main(String[] args) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {


  String key = "12345678901234567890123456789012";    //key는 16~32자리
  
  AES256Util aes256Util = new AES256Util();
  
  /* XML 문자열 암호화 */
  String encStr = aes256Util.strEncode(xmlStr, key);
  System.out.println("encStr =============> " + encStr);
  
  
  /* XML 문자열 복호화 */
  String decStr = aes256Util.strDecode(encStr, key);
  System.out.println("decStr =============> " + decStr);


 }

}

 

참조 : freecatz.tistory.com/374

728x90
반응형

댓글