C#实现标准JAVA的Md5withRsa算法

确认可行的方法
 public static String pfxpath = @"E:\xxx.pfx";
        public static String cerpath = @"E:\xxxx.cer";
        public static String pfxpassword = "111111"; 

        /// 
        /// 签名
        /// 
        /// xml数据
        /// 签名数据
        public static String signData(String xml) 
        {
            Console.WriteLine("加载数字证书私钥:"+ pfxpath + "证书密码:" +  pfxpassword); 
            //签名 
            Console.WriteLine("--------开始使用私钥创建例子"); 
            Console.WriteLine("--------需要签名的:" + xml); 
           // X509Certificate2 objx5092 = new X509Certificate2(pfxpath, pfxpassword);   //当前用户存储,本地测试
            X509Certificate2 objx5092 = new X509Certificate2(pfxpath, pfxpassword,X509KeyStorageFlags.MachineKeySet);   //本地存储,服务器测试(windows server2008)要使用这个

            RSACryptoServiceProvider rsa = objx5092.PrivateKey as RSACryptoServiceProvider;   
            byte[] data = Encoding.GetEncoding("GBK").GetBytes(xml); 
            byte[] hashValue = rsa.SignData(data, "MD5");
            string msg = Convert.ToBase64String(hashValue);          
            Console.WriteLine("--------得到:" + msg);

            return msg;
        }

        /// 
        /// 解签 
        /// 
        /// 解签数据
        /// 是否成功
        public  bool verifyData(String msg, String check)
        {
            byte[] msgByte = System.Convert.FromBase64String(msg);
            byte[] checkByte = System.Convert.FromBase64String(check);
            X509Certificate2 pub = new X509Certificate2(cerpath);
            RSACryptoServiceProvider rsaPublic = pub.PublicKey.Key as RSACryptoServiceProvider;
            return rsaPublic.VerifyData(msgByte, "MD5", checkByte);
        }

此方式等同于JAVA mad5withrsa

你可能感兴趣的:(加密解密)