RSACrypt.cs 16 KB

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