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);
}
}
728x90
반응형
'Study > Java' 카테고리의 다른 글
Gson을 이용한 json을 객체에 담기 (0) | 2020.11.27 |
---|---|
RSA 암호화, 복호화 (0) | 2020.11.12 |
자바 암호화 sha- 256, sha-512 (0) | 2020.11.10 |
[FCM]파이어베이스에서 안드로이드로 보내기 (0) | 2020.04.29 |
QR 코드, 바코드 생성하기 (1) | 2020.02.27 |
댓글