package kr.co.udapsoft.common.util; import kr.co.hsnc.common.config.WAFConfig; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import javax.crypto.spec.SecretKeySpec; import javax.crypto.Cipher; public class Encrypt { /** * 암호화,복호화, 단방향 암호화 MD5 */ public String MD5(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(NoSuchAlgorithmException e){ e.printStackTrace(); MD5 = null; } return MD5; } public String SHA256(String str){ String SHA = ""; try{ MessageDigest sh = MessageDigest.getInstance("SHA-256"); sh.update(str.getBytes()); byte byteData[] = sh.digest(); StringBuffer sb = new StringBuffer(); for(int i = 0 ; i < byteData.length ; i++){ sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1)); } SHA = sb.toString(); }catch(NoSuchAlgorithmException e){ e.printStackTrace(); SHA = null; } return SHA; } public String encrypt(String message) throws Exception{ if(message == null){ return null; }else{ String sKey = WAFConfig.get("waf.key.encrypt"); SecretKeySpec secretKeySpec = new SecretKeySpec(sKey.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encrypted = cipher.doFinal(message.getBytes()); //System.out.println(encrypted); 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