C#实现RSA加密与解密、签名与认证

一、RSA简介

RSA公钥加密算法是1977年由Ron Rivest、Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的。RSA取名来自开发他们三者的名字。RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的所有密码攻击,已被ISO推荐为公钥数据加密标准。RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。
RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一。RSA的安全性依赖于大数的因子分解,但并没有从理论上证明破译RSA的难度与大数分解难度等价。即RSA的重大缺陷是无法从理论上把握它的保密性能如何,而且密码学界多数人士倾向于因子分解不是NPC问题。
RSA的缺点主要有:
A)产生密钥很麻烦,受到素数产生技术的限制,因而难以做到一次一密。

B)分组长度太大,为保证安全性,n 至少也要 600bits以上,使运算代价很高,尤其是速度较慢,较对称密码算法慢几个数量级;且随着大数分解技术的发展,这个长度还在增加,不利于数据格式的标准化。目前,SET(Secure Electronic Transaction)协议中要求CA采用2048bits长的密钥,其他实体使用1024比特的密钥。

C)RSA密钥长度随着保密级别提高,增加很快。下表列出了对同一安全级别所对应的密钥长度。

这种算法1978年就出现了,它是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。算法的名字以发明者的名字命名:Ron Rivest, 
AdiShamir 和Leonard Adleman。早在1973年,英国国家通信总局的数学家Clifford Cocks就发现了类似的算法。但是他的发现被列为绝密,直到1998年才公诸于世。
RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密。
RSA的算法涉及三个参数,n、e1、e2。
其中,n是两个大质数p、q的积,n的二进制表示时所占用的位数,就是所谓的密钥长度。
e1和e2是一对相关的值,e1可以任意取,但要求e1与(p-1)*(q-1)互质;再选择e2,要求(e2*e1)mod((p-1)*(q-1))=1。
(n及e1),(n及e2)就是密钥对。
RSA加解密的算法完全相同,设A为明文,B为密文,则:A=B^e1 mod n;B=A^e2 mod n;
e1和e2可以互换使用,即:

A=B^e2 mod n;B=A^e1 mod n;


二、MD5加密介绍

参考:http://blog.csdn.net/wonsoft/article/details/5913572
MD5的全称是message-digest algorithm 5(信息-摘要算法,在90年代初由mit laboratory for computer science和rsa data security inc的ronald l. rivest开发出来, 经md2、md3和md4发展而来。
MD5具有很好的安全性(因为它具有不可逆的特征,加过密的密文经过解密后和加密前的东东相同的可能性极小)
[csharp]  view plain  copy
  1. public string GetStrMd5(string ConvertString)  
  2.        {  
  3.            string strBodyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(ConvertString));  
  4.            string t2=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strBodyBase64, "MD5").ToUpper();  
  5.            return t2;  
  6.        }  

三、C#对PKCS#8编码的RSA私钥进行签名

 对MD5加密后的长度为32的密文进行PKCS8的RSA签名的方法:
[html]  view plain  copy
  1. /// <summary>  
  2.         /// 对MD5加密后的长度为32的密文进行签名  
  3.         /// </summary>  
  4.         /// <param name="strPrivateKey">私钥</param>  
  5.         /// <param name="strContent">MD5加密后的密文</param>  
  6.         /// <returns></returns>  
  7.         public string SignatureFormatter(string strPrivateKey, string strContent)  
  8.         {  
  9.             byte[] btContent = Encoding.UTF8.GetBytes(strContent);  
  10.             byte[] hv = MD5.Create().ComputeHash(btContent);  
  11.             RSACryptoServiceProvider rsp = new RSACryptoServiceProvider();  
  12.             rsp.FromXmlString(strPrivateKey);  
  13.             RSAPKCS1SignatureFormatter rf = new RSAPKCS1SignatureFormatter(rsp);  
  14.             rf.SetHashAlgorithm("MD5");  
  15.             byte[] signature = rf.CreateSignature(hv);  
  16.             return Convert.ToBase64String(signature);  
  17.         }  

四、C#实现RSA加密与解密、签名与认证常用方法

1.RSA加密解密:
 (1)获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥 (2)加密 (3)解密
2.RSA签名和验证
 (1)获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥 (2)获取待签名的Hash码 (3)获取签名的字符串 (4)验证
3.公钥与私钥的理解:
 (1)私钥用来进行解密和签名,是给自己用的。
 (2)公钥由本人公开,用于加密和验证签名,是给别人用的。
    (3)当该用户发送文件时,用私钥签名,别人用他给的公钥验证签名,可以保证该信息是由他发送的。当该用户接受文件时,别人用他的公钥加密,他用私钥解密,可以保证该信息只能由他接收到。
[csharp]  view plain  copy
  1. using System.Security.Cryptography;  
  2. class RSACryption  
  3. {          
  4.     #region RSA 加密解密  
  5.     #region RSA 的密钥产生  
  6.     /// <summary>  
  7.     /// RSA产生密钥  
  8.     /// </summary>  
  9.     /// <param name="xmlKeys">私钥</param>  
  10.     /// <param name="xmlPublicKey">公钥</param>  
  11.     public void RSAKey(out string xmlKeys, out string xmlPublicKey)  
  12.     {  
  13.         try  
  14.         {  
  15.             System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
  16.             xmlKeys = rsa.ToXmlString(true);  
  17.             xmlPublicKey = rsa.ToXmlString(false);  
  18.         }  
  19.         catch (Exception ex)  
  20.         {  
  21.             throw ex;  
  22.         }  
  23.     }  
  24.     #endregion  
  25.  
  26.     #region RSA加密函数  
  27.     //##############################################################################   
  28.     //RSA 方式加密   
  29.     //KEY必须是XML的形式,返回的是字符串   
  30.     //该加密方式有长度限制的!  
  31.     //##############################################################################   
  32.          
  33.     /// <summary>  
  34.     /// RSA的加密函数  
  35.     /// </summary>  
  36.     /// <param name="xmlPublicKey">公钥</param>  
  37.     /// <param name="encryptString">待加密的字符串</param>  
  38.     /// <returns></returns>  
  39.     public string RSAEncrypt(string xmlPublicKey, string encryptString)  
  40.     {  
  41.         try  
  42.         {  
  43.             byte[] PlainTextBArray;  
  44.             byte[] CypherTextBArray;  
  45.             string Result;  
  46.             System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
  47.             rsa.FromXmlString(xmlPublicKey);  
  48.             PlainTextBArray = (new UnicodeEncoding()).GetBytes(encryptString);  
  49.             CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);  
  50.             Result = Convert.ToBase64String(CypherTextBArray);  
  51.             return Result;  
  52.         }  
  53.         catch (Exception ex)  
  54.         {  
  55.             throw ex;  
  56.         }  
  57.     }          
  58.     /// <summary>  
  59.     /// RSA的加密函数   
  60.     /// </summary>  
  61.     /// <param name="xmlPublicKey">公钥</param>  
  62.     /// <param name="EncryptString">待加密的字节数组</param>  
  63.     /// <returns></returns>  
  64.     public string RSAEncrypt(string xmlPublicKey, byte[] EncryptString)  
  65.     {  
  66.         try  
  67.         {  
  68.             byte[] CypherTextBArray;  
  69.             string Result;  
  70.             System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
  71.             rsa.FromXmlString(xmlPublicKey);  
  72.             CypherTextBArray = rsa.Encrypt(EncryptString, false);  
  73.             Result = Convert.ToBase64String(CypherTextBArray);  
  74.             return Result;  
  75.         }  
  76.         catch (Exception ex)  
  77.         {  
  78.             throw ex;  
  79.         }  
  80.     }  
  81.     #endregion  
  82.  
  83.     #region RSA的解密函数          
  84.     /// <summary>  
  85.     /// RSA的解密函数  
  86.     /// </summary>  
  87.     /// <param name="xmlPrivateKey">私钥</param>  
  88.     /// <param name="decryptString">待解密的字符串</param>  
  89.     /// <returns></returns>  
  90.     public string RSADecrypt(string xmlPrivateKey, string decryptString)  
  91.     {  
  92.         try  
  93.         {  
  94.             byte[] PlainTextBArray;  
  95.             byte[] DypherTextBArray;  
  96.             string Result;  
  97.             System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
  98.             rsa.FromXmlString(xmlPrivateKey);  
  99.             PlainTextBArray = Convert.FromBase64String(decryptString);  
  100.             DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);  
  101.             Result = (new UnicodeEncoding()).GetString(DypherTextBArray);  
  102.             return Result;  
  103.         }  
  104.         catch (Exception ex)  
  105.         {  
  106.             throw ex;  
  107.         }  
  108.     }          
  109.     /// <summary>  
  110.     /// RSA的解密函数   
  111.     /// </summary>  
  112.     /// <param name="xmlPrivateKey">私钥</param>  
  113.     /// <param name="DecryptString">待解密的字节数组</param>  
  114.     /// <returns></returns>  
  115.     public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)  
  116.     {  
  117.         try  
  118.         {  
  119.             byte[] DypherTextBArray;  
  120.             string Result;  
  121.             System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
  122.             rsa.FromXmlString(xmlPrivateKey);  
  123.             DypherTextBArray = rsa.Decrypt(DecryptString, false);  
  124.             Result = (new UnicodeEncoding()).GetString(DypherTextBArray);  
  125.             return Result;  
  126.         }  
  127.         catch (Exception ex)  
  128.         {  
  129.             throw ex;  
  130.         }  
  131.     }  
  132.     #endregion  
  133.     #endregion  
  134.  
  135.     #region RSA数字签名  
  136.     #region 获取Hash描述表          
  137.     /// <summary>  
  138.     /// 获取Hash描述表  
  139.     /// </summary>  
  140.     /// <param name="strSource">待签名的字符串</param>  
  141.     /// <param name="HashData">Hash描述</param>  
  142.     /// <returns></returns>  
  143.     public bool GetHash(string strSource, ref byte[] HashData)  
  144.     {  
  145.         try  
  146.         {                 
  147.             byte[] Buffer;  
  148.             System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");  
  149.             Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(strSource);  
  150.             HashData = MD5.ComputeHash(Buffer);  
  151.             return true;  
  152.         }  
  153.         catch (Exception ex)  
  154.         {  
  155.             throw ex;  
  156.         }  
  157.     }  
  158.   
  159.     /// <summary>  
  160.     /// 获取Hash描述表  
  161.     /// </summary>  
  162.     /// <param name="strSource">待签名的字符串</param>  
  163.     /// <param name="strHashData">Hash描述</param>  
  164.     /// <returns></returns>  
  165.     public bool GetHash(string strSource, ref string strHashData)  
  166.     {  
  167.         try  
  168.         {  
  169.             //从字符串中取得Hash描述   
  170.             byte[] Buffer;  
  171.             byte[] HashData;  
  172.             System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");  
  173.             Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(strSource);  
  174.             HashData = MD5.ComputeHash(Buffer);  
  175.             strHashData = Convert.ToBase64String(HashData);  
  176.             return true;  
  177.         }  
  178.         catch (Exception ex)  
  179.         {  
  180.             throw ex;  
  181.         }  
  182.     }  
  183.   
  184.     /// <summary>  
  185.     /// 获取Hash描述表  
  186.     /// </summary>  
  187.     /// <param name="objFile">待签名的文件</param>  
  188.     /// <param name="HashData">Hash描述</param>  
  189.     /// <returns></returns>  
  190.     public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)  
  191.     {  
  192.         try  
  193.         {  
  194.             //从文件中取得Hash描述   
  195.             System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");  
  196.             HashData = MD5.ComputeHash(objFile);  
  197.             objFile.Close();  
  198.             return true;  
  199.         }  
  200.         catch (Exception ex)  
  201.         {  
  202.             throw ex;  
  203.         }  
  204.     }  
  205.   
  206.     /// <summary>  
  207.     /// 获取Hash描述表  
  208.     /// </summary>  
  209.     /// <param name="objFile">待签名的文件</param>  
  210.     /// <param name="strHashData">Hash描述</param>  
  211.     /// <returns></returns>  
  212.     public bool GetHash(System.IO.FileStream objFile, ref string strHashData)  
  213.     {  
  214.         try  
  215.         {  
  216.             //从文件中取得Hash描述   
  217.             byte[] HashData;  
  218.             System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");  
  219.             HashData = MD5.ComputeHash(objFile);  
  220.             objFile.Close();  
  221.             strHashData = Convert.ToBase64String(HashData);  
  222.             return true;  
  223.         }  
  224.         catch (Exception ex)  
  225.         {  
  226.             throw ex;  
  227.         }  
  228.     }  
  229.     #endregion  
  230.  
  231.     #region RSA签名  
  232.     /// <summary>  
  233.     /// RSA签名  
  234.     /// </summary>  
  235.     /// <param name="strKeyPrivate">私钥</param>  
  236.     /// <param name="HashbyteSignature">待签名Hash描述</param>  
  237.     /// <param name="EncryptedSignatureData">签名后的结果</param>  
  238.     /// <returns></returns>  
  239.     public bool SignatureFormatter(string strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)  
  240.     {  
  241.         try  
  242.         {  
  243.             System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();  
  244.   
  245.             RSA.FromXmlString(strKeyPrivate);  
  246.             System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter  
  247.   
  248. (RSA);  
  249.             //设置签名的算法为MD5   
  250.             RSAFormatter.SetHashAlgorithm("MD5");  
  251.             //执行签名   
  252.             EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);  
  253.             return true;  
  254.         }  
  255.         catch (Exception ex)  
  256.         {  
  257.             throw ex;  
  258.         }  
  259.     }  
  260.   
  261.     /// <summary>  
  262.     /// RSA签名  
  263.     /// </summary>  
  264.     /// <param name="strKeyPrivate">私钥</param>  
  265.     /// <param name="HashbyteSignature">待签名Hash描述</param>  
  266.     /// <param name="m_strEncryptedSignatureData">签名后的结果</param>  
  267.     /// <returns></returns>  
  268.     public bool SignatureFormatter(string strKeyPrivate, byte[] HashbyteSignature, ref string strEncryptedSignatureData)  
  269.     {  
  270.         try  
  271.         {  
  272.             byte[] EncryptedSignatureData;  
  273.             System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();  
  274.             RSA.FromXmlString(strKeyPrivate);  
  275.             System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter  
  276.   
  277. (RSA);  
  278.             //设置签名的算法为MD5   
  279.             RSAFormatter.SetHashAlgorithm("MD5");  
  280.             //执行签名   
  281.             EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);  
  282.             strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);  
  283.             return true;  
  284.         }  
  285.         catch (Exception ex)  
  286.         {  
  287.             throw ex;  
  288.         }  
  289.     }  
  290.   
  291.     /// <summary>  
  292.     /// RSA签名  
  293.     /// </summary>  
  294.     /// <param name="strKeyPrivate">私钥</param>  
  295.     /// <param name="strHashbyteSignature">待签名Hash描述</param>  
  296.     /// <param name="EncryptedSignatureData">签名后的结果</param>  
  297.     /// <returns></returns>  
  298.     public bool SignatureFormatter(string strKeyPrivate, string strHashbyteSignature, ref byte[] EncryptedSignatureData)  
  299.     {  
  300.         try  
  301.         {  
  302.             byte[] HashbyteSignature;  
  303.   
  304.             HashbyteSignature = Convert.FromBase64String(strHashbyteSignature);  
  305.             System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();  
  306.   
  307.   
  308.             RSA.FromXmlString(strKeyPrivate);  
  309.             System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter  
  310.   
  311. (RSA);  
  312.             //设置签名的算法为MD5   
  313.             RSAFormatter.SetHashAlgorithm("MD5");  
  314.             //执行签名   
  315.             EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);  
  316.   
  317.             return true;  
  318.         }  
  319.         catch (Exception ex)  
  320.         {  
  321.             throw ex;  
  322.         }  
  323.     }  
  324.   
  325.     /// <summary>  
  326.     /// RSA签名  
  327.     /// </summary>  
  328.     /// <param name="strKeyPrivate">私钥</param>  
  329.     /// <param name="strHashbyteSignature">待签名Hash描述</param>  
  330.     /// <param name="strEncryptedSignatureData">签名后的结果</param>  
  331.     /// <returns></returns>  
  332.     public bool SignatureFormatter(string strKeyPrivate, string strHashbyteSignature, ref string strEncryptedSignatureData)  
  333.     {  
  334.         try  
  335.         {  
  336.             byte[] HashbyteSignature;  
  337.             byte[] EncryptedSignatureData;  
  338.             HashbyteSignature = Convert.FromBase64String(strHashbyteSignature);  
  339.             System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();  
  340.             RSA.FromXmlString(strKeyPrivate);  
  341.             System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter  
  342.   
  343. (RSA);  
  344.             //设置签名的算法为MD5   
  345.             RSAFormatter.SetHashAlgorithm("MD5");  
  346.             //执行签名   
  347.             EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);  
  348.             strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);  
  349.             return true;  
  350.         }  
  351.         catch (Exception ex)  
  352.         {  
  353.             throw ex;  
  354.         }  
  355.     }  
  356.     #endregion  
  357.  
  358.     #region RSA 签名验证  
  359.     /// <summary>  
  360.     /// RSA签名验证  
  361.     /// </summary>  
  362.     /// <param name="strKeyPublic">公钥</param>  
  363.     /// <param name="HashbyteDeformatter">Hash描述</param>  
  364.     /// <param name="DeformatterData">签名后的结果</param>  
  365.     /// <returns></returns>  
  366.     public bool SignatureDeformatter(string strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)  
  367.     {  
  368.         try  
  369.         {  
  370.             System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();  
  371.             RSA.FromXmlString(strKeyPublic);  
  372.             System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new   
  373.   
  374. System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);  
  375.             //指定解密的时候HASH算法为MD5   
  376.             RSADeformatter.SetHashAlgorithm("MD5");  
  377.             if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))  
  378.             {  
  379.                 return true;  
  380.             }  
  381.             else  
  382.             {  
  383.                 return false;  
  384.             }  
  385.         }  
  386.         catch (Exception ex)  
  387.         {  
  388.             throw ex;  
  389.         }  
  390.     }  
  391.     /// <summary>  
  392.     /// RSA签名验证  
  393.     /// </summary>  
  394.     /// <param name="strKeyPublic">公钥</param>  
  395.     /// <param name="strHashbyteDeformatter">Hash描述</param>  
  396.     /// <param name="DeformatterData">签名后的结果</param>  
  397.     /// <returns></returns>  
  398.     public bool SignatureDeformatter(string strKeyPublic, string strHashbyteDeformatter, byte[] DeformatterData)  
  399.     {  
  400.         try  
  401.         {  
  402.             byte[] HashbyteDeformatter;  
  403.             HashbyteDeformatter = Convert.FromBase64String(strHashbyteDeformatter);  
  404.             System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();  
  405.             RSA.FromXmlString(strKeyPublic);  
  406.             System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new   
  407.   
  408. System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);  
  409.             //指定解密的时候HASH算法为MD5   
  410.             RSADeformatter.SetHashAlgorithm("MD5");  
  411.             if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))  
  412.             {  
  413.                 return true;  
  414.             }  
  415.             else  
  416.             {  
  417.                 return false;  
  418.             }  
  419.         }  
  420.         catch (Exception ex)  
  421.         {  
  422.             throw ex;  
  423.         }  
  424.     }  
  425.     /// <summary>  
  426.     /// RSA签名验证  
  427.     /// </summary>  
  428.     /// <param name="strKeyPublic">公钥</param>  
  429.     /// <param name="HashbyteDeformatter">Hash描述</param>  
  430.     /// <param name="strDeformatterData">签名后的结果</param>  
  431.     /// <returns></returns>  
  432.     public bool SignatureDeformatter(string strKeyPublic, byte[] HashbyteDeformatter, string strDeformatterData)  
  433.     {  
  434.         try  
  435.         {  
  436.             byte[] DeformatterData;  
  437.             System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();  
  438.             RSA.FromXmlString(strKeyPublic);  
  439.             System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new   
  440.   
  441. System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);  
  442.             //指定解密的时候HASH算法为MD5   
  443.             RSADeformatter.SetHashAlgorithm("MD5");  
  444.             DeformatterData = Convert.FromBase64String(strDeformatterData);  
  445.             if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))  
  446.             {  
  447.                 return true;  
  448.             }  
  449.             else  
  450.             {  
  451.                 return false;  
  452.             }  
  453.         }  
  454.         catch (Exception ex)  
  455.         {  
  456.             throw ex;  
  457.         }  
  458.     }  
  459.     /// <summary>  
  460.     /// RSA签名验证  
  461.     /// </summary>  
  462.     /// <param name="strKeyPublic">公钥</param>  
  463.     /// <param name="strHashbyteDeformatter">Hash描述</param>  
  464.     /// <param name="strDeformatterData">签名后的结果</param>  
  465.     /// <returns></returns>  
  466.     public bool SignatureDeformatter(string strKeyPublic, string strHashbyteDeformatter, string strDeformatterData)  
  467.     {  
  468.         try  
  469.         {  
  470.             byte[] DeformatterData;  
  471.             byte[] HashbyteDeformatter;  
  472.             HashbyteDeformatter = Convert.FromBase64String(strHashbyteDeformatter);  
  473.             System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();  
  474.             RSA.FromXmlString(strKeyPublic);  
  475.             System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new   
  476.   
  477. System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);  
  478.             //指定解密的时候HASH算法为MD5   
  479.             RSADeformatter.SetHashAlgorithm("MD5");  
  480.             DeformatterData = Convert.FromBase64String(strDeformatterData);  
  481.             if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))  
  482.             {  
  483.                 return true;  
  484.             }  
  485.             else  
  486.             {  
  487.                 return false;  
  488.             }  
  489.         }  
  490.         catch (Exception ex)  
  491.         {  
  492.             throw ex;  
  493.         }  
  494.     }  
  495.     #endregion  
  496.     #endregion   
  497. }  

参考网站:

http://www.cnblogs.com/linzheng/archive/2011/02/20/1959123.html
http://www.cnblogs.com/sydeveloper/archive/2012/08/11/2633624.html

http://codego.net/126956/

RSA公钥密钥生成网站:http://www.bm8.com.cn/webtool/rsa/

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值