RSACrypt.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace Masuit.Tools.Security
  6. {
  7. /// <summary>
  8. /// RSA密钥对
  9. /// </summary>
  10. public struct RsaKey
  11. {
  12. /// <summary>
  13. /// 公钥
  14. /// </summary>
  15. public string PublicKey;
  16. /// <summary>
  17. /// 私钥
  18. /// </summary>
  19. public string PrivateKey;
  20. }
  21. /// <summary>
  22. /// RSA加密解密及RSA签名和验证
  23. /// </summary>
  24. public static class RsaCrypt
  25. {
  26. #region RSA 加密解密
  27. #region RSA 的密钥产生
  28. /// <summary>
  29. /// 生成 RSA 公钥和私钥
  30. /// </summary>
  31. public static RsaKey GenerateRsaKeys()
  32. {
  33. using (var rsa = new RSACryptoServiceProvider())
  34. {
  35. return new RsaKey { PrivateKey = rsa.ToXmlString(true), PublicKey = rsa.ToXmlString(false) };
  36. }
  37. }
  38. #endregion
  39. #region RSA的加密函数
  40. //##############################################################################
  41. //RSA 方式加密
  42. //说明KEY必须是XML的行式,返回的是字符串
  43. //在有一点需要说明!!该加密方式有 长度 限制的!!
  44. //##############################################################################
  45. //RSA的加密函数 string
  46. /// <summary>
  47. /// RSA的加密函数 string
  48. /// </summary>
  49. /// <param name="publicKey">公钥</param>
  50. /// <param name="mStrEncryptString">需要加密的字符串</param>
  51. /// <returns>加密后的内容</returns>
  52. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  53. public static string RSAEncrypt(this string mStrEncryptString, string publicKey)
  54. {
  55. var rsa = new RSACryptoServiceProvider();
  56. rsa.FromXmlString(publicKey);
  57. var plainTextBArray = new UnicodeEncoding().GetBytes(mStrEncryptString);
  58. var cypherTextBArray = rsa.Encrypt(plainTextBArray, false);
  59. return Convert.ToBase64String(cypherTextBArray);
  60. }
  61. //RSA的加密函数 byte[]
  62. /// <summary>
  63. /// RSA的加密函数 byte[]
  64. /// </summary>
  65. /// <param name="encryptString">需要加密的字节数组</param>
  66. /// <param name="publicKey">公钥</param>
  67. /// <returns>加密后的内容</returns>
  68. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  69. public static string RSAEncrypt(this byte[] encryptString, string publicKey)
  70. {
  71. var rsa = new RSACryptoServiceProvider();
  72. rsa.FromXmlString(publicKey);
  73. var cypherTextBArray = rsa.Encrypt(encryptString, false);
  74. return Convert.ToBase64String(cypherTextBArray);
  75. }
  76. #endregion
  77. #region RSA的解密函数
  78. //RSA的解密函数 string
  79. /// <summary>
  80. /// RSA的解密函数 string
  81. /// </summary>
  82. /// <param name="mStrDecryptString">需要解密的字符串</param>
  83. /// <param name="privateKey">私钥</param>
  84. /// <returns>解密后的内容</returns>
  85. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  86. public static string RSADecrypt(this string mStrDecryptString, string privateKey)
  87. {
  88. var rsa = new RSACryptoServiceProvider();
  89. rsa.FromXmlString(privateKey);
  90. var plainTextBArray = Convert.FromBase64String(mStrDecryptString);
  91. var dypherTextBArray = rsa.Decrypt(plainTextBArray, false);
  92. return new UnicodeEncoding().GetString(dypherTextBArray);
  93. }
  94. //RSA的解密函数 byte
  95. /// <summary>
  96. /// RSA的解密函数 byte
  97. /// </summary>
  98. /// <param name="decryptString">需要解密的字符串</param>
  99. /// <param name="privateKey">私钥</param>
  100. /// <returns>解密后的内容</returns>
  101. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  102. public static string RSADecrypt(this byte[] decryptString, string privateKey)
  103. {
  104. var rsa = new RSACryptoServiceProvider();
  105. rsa.FromXmlString(privateKey);
  106. var dypherTextBArray = rsa.Decrypt(decryptString, false);
  107. return new UnicodeEncoding().GetString(dypherTextBArray);
  108. }
  109. #endregion
  110. #endregion
  111. #region RSA数字签名
  112. #region 获取Hash描述表
  113. //获取Hash描述表
  114. /// <summary>
  115. /// 获取Hash描述表
  116. /// </summary>
  117. /// <param name="mStrSource">源数据</param>
  118. /// <returns>Hash描述表</returns>
  119. public static byte[] GetHashBytes(this string mStrSource)
  120. {
  121. //从字符串中取得Hash描述
  122. HashAlgorithm md5 = HashAlgorithm.Create("MD5");
  123. var buffer = Encoding.UTF8.GetBytes(mStrSource);
  124. return md5?.ComputeHash(buffer);
  125. }
  126. //获取Hash描述表
  127. /// <summary>
  128. /// 获取Hash描述表
  129. /// </summary>
  130. /// <param name="mStrSource">源数据</param>
  131. /// <returns>Hash描述表</returns>
  132. public static string GetHashString(this string mStrSource)
  133. {
  134. //从字符串中取得Hash描述
  135. HashAlgorithm md5 = HashAlgorithm.Create("MD5");
  136. var buffer = Encoding.UTF8.GetBytes(mStrSource);
  137. var hashData = md5?.ComputeHash(buffer);
  138. return Convert.ToBase64String(hashData);
  139. }
  140. //获取Hash描述表
  141. /// <summary>
  142. /// 从文件流获取Hash描述表
  143. /// </summary>
  144. /// <param name="objFile">源文件</param>
  145. /// <returns>Hash描述表</returns>
  146. public static byte[] GetHashBytes(this FileStream objFile)
  147. {
  148. //从文件中取得Hash描述
  149. using (objFile)
  150. {
  151. HashAlgorithm md5 = HashAlgorithm.Create("MD5");
  152. return md5?.ComputeHash(objFile);
  153. }
  154. }
  155. //获取Hash描述表
  156. /// <summary>
  157. /// 从文件流获取Hash描述表
  158. /// </summary>
  159. /// <param name="objFile">源文件</param>
  160. /// <returns>Hash描述表</returns>
  161. public static string GetHashString(this FileStream objFile)
  162. {
  163. //从文件中取得Hash描述
  164. using (objFile)
  165. {
  166. HashAlgorithm md5 = HashAlgorithm.Create("MD5");
  167. var hashData = md5?.ComputeHash(objFile);
  168. return Convert.ToBase64String(hashData);
  169. }
  170. }
  171. #endregion
  172. #region RSA签名
  173. //RSA签名
  174. /// <summary>
  175. /// RSA签名
  176. /// </summary>
  177. /// <param name="hashbyteSignature">签名字节数据</param>
  178. /// <param name="privateKey">私钥</param>
  179. /// <returns>处理结果</returns>
  180. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  181. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  182. public static byte[] SignatureBytes(this byte[] hashbyteSignature, string privateKey)
  183. {
  184. var rsa = new RSACryptoServiceProvider();
  185. rsa.FromXmlString(privateKey);
  186. var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
  187. //设置签名的算法为MD5
  188. rsaFormatter.SetHashAlgorithm("MD5");
  189. //执行签名
  190. return rsaFormatter.CreateSignature(hashbyteSignature);
  191. }
  192. //RSA签名
  193. /// <summary>
  194. /// RSA签名
  195. /// </summary>
  196. /// <param name="hashbyteSignature">签名字节数据</param>
  197. /// <param name="privateKey">私钥</param>
  198. /// <returns>处理结果</returns>
  199. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  200. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  201. public static string SignatureString(this byte[] hashbyteSignature, string privateKey)
  202. {
  203. var rsa = new RSACryptoServiceProvider();
  204. rsa.FromXmlString(privateKey);
  205. var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
  206. //设置签名的算法为MD5
  207. rsaFormatter.SetHashAlgorithm("MD5");
  208. //执行签名
  209. var encryptedSignatureData = rsaFormatter.CreateSignature(hashbyteSignature);
  210. return Convert.ToBase64String(encryptedSignatureData);
  211. }
  212. //RSA签名
  213. /// <summary>
  214. /// RSA签名
  215. /// </summary>
  216. /// <param name="mStrHashbyteSignature">签名字符串数据</param>
  217. /// <param name="pStrKeyPrivate">私钥</param>
  218. /// <returns>处理结果</returns>
  219. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  220. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  221. public static byte[] SignatureBytes(this string mStrHashbyteSignature, string pStrKeyPrivate)
  222. {
  223. byte[] hashbyteSignature = Convert.FromBase64String(mStrHashbyteSignature);
  224. var rsa = new RSACryptoServiceProvider();
  225. rsa.FromXmlString(pStrKeyPrivate);
  226. var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
  227. //设置签名的算法为MD5
  228. rsaFormatter.SetHashAlgorithm("MD5");
  229. //执行签名
  230. return rsaFormatter.CreateSignature(hashbyteSignature);
  231. }
  232. //RSA签名
  233. /// <summary>
  234. /// RSA签名
  235. /// </summary>
  236. /// <param name="mStrHashbyteSignature">签名字符串数据</param>
  237. /// <param name="pStrKeyPrivate">私钥</param>
  238. /// <returns>处理结果</returns>
  239. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  240. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  241. public static string SignatureString(this string mStrHashbyteSignature, string pStrKeyPrivate)
  242. {
  243. var hashbyteSignature = Convert.FromBase64String(mStrHashbyteSignature);
  244. var rsa = new RSACryptoServiceProvider();
  245. rsa.FromXmlString(pStrKeyPrivate);
  246. var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
  247. //设置签名的算法为MD5
  248. rsaFormatter.SetHashAlgorithm("MD5");
  249. //执行签名
  250. var encryptedSignatureData = rsaFormatter.CreateSignature(hashbyteSignature);
  251. return Convert.ToBase64String(encryptedSignatureData);
  252. }
  253. #endregion
  254. #region RSA 签名验证
  255. /// <summary>
  256. /// RSA 签名验证
  257. /// </summary>
  258. /// <param name="deformatterData">反格式化字节数据</param>
  259. /// <param name="publicKey">公钥</param>
  260. /// <param name="hashbyteDeformatter">哈希字节数据</param>
  261. /// <returns>处理结果</returns>
  262. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  263. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  264. public static bool SignatureDeformatter(this byte[] deformatterData, string publicKey, byte[] hashbyteDeformatter)
  265. {
  266. var rsa = new RSACryptoServiceProvider();
  267. rsa.FromXmlString(publicKey);
  268. var rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
  269. //指定解密的时候HASH算法为MD5
  270. rsaDeformatter.SetHashAlgorithm("MD5");
  271. if (rsaDeformatter.VerifySignature(hashbyteDeformatter, deformatterData)) return true;
  272. return false;
  273. }
  274. /// <summary>
  275. /// RSA 签名验证
  276. /// </summary>
  277. /// <param name="deformatterData">反格式化字节数据</param>
  278. /// <param name="publicKey">公钥</param>
  279. /// <param name="pStrHashbyteDeformatter">哈希字符串数据</param>
  280. /// <returns>处理结果</returns>
  281. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  282. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  283. public static bool SignatureDeformatter(this byte[] deformatterData, string publicKey, string pStrHashbyteDeformatter)
  284. {
  285. byte[] hashbyteDeformatter = Convert.FromBase64String(pStrHashbyteDeformatter);
  286. var rsa = new RSACryptoServiceProvider();
  287. rsa.FromXmlString(publicKey);
  288. var rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
  289. //指定解密的时候HASH算法为MD5
  290. rsaDeformatter.SetHashAlgorithm("MD5");
  291. if (rsaDeformatter.VerifySignature(hashbyteDeformatter, deformatterData)) return true;
  292. return false;
  293. }
  294. /// <summary>
  295. /// RSA 签名验证
  296. /// </summary>
  297. /// <param name="pStrDeformatterData">反格式化字符串数据</param>
  298. /// <param name="publicKey">公钥</param>
  299. /// <param name="hashbyteDeformatter">哈希字节数据</param>
  300. /// <returns>处理结果</returns>
  301. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  302. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  303. public static bool SignatureDeformatter(this string pStrDeformatterData, string publicKey, byte[] hashbyteDeformatter)
  304. {
  305. var rsa = new RSACryptoServiceProvider();
  306. rsa.FromXmlString(publicKey);
  307. var rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
  308. //指定解密的时候HASH算法为MD5
  309. rsaDeformatter.SetHashAlgorithm("MD5");
  310. var deformatterData = Convert.FromBase64String(pStrDeformatterData);
  311. if (rsaDeformatter.VerifySignature(hashbyteDeformatter, deformatterData)) return true;
  312. return false;
  313. }
  314. /// <summary>
  315. /// RSA 签名验证
  316. /// </summary>
  317. /// <param name="pStrDeformatterData">格式字符串数据</param>
  318. /// <param name="publicKey">公钥</param>
  319. /// <param name="pStrHashbyteDeformatter">哈希字符串数据</param>
  320. /// <returns>处理结果</returns>
  321. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  322. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  323. public static bool SignatureDeformatter(this string pStrDeformatterData, string publicKey, string pStrHashbyteDeformatter)
  324. {
  325. byte[] hashbyteDeformatter = Convert.FromBase64String(pStrHashbyteDeformatter);
  326. var rsa = new RSACryptoServiceProvider();
  327. rsa.FromXmlString(publicKey);
  328. var rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
  329. //指定解密的时候HASH算法为MD5
  330. rsaDeformatter.SetHashAlgorithm("MD5");
  331. var deformatterData = Convert.FromBase64String(pStrDeformatterData);
  332. if (rsaDeformatter.VerifySignature(hashbyteDeformatter, deformatterData)) return true;
  333. return false;
  334. }
  335. #endregion
  336. #endregion
  337. }
  338. }