RSACrypt.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace Masuit.Tools.Security;
  6. /// <summary>
  7. /// RSA加密解密及RSA签名和验证
  8. /// </summary>
  9. public static class RsaCrypt
  10. {
  11. private static RsaKey RsaKey;
  12. #region RSA 加密解密
  13. #region RSA 的密钥产生
  14. /// <summary>
  15. /// 生成 RSA 公钥和私钥
  16. /// </summary>
  17. /// <param name="type">密钥类型</param>
  18. /// <param name="length">密钥长度</param>
  19. /// <returns></returns>
  20. public static RsaKey GenerateRsaKeys(RsaKeyType type = RsaKeyType.PKCS8, int length = 1024)
  21. {
  22. var rsa = new RSA(length);
  23. return type switch
  24. {
  25. RsaKeyType.PKCS1 => RsaKey ??= new RsaKey
  26. {
  27. PrivateKey = rsa.ToPEM_PKCS1(),
  28. PublicKey = rsa.ToPEM_PKCS1(true)
  29. },
  30. RsaKeyType.PKCS8 => RsaKey ??= new RsaKey
  31. {
  32. PrivateKey = rsa.ToPEM_PKCS8(),
  33. PublicKey = rsa.ToPEM_PKCS8(true)
  34. },
  35. RsaKeyType.XML => RsaKey ??= new RsaKey
  36. {
  37. PrivateKey = rsa.ToXML(),
  38. PublicKey = rsa.ToXML(true)
  39. },
  40. _ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
  41. };
  42. }
  43. #endregion RSA 的密钥产生
  44. #region RSA的加密函数
  45. /// <summary>
  46. /// RSA的加密函数 string
  47. /// </summary>
  48. /// <param name="publicKey">公钥</param>
  49. /// <param name="value">需要加密的字符串</param>
  50. /// <returns>加密后的内容</returns>
  51. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  52. public static string RSAEncrypt(this string value, string publicKey)
  53. {
  54. var rsa = new RSA(publicKey);
  55. return rsa.Encrypt(value);
  56. }
  57. /// <summary>
  58. /// RSA的加密函数 string
  59. /// </summary>
  60. /// <param name="value">需要加密的字符串</param>
  61. /// <returns>加密后的内容</returns>
  62. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  63. public static string RSAEncrypt(this string value)
  64. {
  65. return RSAEncrypt(value, RsaKey.PublicKey);
  66. }
  67. /// <summary>
  68. /// RSA的加密函数 byte[]
  69. /// </summary>
  70. /// <param name="data">需要加密的字节数组</param>
  71. /// <param name="publicKey">公钥</param>
  72. /// <returns>加密后的内容</returns>
  73. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  74. public static string RSAEncrypt(this byte[] data, string publicKey)
  75. {
  76. var rsa = new RSA(publicKey);
  77. return Convert.ToBase64String(rsa.Encrypt(data));
  78. }
  79. /// <summary>
  80. /// RSA的加密函数 byte[]
  81. /// </summary>
  82. /// <param name="data">需要加密的字节数组</param>
  83. /// <returns>加密后的内容</returns>
  84. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  85. public static string RSAEncrypt(this byte[] data)
  86. {
  87. return RSAEncrypt(data, RsaKey.PublicKey);
  88. }
  89. #endregion RSA的加密函数
  90. #region RSA的解密函数
  91. /// <summary>
  92. /// RSA的解密函数 string
  93. /// </summary>
  94. /// <param name="value">需要解密的字符串</param>
  95. /// <param name="privateKey">私钥</param>
  96. /// <returns>解密后的内容</returns>
  97. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  98. public static string RSADecrypt(this string value, string privateKey)
  99. {
  100. var rsa = new RSA(privateKey);
  101. return rsa.DecryptOrNull(value);
  102. }
  103. /// <summary>
  104. /// RSA的解密函数 string
  105. /// </summary>
  106. /// <param name="value">需要解密的字符串</param>
  107. /// <returns>解密后的内容</returns>
  108. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  109. public static string RSADecrypt(this string value)
  110. {
  111. return RSADecrypt(value, RsaKey.PrivateKey);
  112. }
  113. /// <summary>
  114. /// RSA的解密函数 byte
  115. /// </summary>
  116. /// <param name="data">需要解密的字符串</param>
  117. /// <param name="privateKey">私钥</param>
  118. /// <returns>解密后的内容</returns>
  119. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  120. public static string RSADecrypt(this byte[] data, string privateKey)
  121. {
  122. var rsa = new RSA(privateKey);
  123. return new UnicodeEncoding().GetString(rsa.DecryptOrNull(data));
  124. }
  125. /// <summary>
  126. /// RSA的解密函数 byte
  127. /// </summary>
  128. /// <param name="data">需要解密的字符串</param>
  129. /// <returns>解密后的内容</returns>
  130. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  131. public static string RSADecrypt(this byte[] data)
  132. {
  133. return RSADecrypt(data, RsaKey.PrivateKey);
  134. }
  135. #endregion RSA的解密函数
  136. #endregion RSA 加密解密
  137. #region RSA数字签名
  138. #region 获取Hash描述表
  139. /// <summary>
  140. /// 获取Hash描述表
  141. /// </summary>
  142. /// <param name="value">源数据</param>
  143. /// <returns>Hash描述表</returns>
  144. public static byte[] GetHashBytes(this string value)
  145. {
  146. //从字符串中取得Hash描述
  147. using var md5 = MD5.Create();
  148. var buffer = Encoding.UTF8.GetBytes(value);
  149. return md5.ComputeHash(buffer);
  150. }
  151. /// <summary>
  152. /// 获取Hash描述表
  153. /// </summary>
  154. /// <param name="value">源数据</param>
  155. /// <returns>Hash描述表</returns>
  156. public static string GetHashString(this string value)
  157. {
  158. //从字符串中取得Hash描述
  159. using var md5 = MD5.Create();
  160. var buffer = Encoding.UTF8.GetBytes(value);
  161. var hashData = md5.ComputeHash(buffer);
  162. return Convert.ToBase64String(hashData);
  163. }
  164. /// <summary>
  165. /// 从文件流获取Hash描述表
  166. /// </summary>
  167. /// <param name="file">源文件</param>
  168. /// <returns>Hash描述表</returns>
  169. public static byte[] GetHashBytes(this FileStream file)
  170. {
  171. //从文件中取得Hash描述
  172. using var md5 = MD5.Create();
  173. return md5.ComputeHash(file);
  174. }
  175. /// <summary>
  176. /// 从文件流获取Hash描述表
  177. /// </summary>
  178. /// <param name="file">源文件</param>
  179. /// <returns>Hash描述表</returns>
  180. public static string GetHashString(this FileStream file)
  181. {
  182. //从文件中取得Hash描述
  183. using var md5 = MD5.Create();
  184. var hashData = md5.ComputeHash(file);
  185. return Convert.ToBase64String(hashData);
  186. }
  187. #endregion 获取Hash描述表
  188. #region RSA签名
  189. /// <summary>
  190. /// RSA签名
  191. /// </summary>
  192. /// <param name="data">签名字节数据</param>
  193. /// <param name="privateKey">私钥</param>
  194. /// <param name="halg">hash算法</param>
  195. /// <returns>处理结果</returns>
  196. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  197. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  198. public static byte[] SignatureBytes(this byte[] data, string privateKey, HashAlgo halg = HashAlgo.MD5)
  199. {
  200. var rsa = new RSA(privateKey);
  201. return rsa.Sign(halg.ToString(), data);
  202. }
  203. /// <summary>
  204. /// RSA签名
  205. /// </summary>
  206. /// <param name="data">签名字节数据</param>
  207. /// <param name="privateKey">私钥</param>
  208. /// <returns>处理结果</returns>
  209. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  210. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  211. public static string SignatureString(this byte[] data, string privateKey)
  212. {
  213. return Convert.ToBase64String(SignatureBytes(data, privateKey));
  214. }
  215. /// <summary>
  216. /// RSA签名
  217. /// </summary>
  218. /// <param name="value">签名字符串数据</param>
  219. /// <param name="privateKey">私钥</param>
  220. /// <param name="halg">hash算法</param>
  221. /// <returns>处理结果</returns>
  222. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  223. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  224. public static byte[] SignatureBytes(this string value, string privateKey, HashAlgo halg = HashAlgo.MD5)
  225. {
  226. var rsa = new RSA(privateKey);
  227. return Encoding.UTF32.GetBytes(rsa.Sign(halg.ToString(), value));
  228. }
  229. /// <summary>
  230. /// RSA签名
  231. /// </summary>
  232. /// <param name="value">签名字符串数据</param>
  233. /// <param name="privateKey">私钥</param>
  234. /// <param name="halg">hash算法</param>
  235. /// <returns>处理结果</returns>
  236. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  237. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  238. public static string SignatureString(this string value, string privateKey, HashAlgo halg = HashAlgo.MD5)
  239. {
  240. var rsa = new RSA(privateKey);
  241. return rsa.Sign(halg.ToString(), value);
  242. }
  243. #endregion RSA签名
  244. #region RSA 签名验证
  245. /// <summary>
  246. /// RSA 签名验证
  247. /// </summary>
  248. /// <param name="data">反格式化字节数据</param>
  249. /// <param name="publicKey">公钥</param>
  250. /// <param name="sign">哈希字节数据</param>
  251. /// <param name="halg">hash算法</param>
  252. /// <returns>处理结果</returns>
  253. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  254. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  255. public static bool SignatureDeformatter(this byte[] data, string publicKey, byte[] sign, HashAlgo halg = HashAlgo.MD5)
  256. {
  257. var rsa = new RSA(publicKey);
  258. return rsa.Verify(halg.ToString(), sign, data);
  259. }
  260. /// <summary>
  261. /// RSA 签名验证
  262. /// </summary>
  263. /// <param name="data">反格式化字节数据</param>
  264. /// <param name="publicKey">公钥</param>
  265. /// <param name="sign">哈希字符串数据</param>
  266. /// <param name="halg">hash算法</param>
  267. /// <returns>处理结果</returns>
  268. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  269. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  270. public static bool SignatureDeformatter(this byte[] data, string publicKey, string sign, HashAlgo halg = HashAlgo.MD5)
  271. {
  272. var rsa = new RSA(publicKey);
  273. return rsa.Verify(halg.ToString(), Convert.FromBase64String(sign), data);
  274. }
  275. /// <summary>
  276. /// RSA 签名验证
  277. /// </summary>
  278. /// <param name="value">反格式化字符串数据</param>
  279. /// <param name="publicKey">公钥</param>
  280. /// <param name="sign">哈希字节数据</param>
  281. /// <param name="halg">hash算法</param>
  282. /// <returns>处理结果</returns>
  283. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  284. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  285. public static bool SignatureDeformatter(this string value, string publicKey, byte[] sign, HashAlgo halg = HashAlgo.MD5)
  286. {
  287. var rsa = new RSA(publicKey);
  288. return rsa.Verify(halg.ToString(), sign, Convert.FromBase64String(value));
  289. }
  290. /// <summary>
  291. /// RSA 签名验证
  292. /// </summary>
  293. /// <param name="value">格式字符串数据</param>
  294. /// <param name="publicKey">公钥</param>
  295. /// <param name="sign">哈希字符串数据</param>
  296. /// <param name="halg">hash算法</param>
  297. /// <returns>处理结果</returns>
  298. /// <exception cref="CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
  299. /// <exception cref="CryptographicUnexpectedOperationException">The key is null.-or- The hash algorithm is null. </exception>
  300. public static bool SignatureDeformatter(this string value, string publicKey, string sign, HashAlgo halg = HashAlgo.MD5)
  301. {
  302. var rsa = new RSA(publicKey);
  303. return rsa.Verify(halg.ToString(), sign, value);
  304. }
  305. #endregion RSA 签名验证
  306. #endregion RSA数字签名
  307. }