C#標準JAVAを実現するMd 5 withRsaアルゴリズム

1919 ワード

       
 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 mad 5 withrsaに等しい