package com.ectrip.cyt.utils; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; /** * 基础加密算法类?当前支持des,md5码? * * @author huhaopeng */ public class EncryptUtil { /** * MD5值计码?p> * MD5的算法在RFC1321 中定码? * 在RFC 1321中,给出了Test suite用来码?码码你的实现是否正确码? * MD5 ("") = d41d8cd98f00b204e9800998ecf8427e * MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661 * MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72 * MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0 * MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b * @param str 源字符串 * @return md5码? */ public final static byte[] md5(String str) { try { byte[] res = str.getBytes("UTF-8"); MessageDigest mdTemp = MessageDigest.getInstance("MD5".toUpperCase()); mdTemp.update(res); byte[] hash = mdTemp.digest(); return hash; } catch (Exception e) { return null; } } //hex repr. of md5 public final static String MD5Hex(String input) { String s = null; char hexDigits[] = { // 用来将字节转换成 16 进制表示的字码? '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; try { MessageDigest md = MessageDigest .getInstance("MD5"); md.update(input.getBytes("utf-8")); byte tmp[] = md.digest(); char str[] = new char[16 * 2]; int k = 0; for (int i = 0; i < 16; i++) { byte byte0 = tmp[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } s = new String(str); } catch (Exception e) { e.printStackTrace(); } return s; } // 加密后解码? public static String JM(byte[] inStr) { String newStr = new String(inStr); char[] a = newStr.toCharArray(); for (int i = 0; i < a.length; i++) { a[i] = (char) (a[i] ^ 't'); } String k = new String(a); return k; } /** * @param key 24位密码? * @param str 源字符串 * @return * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws UnsupportedEncodingException * @throws InvalidKeySpecException * @throws IllegalBlockSizeException * @throws BadPaddingException */ public static byte[] des3Encrypt(String key, String str) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException { byte[] newkey = key.getBytes(); SecureRandom sr = new SecureRandom(); DESedeKeySpec dks = new DESedeKeySpec(newkey); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); byte[] bt = cipher.doFinal(str.getBytes("utf-8")); return bt; } public final static String taoBaoMD5(String s) { char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; try { byte[] strTemp = s.getBytes("GBK"); MessageDigest mdTemp = MessageDigest.getInstance("MD5"); mdTemp.update(strTemp); byte[] md = mdTemp.digest(); int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { return null; } } }