대칭 키로서, 로컬 PC에서 사용할 대칭키 알고리즘을 사용한 클래스이다.

import java.security.InvalidKeyException;
import java.security.Key;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class SimpleCodec {
 private static String algorithm = "AES"; 
 private static Key key = null;
 private static Cipher cipher = null;
 
 public SimpleCodec() throws Exception {
  
  key = KeyGenerator.getInstance(algorithm).generateKey();
  cipher = Cipher.getInstance(algorithm);
  
 }
 private String getTimeKey() {
  SimpleDateFormat format0 = new SimpleDateFormat("MMddHH");
  Date now = new Date();
  // add 1 minute
  now.setTime(now.getTime() + 60 * 1000);
  String dtime = format0.format(now);
  return dtime;
 }
 public String encrypt(String key) throws Exception {
  byte[] encryptionBytes = encryptImpl(key);
  BASE64Encoder encoder = new BASE64Encoder();
  String encodeString = encoder.encode(encryptionBytes);
  return encodeString;
 }
 public String encrypt() throws Exception {
  byte[] encryptionBytes = encryptImpl(getTimeKey());
  BASE64Encoder encoder = new BASE64Encoder();
  String encodeString = encoder.encode(encryptionBytes);
  return encodeString;
 }
 public String decrypt(String encodedString) throws Exception {
  if (encodedString == null) {
   return null;
  }
  BASE64Decoder decoder = new BASE64Decoder();
  return decrypt(decoder.decodeBuffer(encodedString));
 }
 private static byte[] encryptImpl(String input) throws InvalidKeyException, BadPaddingException,
   IllegalBlockSizeException {
  cipher.init(Cipher.ENCRYPT_MODE, key);
  byte[] inputBytes = input.getBytes();
  return cipher.doFinal(inputBytes);
 }
 private static String decrypt(byte[] encryptionBytes) throws InvalidKeyException, BadPaddingException,
   IllegalBlockSizeException {
  cipher.init(Cipher.DECRYPT_MODE, key);
  byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
  String recovered = new String(recoveredBytes);
  return recovered;
 }
}



반면, 네트웍을 통해서 대칭키를 가지고, 서로 통신해야 하는 경우라면, 다음을 사용해야 하는 것이 좋다.
즉, Key를 서로 공유할 수 있도록 하는 것이다.


public class SimpleCodec {
 private static Key key = null;
 private static Cipher cipher = null;
 
 byte[] symKey = {
  (byte)0xca, (byte)0x00, (byte)0x25, (byte)0x06,
  (byte)0x28, (byte)0xae, (byte)0xd2, (byte)0xbd,
  (byte)0x2b, (byte)0x7e, (byte)0x15, (byte)0x16,
  (byte)0x28, (byte)0xae, (byte)0xd2, (byte)0xa6,
  (byte)0x2c, (byte)0x7e, (byte)0x10, (byte)0x96,
  (byte)0x28, (byte)0xff, (byte)0xd2, (byte)0xa6,
 };
 public SimpleCodec() throws Exception {
  key = new SecretKeySpec(symKey, 0, symKey.length, "DESede");
  cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");

 }
 private String getTimeKey() {
  SimpleDateFormat format0 = new SimpleDateFormat("MMddHH");
  Date now = new Date();
  // add 1 minute
  now.setTime(now.getTime() + 60 * 1000);
  String dtime = format0.format(now);
  return dtime;
 }
 public String encrypt(String key) throws Exception {
  byte[] encryptionBytes = encryptImpl(key);
  BASE64Encoder encoder = new BASE64Encoder();
  String encodeString = encoder.encode(encryptionBytes);
  return encodeString;
 }
 public String encrypt() throws Exception {
  byte[] encryptionBytes = encryptImpl(getTimeKey());
  BASE64Encoder encoder = new BASE64Encoder();
  String encodeString = encoder.encode(encryptionBytes);
  return encodeString;
 }
 public String decrypt(String encodedString) throws Exception {
  if (encodedString == null) {
   return null;
  }
  BASE64Decoder decoder = new BASE64Decoder();
  return decrypt(decoder.decodeBuffer(encodedString));
 }
 private static byte[] encryptImpl(String input) throws InvalidKeyException, BadPaddingException,
   IllegalBlockSizeException {
  cipher.init(Cipher.ENCRYPT_MODE, key);
  byte[] inputBytes = input.getBytes();
  return cipher.doFinal(inputBytes);
 }
 private static String decrypt(byte[] encryptionBytes) throws InvalidKeyException, BadPaddingException,
   IllegalBlockSizeException {
  cipher.init(Cipher.DECRYPT_MODE, key);
  byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
  String recovered = new String(recoveredBytes);
  return recovered;
 }
}



참고자료
http://blog.sdnkorea.com/blog/469
http://kr.sun.com/developers/techtips/c2004_01_16.htm

'java core' 카테고리의 다른 글

Java Memroy Model  (0) 2009.08.06
Java Pattern 활용하기  (0) 2009.07.23
URLConnection vs HTTPURLConnection  (0) 2009.07.20
Finally 범위  (0) 2009.07.15
How to get object size in java.  (0) 2009.05.29
Posted by '김용환'
,