자바 AES 암호화 / 복호화
package api.etc.base;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
class JavaEnCryto
{
public static void main(String[] args)
{
try
{
String originalText = "plain text";
String key = "key";
String en = Encrypt( originalText, key);
String de = Decrypt( en, key);
System.out.println( "Original Text is " + originalText);
System.out.println( "Encrypted Text is " + en );
System.out.println( "Decrypted Text is " + de );
}
catch (Exception ex)
{
System.out.println("오류");
}
}
public static String Decrypt(String text, String key) throws Exception
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] keyBytes= new byte[16];
byte[] b= key.getBytes("UTF-8");
int len= b.length;
if (len > keyBytes.length) len = keyBytes.length;
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);
BASE64Decoder decoder = new BASE64Decoder();
byte [] results = cipher.doFinal(decoder.decodeBuffer(text));
return new String(results,"UTF-8");
}
public static String Encrypt(String text, String key) throws Exception
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] keyBytes= new byte[16];
byte[] b= key.getBytes("UTF-8");
int len= b.length;
if (len > keyBytes.length) len = keyBytes.length;
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec);
byte[] results = cipher.doFinal(text.getBytes("UTF-8"));
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(results);
}
}
'Java' 카테고리의 다른 글
리눅스 centos 자바 jdk 삭제 (0) | 2014.07.17 |
---|---|
자바 SMS 문자메세지 보내기 (cool sms) (12) | 2014.07.04 |
Java에서 엑셀이용하기 (xlsl, xls) (0) | 2014.06.19 |
리눅스 자바 패키지 컴파일 / 실행 (0) | 2014.06.03 |
리눅스에서 자바 컴파일, 실행 (javac, java) (0) | 2014.05.16 |
댓글