Encrypt.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package kr.co.udapsoft.common.util;
  2. import kr.co.hsnc.common.config.WAFConfig;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import javax.crypto.spec.SecretKeySpec;
  6. import javax.crypto.Cipher;
  7. public class Encrypt
  8. {
  9. /**
  10. * 암호화,복호화, 단방향 암호화 MD5
  11. */
  12. public String MD5(String str){
  13. String MD5 = "";
  14. try{
  15. MessageDigest md = MessageDigest.getInstance("MD5");
  16. md.update(str.getBytes());
  17. byte byteData[] = md.digest();
  18. StringBuffer sb = new StringBuffer();
  19. for(int i = 0 ; i < byteData.length ; i++){
  20. sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
  21. }
  22. MD5 = sb.toString();
  23. }catch(NoSuchAlgorithmException e){
  24. e.printStackTrace();
  25. MD5 = null;
  26. }
  27. return MD5;
  28. }
  29. public String SHA256(String str){
  30. String SHA = "";
  31. try{
  32. MessageDigest sh = MessageDigest.getInstance("SHA-256");
  33. sh.update(str.getBytes());
  34. byte byteData[] = sh.digest();
  35. StringBuffer sb = new StringBuffer();
  36. for(int i = 0 ; i < byteData.length ; i++){
  37. sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
  38. }
  39. SHA = sb.toString();
  40. }catch(NoSuchAlgorithmException e){
  41. e.printStackTrace();
  42. SHA = null;
  43. }
  44. return SHA;
  45. }
  46. public String encrypt(String message) throws Exception{
  47. if(message == null){
  48. return null;
  49. }else{
  50. String sKey = WAFConfig.get("waf.key.encrypt");
  51. SecretKeySpec secretKeySpec = new SecretKeySpec(sKey.getBytes(), "AES");
  52. Cipher cipher = Cipher.getInstance("AES");
  53. cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
  54. byte[] encrypted = cipher.doFinal(message.getBytes());
  55. //System.out.println(encrypted);
  56. return byteArrayToHex(encrypted);
  57. }
  58. }
  59. private static String byteArrayToHex(byte[] encrypted) {
  60. if(encrypted == null || encrypted.length ==0){
  61. return null;
  62. }
  63. StringBuffer sb = new StringBuffer(encrypted.length * 2);
  64. String hexNumber;
  65. for(int x=0; x<encrypted.length; x++){
  66. System.out.println( encrypted[x]);
  67. hexNumber = "0" + Integer.toHexString(0xff & encrypted[x]);
  68. //System.out.println(hexNumber);
  69. sb.append(hexNumber.substring(hexNumber.length() - 2));
  70. }
  71. //System.out.println(sb);
  72. return sb.toString();
  73. }
  74. public String decrypt(String encrypted) throws Exception{
  75. if(encrypted == null){
  76. return null;
  77. }else{
  78. String sKey = WAFConfig.get("waf.key.encrypt");
  79. SecretKeySpec secretKeySpec = new SecretKeySpec(sKey.getBytes(), "AES");
  80. Cipher cipher = Cipher.getInstance("AES");
  81. cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
  82. byte[] original = cipher.doFinal(hexToByteArray(encrypted));
  83. String originalStr = new String(original);
  84. return originalStr;
  85. }
  86. }
  87. private static byte[] hexToByteArray(String hex) {
  88. if(hex == null || hex.length() == 0){
  89. return null;
  90. }
  91. //16진수 문자열을 byte로 변환
  92. byte[] byteArray = new byte[hex.length() /2 ];
  93. for(int i=0; i<byteArray.length; i++){
  94. byteArray[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2*i+2), 16);
  95. }
  96. return byteArray;
  97. }
  98. }