티스토리 뷰
AES 는 PKI 같은 양방향 암호화 알고리즘 입니다.
하지만 PKI(공개키 방식) 은 암호화키와 복호화키가 따로 존재해서 공개키로 암호화 하고 비밀키로 복호화 할수 있습니다.
AES 방식은 암복호화시에 같은 키를 이용 합니다. 또한 AES 암호화 방식은 미국 1급비밀에도 사용가능한 유일하게 공개 되있는 알고리즘중 하나일 만큼 보안적으로 안전하다고 볼수 있습니다.
이하 간단한 샘플 소스를 올려 봅니다.
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package aes; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class AESencrp { private static final String ALGO = "AES"; private static final byte[] keyValue = new byte[]{'m', '2', 's', 'j', 'c', 'o', 'm', 's', 'e', 'c', 'r', 'e', 't', 'k', 'e', 'y'}; public static String encrypt(String Data) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = c.doFinal(Data.getBytes()); String encryptedValue = new BASE64Encoder().encode(encVal); return encryptedValue; } public static String decrypt(String encryptedData) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.DECRYPT_MODE, key); byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); byte[] decValue = c.doFinal(decordedValue); String decryptedValue = new String(decValue); return decryptedValue; } private static Key generateKey() throws Exception { Key key = new SecretKeySpec(keyValue, ALGO); return key; } public static void main(String[] args) throws Exception { String password = "동해물과백두산이 마르고 닳도록 하느님이 보우하사"; String passwordEnc = AESencrp.encrypt(password); String passwordDec = AESencrp.decrypt(passwordEnc); System.out.println("Plain Text : " + password); System.out.println("Encrypted Text : " + passwordEnc); System.out.println("Decrypted Text : " + passwordDec); } } | cs |
'Web Development' 카테고리의 다른 글
[Python] Tab to Space (0) | 2015.03.24 |
---|---|
[Python] Web Image Download (0) | 2014.03.31 |
[웹개발] 웹개발과 한글깨짐 (4) | 2014.02.13 |
[웹개발] Unicode vs UTF-8 (0) | 2014.01.07 |
[웹개발] XSS 와 CSRF 차이점 (0) | 2013.12.19 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크