RSACrypt.cs 17 KB

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