日期:2014-05-20  浏览次数:20911 次

加到300分,求在JAVA下的“PBEWithMD5AndDES”加密算法,到C#下如何实现?(二)

加到300分

输入字串:这是一个测试
输出字串:09IdjpQQAPgniEaO/skp9gy8+LkD42fZ

输入字串:123456
输出字串:OEnU8DEcgsE=

以下是JAVA的源代码,想求一下在C#使用哪种函数能实现同样的加密算法。

一、http://topic.csdn.net/u/20120212/20/077d891b-d861-417d-8df0-aa973eb46bbf.html

二、本帖

三、


Java code


//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);
        }

}






------解决方案--------------------
有空帮你写一把,先顶一下呗
------解决方案--------------------