양방향 암호화

importjava.security.Key;
importjavax.crypto.Cipher;importjavax.crypto.spec.SecretKeySpec;
publicclassAESCrypto {
   private String use = “Y”;
   private String salt;

   public String getUse() {
       return use;
   }

   publicvoidsetUse(String use) {
       this.use = use;
   }

   public String getSalt() {
       return salt;
   }

   publicvoidsetSalt(String salt) {
       this.salt = salt;
   }

   publicstatic Key generateKey(String algorithm, byte[] keyData) {
       SecretKeySpec keySpec = new SecretKeySpec(keyData, algorithm);
       return keySpec;
   }

   public String Encrypt(String src) throws Exception {
       if (!“Y”.equals(use)) return src;
       Key key = generateKey(“AES”, toBytes(salt, 16));
       String transformation = “AES”;
       Cipher cipher = Cipher.getInstance(transformation);
       cipher.init(Cipher.ENCRYPT_MODE, key);
       byte[] plain = src.getBytes();
       byte[] encrypt = cipher.doFinal(plain);
       return toHexString(encrypt);
   }

   public String Decrypt(String hex) throws Exception {
       if (!“Y”.equals(use)) return hex;
       Key key = generateKey(“AES”, toBytes(salt, 16));
       String transformation = “AES”;
       Cipher cipher = Cipher.getInstance(transformation);
       cipher.init(Cipher.DECRYPT_MODE, key);
       byte[] encrypt = toBytesFromHexString(hex);
       byte[] decrypt = cipher.doFinal(encrypt);
       returnnew String(decrypt);
   }

   publicstaticbyte[] toBytes(String digits, int radix)
       throws IllegalArgumentException, NumberFormatException {
           if (digits == null) {
               returnnull;
           }
           if (radix != 16 && radix != 10 && radix != 8) {
               thrownew IllegalArgumentException(“For input radix: \”” + radix
                       + “\””);
           }
           int divLen = (radix == 16) ? 2 : 3;
           int length = digits.length();
           if (length % divLen == 1) {
               thrownew IllegalArgumentException(“For input string: \”” + digits
                       + “\””);
           }
           length = length / divLen;
           byte[] bytes = newbyte[length];
           for (int i = 0; i < length; i++) {
               int index = i * divLen;
               bytes[i] = (byte) (Short.parseShort(
                           digits.substring(index, index + divLen), radix));
           }
           return bytes;
       }

   publicstaticbyte[] toBytesFromHexString(String digits)
       throws IllegalArgumentException, NumberFormatException {
           if (digits == null) {
               returnnull;
           }
           int length = digits.length();
           if (length % 2 == 1) {
               thrownew IllegalArgumentException(“For input string: \”” + digits
                       + “\””);
           }
           length = length / 2;
           byte[] bytes = newbyte[length];
           for (int i = 0; i < length; i++) {
               int index = i * 2;
               bytes[i] = (byte) (Short.parseShort(
                           digits.substring(index, index + 2), 16));
           }
           return bytes;
       }

   publicstatic String toHexString(byte[] bytes) {
       if (bytes == null) {
           returnnull;
       }

       StringBuffer result = new StringBuffer();
       for (byte b : bytes) {
           result.append(Integer.toString((b & 0xF0) >> 4, 16));
           result.append(Integer.toString(b & 0x0F, 16));
       }
       return result.toString();
   }
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s