1
1

RSACrypt.cs 14 KB

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