RSACryption.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using System;
  2. using System.Text;
  3. using System.Security.Cryptography;
  4. namespace Ant.Service.Utilities
  5. {
  6. /// <summary>
  7. /// RSA加密解密及RSA签名和验证
  8. /// </summary>
  9. public class RSACryption
  10. {
  11. public RSACryption()
  12. {
  13. }
  14. #region RSA 加密解密
  15. #region RSA 的密钥产生
  16. /// <summary>
  17. /// RSA 的密钥产生 产生私钥 和公钥
  18. /// </summary>
  19. /// <param name="xmlKeys"></param>
  20. /// <param name="xmlPublicKey"></param>
  21. public void RSAKey(out string xmlKeys,out string xmlPublicKey)
  22. {
  23. System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  24. xmlKeys=rsa.ToXmlString(true);
  25. xmlPublicKey = rsa.ToXmlString(false);
  26. }
  27. #endregion
  28. #region RSA的加密函数
  29. //##############################################################################
  30. //RSA 方式加密
  31. //说明KEY必须是XML的行式,返回的是字符串
  32. //在有一点需要说明!!该加密方式有 长度 限制的!!
  33. //##############################################################################
  34. //RSA的加密函数 string
  35. public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString )
  36. {
  37. byte[] PlainTextBArray;
  38. byte[] CypherTextBArray;
  39. string Result;
  40. RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  41. rsa.FromXmlString(xmlPublicKey);
  42. PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);
  43. CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
  44. Result=Convert.ToBase64String(CypherTextBArray);
  45. return Result;
  46. }
  47. //RSA的加密函数 byte[]
  48. public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString )
  49. {
  50. byte[] CypherTextBArray;
  51. string Result;
  52. RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  53. rsa.FromXmlString(xmlPublicKey);
  54. CypherTextBArray = rsa.Encrypt(EncryptString, false);
  55. Result=Convert.ToBase64String(CypherTextBArray);
  56. return Result;
  57. }
  58. #endregion
  59. #region RSA的解密函数
  60. //RSA的解密函数 string
  61. public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString )
  62. {
  63. byte[] PlainTextBArray;
  64. byte[] DypherTextBArray;
  65. string Result;
  66. System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  67. rsa.FromXmlString(xmlPrivateKey);
  68. PlainTextBArray =Convert.FromBase64String(m_strDecryptString);
  69. DypherTextBArray=rsa.Decrypt(PlainTextBArray, false);
  70. Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
  71. return Result;
  72. }
  73. //RSA的解密函数 byte
  74. public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString )
  75. {
  76. byte[] DypherTextBArray;
  77. string Result;
  78. System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  79. rsa.FromXmlString(xmlPrivateKey);
  80. DypherTextBArray=rsa.Decrypt(DecryptString, false);
  81. Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
  82. return Result;
  83. }
  84. #endregion
  85. #endregion
  86. #region RSA数字签名
  87. #region 获取Hash描述表
  88. //获取Hash描述表
  89. public bool GetHash(string m_strSource, ref byte[] HashData)
  90. {
  91. //从字符串中取得Hash描述
  92. byte[] Buffer;
  93. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  94. Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
  95. HashData = MD5.ComputeHash(Buffer);
  96. return true;
  97. }
  98. //获取Hash描述表
  99. public bool GetHash(string m_strSource, ref string strHashData)
  100. {
  101. //从字符串中取得Hash描述
  102. byte[] Buffer;
  103. byte[] HashData;
  104. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  105. Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
  106. HashData = MD5.ComputeHash(Buffer);
  107. strHashData = Convert.ToBase64String(HashData);
  108. return true;
  109. }
  110. //获取Hash描述表
  111. public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)
  112. {
  113. //从文件中取得Hash描述
  114. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  115. HashData = MD5.ComputeHash(objFile);
  116. objFile.Close();
  117. return true;
  118. }
  119. //获取Hash描述表
  120. public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
  121. {
  122. //从文件中取得Hash描述
  123. byte[] HashData;
  124. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  125. HashData = MD5.ComputeHash(objFile);
  126. objFile.Close();
  127. strHashData = Convert.ToBase64String(HashData);
  128. return true;
  129. }
  130. #endregion
  131. #region RSA签名
  132. //RSA签名
  133. public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
  134. {
  135. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  136. RSA.FromXmlString(p_strKeyPrivate);
  137. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  138. //设置签名的算法为MD5
  139. RSAFormatter.SetHashAlgorithm("MD5");
  140. //执行签名
  141. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  142. return true;
  143. }
  144. //RSA签名
  145. public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData)
  146. {
  147. byte[] EncryptedSignatureData;
  148. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  149. RSA.FromXmlString(p_strKeyPrivate);
  150. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  151. //设置签名的算法为MD5
  152. RSAFormatter.SetHashAlgorithm("MD5");
  153. //执行签名
  154. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  155. m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
  156. return true;
  157. }
  158. //RSA签名
  159. public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData)
  160. {
  161. byte[] HashbyteSignature;
  162. HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
  163. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  164. RSA.FromXmlString(p_strKeyPrivate);
  165. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  166. //设置签名的算法为MD5
  167. RSAFormatter.SetHashAlgorithm("MD5");
  168. //执行签名
  169. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  170. return true;
  171. }
  172. //RSA签名
  173. public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData)
  174. {
  175. byte[] HashbyteSignature;
  176. byte[] EncryptedSignatureData;
  177. HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
  178. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  179. RSA.FromXmlString(p_strKeyPrivate);
  180. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  181. //设置签名的算法为MD5
  182. RSAFormatter.SetHashAlgorithm("MD5");
  183. //执行签名
  184. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  185. m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
  186. return true;
  187. }
  188. #endregion
  189. #region RSA 签名验证
  190. public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
  191. {
  192. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  193. RSA.FromXmlString(p_strKeyPublic);
  194. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  195. //指定解密的时候HASH算法为MD5
  196. RSADeformatter.SetHashAlgorithm("MD5");
  197. if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  198. {
  199. return true;
  200. }
  201. else
  202. {
  203. return false;
  204. }
  205. }
  206. public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData)
  207. {
  208. byte[] HashbyteDeformatter;
  209. HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
  210. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  211. RSA.FromXmlString(p_strKeyPublic);
  212. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  213. //指定解密的时候HASH算法为MD5
  214. RSADeformatter.SetHashAlgorithm("MD5");
  215. if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  216. {
  217. return true;
  218. }
  219. else
  220. {
  221. return false;
  222. }
  223. }
  224. public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData)
  225. {
  226. byte[] DeformatterData;
  227. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  228. RSA.FromXmlString(p_strKeyPublic);
  229. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  230. //指定解密的时候HASH算法为MD5
  231. RSADeformatter.SetHashAlgorithm("MD5");
  232. DeformatterData =Convert.FromBase64String(p_strDeformatterData);
  233. if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  234. {
  235. return true;
  236. }
  237. else
  238. {
  239. return false;
  240. }
  241. }
  242. public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
  243. {
  244. byte[] DeformatterData;
  245. byte[] HashbyteDeformatter;
  246. HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
  247. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  248. RSA.FromXmlString(p_strKeyPublic);
  249. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  250. //指定解密的时候HASH算法为MD5
  251. RSADeformatter.SetHashAlgorithm("MD5");
  252. DeformatterData =Convert.FromBase64String(p_strDeformatterData);
  253. if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  254. {
  255. return true;
  256. }
  257. else
  258. {
  259. return false;
  260. }
  261. }
  262. #endregion
  263. #endregion
  264. }
  265. }