양방향 암호화

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();
   }
}

 
이 글은 java 카테고리에 분류되었고 태그가 있으며 님에 의해 에 작성되었습니다.

답글 남기기

아래 항목을 채우거나 오른쪽 아이콘 중 하나를 클릭하여 로그 인 하세요:

WordPress.com 로고

WordPress.com의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Twitter 사진

Twitter의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Facebook 사진

Facebook의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

%s에 연결하는 중