programming/java

[java] md5 aes128 mysql 암호화 적용

labj 2017. 2. 7. 11:06

AES_MD5.zip

mysql.txt

[java] md5 aes128 mysql 암호화 적용

테스트 해 본 결과 MYSQL에서 제공하는 암화화 두가지 (MD5, AES)와 

JAVA 암호화 코드가 동일한 결과가 나옵니다.


* 테스트 결과 ************************************************************************************************

* md5

- 텍스트: test6

- 암호화: 4cfad7076129962ee70c36839a1e3e15


- 텍스트: 12345

- 암호화: 827ccb0eea8a706c4c34a16891f84e7b



* MySQL MD5

- 텍스트: test6

- 암호화: 4cfad7076129962ee70c36839a1e3e15


- 텍스트: 12345

- 암호화: 827ccb0eea8a706c4c34a16891f84e7b



* AES128

- 텍스트: test6

- 암호화: 83997f85bccd7208cac6540b7eb375fd


- 텍스트: 12345

- 암호화: 11c1f96c3bfcfe203d0902d098bf8d64



* MySQL AES128

- 텍스트: test6

- 암호화: 83997f85bccd7208cac6540b7eb375fd


- 텍스트: 12345

- 암호화: 11c1f96c3bfcfe203d0902d098bf8d64

          


* AES128 *****************************************************************************************************

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;


public class Aes128 {

static String key = "1234567890ABCDEF";


public static void main(String[] args) throws Exception {

Aes128 aes = new Aes128();

System.out.println(aes.encrypt("test"));

System.out.println(aes.decrypt("dcf81d4a19f8b26018357230e76f58a2"));

}


public static String encrypt(String message) throws Exception {

if (message == null) {

return null;

} else {

SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

byte[] encrypted = cipher.doFinal(message.getBytes());

return byteArrayToHex(encrypted);

}

}


private static String byteArrayToHex(byte[] encrypted) {

if (encrypted == null || encrypted.length == 0) {

return null;

}

StringBuffer sb = new StringBuffer(encrypted.length * 2);

String hexNumber;

for (int x = 0; x < encrypted.length; x++) {

hexNumber = "0" + Integer.toHexString(0xff & encrypted[x]);

sb.append(hexNumber.substring(hexNumber.length() - 2));

}

return sb.toString();

}


public static String decrypt(String encrypted) throws Exception {

if (encrypted == null) {

return null;

} else {

SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

byte[] original = cipher.doFinal(hexToByteArray(encrypted));

String originalStr = new String(original);

return originalStr;

}

}


private static byte[] hexToByteArray(String hex) {

if (hex == null || hex.length() == 0) {

return null;

}

// 16진수 문자열을 byte로 변환

byte[] byteArray = new byte[hex.length() / 2];

for (int i = 0; i < byteArray.length; i++) {

byteArray[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);

}

return byteArray;

}

}



* MD5 *******************************************************************************************************


import java.security.MessageDigest;


public class Md5 {


public static void main(String[] args) throws Exception {

Md5 md5 = new Md5();

System.out.println(md5.getEncMD5("12345"));

}


public String getEncMD5(String str) {

   String MD5 = "";

   try {

       MessageDigest md = MessageDigest.getInstance("MD5");

       md.update(str.getBytes());

       byte byteData[] = md.digest();

       StringBuffer sb = new StringBuffer();

       for (int i = 0; i < byteData.length; i++) {

           sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));

       }

       MD5 = sb.toString();

 

   } catch (Exception e) {

       e.getMessage();

       MD5 = null;

   }

   return MD5;

}


}



* MySQL ******************************************************************************************************


SELECT MD5('test6')

union all

SELECT MD5('12345');


SELECT LOWER( HEX(AES_ENCRYPT('test6', '****************')) )

union all

SELECT LOWER( HEX(AES_ENCRYPT('12345', '****************')) );



[java] md5 aes128 mysql 암호화 적용