일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- VirtualBox
- IntelliJ
- JavaScript
- plugin
- resource
- profile
- Windows 10
- import
- context
- Source
- 네트워크
- 단축키
- ssh
- GIT
- Mac
- Windows
- grep
- Eclipse
- 줄바꿈 문자
- Quartz
- bash
- tomcat
- vscode
- web.xml
- maVen
- port
- netsh
- find
- lsof
- xargs
- Today
- Total
develog
AES 본문
import java.math.BigInteger;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AES {
public static void main(String[] args) throws Exception {
AES main = new AES();
SecretKey key = main.generateSecretKey();
String message = "암호화 테스트";
System.out.println("암호전 : " + message);
String encrypted = main.encrypt(message, key);
System.out.println("암호화 : " + encrypted);
String decrypted = main.decrypt(encrypted, key);
System.out.println("복호화 : " + decrypted);
}
public SecretKey generateSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
SecretKey skey = keygen.generateKey();
return skey;
}
public String encrypt(String message, SecretKey key) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(message.getBytes());
return new BigInteger(encrypted).toString(16);
}
public String decrypt(String encrypted, SecretKey key) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(new BigInteger(encrypted, 16).toByteArray());
return new String(decrypted);
}
}
'Dev > Java' 카테고리의 다른 글
variable arguments (0) | 2013.02.23 |
---|---|
XML DOM (0) | 2013.01.27 |
poi 대용량 엑셀 write (0) | 2013.01.08 |
Quartz job fire manually (0) | 2012.12.18 |
Quartz - Defining a Job (0) | 2012.12.14 |