日期:2014-05-20 浏览次数:20911 次
//07 import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; //09 import java.security.MessageDigest; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec; //BASE64 import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class pubwin2009 { // key1 姓名身份证加解密 07/09 public static String Rcode(String message,int type)throws Exception { //String Key="123!@#qweQWE/.,?><abc"; //07 String Key="q1!2@3#we>WacE/.Q,?<b"; //09 byte _fldint[] = { -87, -101, -56, 50, 86, 53, -29, 3}; byte bytesrc[]; if (type==1) bytesrc =message.getBytes("UTF8"); else bytesrc =(new BASE64Decoder()).decodeBuffer(message); PBEParameterSpec pbeparameterspec = new PBEParameterSpec(_fldint,19); PBEKeySpec pbekeyspec = new PBEKeySpec(Key.toCharArray()); SecretKeyFactory secretkeyfactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); javax.crypto.SecretKey secretkey = secretkeyfactory.generateSecret(pbekeyspec); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(type, secretkey, pbeparameterspec); byte[] retByte = cipher.doFinal(bytesrc); if (type==1) return new BASE64Encoder().encode(retByte); else return new String(retByte,"UTF8"); } //09会员密码 db.properties 加解密 public static String decrypt(String message,int type) throws Exception { String keye = "hi%$so78"; //MD5 String keyb = "12up56^&"; //IvParameterSpec byte bytesrc[]; if (type==1) bytesrc =message.getBytes(); else bytesrc =(new BASE64Decoder()).decodeBuffer(message); MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(keye.getBytes()); DESKeySpec desKeySpec = new DESKeySpec(md5.digest()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(keyb.getBytes("UTF-8")); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(type, secretKey, iv); byte[] retByte = cipher.doFinal(bytesrc); if (type==1) return new BASE64Encoder().encode(retByte); else return new String(retByte); } public static void main(String args[]) throws Exception { System.out.println( "\n姓名身份证加密:这是一个测试 -> " +Rcode("这是一个测试",1)); System.out.println( "姓名身份证解密:09IdjpQQAPgniEaO/skp9gy8+LkD42fZ -> " +Rcode("09IdjpQQAPgniEaO/skp9gy8+LkD42fZ",2)+"\n"); System.out.println( "09会员密码加解:123456 -> " +decrypt("123456",1)); System.out.println( "09会员密码解密:OEnU8DEcgsE= -> " +decrypt("OEnU8DEcgsE=",2)); System.exit(0); } }