RsaCryption.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. namespace Aitex.Core.RT.Key
  7. {
  8. public class RsaCryption
  9. {
  10. #region RSA 加密解密
  11. #region RSA 的密钥产生
  12. /// <summary>
  13. /// RSA产生密钥
  14. /// </summary>
  15. /// <param name="xmlKeys">私钥</param>
  16. /// <param name="xmlPublicKey">公钥</param>
  17. public void RSAKey(out string xmlKeys, out string xmlPublicKey)
  18. {
  19. try
  20. {
  21. System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  22. xmlKeys = rsa.ToXmlString(true);
  23. xmlPublicKey = rsa.ToXmlString(false);
  24. }
  25. catch (Exception ex)
  26. {
  27. throw ex;
  28. }
  29. }
  30. #endregion
  31. #region RSA加密函数
  32. //##############################################################################
  33. //RSA 方式加密
  34. //KEY必须是XML的形式,返回的是字符串
  35. //该加密方式有长度限制的!
  36. //##############################################################################
  37. /// <summary>
  38. /// RSA的加密函数
  39. /// </summary>
  40. /// <param name="xmlPublicKey">公钥</param>
  41. /// <param name="encryptString">待加密的字符串</param>
  42. /// <returns></returns>
  43. public string RSAEncrypt(string xmlPublicKey, string encryptString)
  44. {
  45. try
  46. {
  47. byte[] PlainTextBArray;
  48. byte[] CypherTextBArray;
  49. string Result;
  50. System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  51. rsa.FromXmlString(xmlPublicKey);
  52. PlainTextBArray = (new UnicodeEncoding()).GetBytes(encryptString);
  53. CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
  54. Result = Convert.ToBase64String(CypherTextBArray);
  55. return Result;
  56. }
  57. catch (Exception ex)
  58. {
  59. throw ex;
  60. }
  61. }
  62. /// <summary>
  63. /// RSA的加密函数
  64. /// </summary>
  65. /// <param name="xmlPublicKey">公钥</param>
  66. /// <param name="EncryptString">待加密的字节数组</param>
  67. /// <returns></returns>
  68. public string RSAEncrypt(string xmlPublicKey, byte[] EncryptString)
  69. {
  70. try
  71. {
  72. byte[] CypherTextBArray;
  73. string Result;
  74. System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  75. rsa.FromXmlString(xmlPublicKey);
  76. CypherTextBArray = rsa.Encrypt(EncryptString, false);
  77. Result = Convert.ToBase64String(CypherTextBArray);
  78. return Result;
  79. }
  80. catch (Exception ex)
  81. {
  82. throw ex;
  83. }
  84. }
  85. #endregion
  86. #region RSA的解密函数
  87. /// <summary>
  88. /// RSA的解密函数
  89. /// </summary>
  90. /// <param name="xmlPrivateKey">私钥</param>
  91. /// <param name="decryptString">待解密的字符串</param>
  92. /// <returns></returns>
  93. public string RSADecrypt(string xmlPrivateKey, string decryptString)
  94. {
  95. try
  96. {
  97. byte[] PlainTextBArray;
  98. byte[] DypherTextBArray;
  99. string Result;
  100. System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  101. rsa.FromXmlString(xmlPrivateKey);
  102. PlainTextBArray = Convert.FromBase64String(decryptString);
  103. DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
  104. Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
  105. return Result;
  106. }
  107. catch (Exception ex)
  108. {
  109. throw ex;
  110. }
  111. }
  112. /// <summary>
  113. /// RSA的解密函数
  114. /// </summary>
  115. /// <param name="xmlPrivateKey">私钥</param>
  116. /// <param name="DecryptString">待解密的字节数组</param>
  117. /// <returns></returns>
  118. public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)
  119. {
  120. try
  121. {
  122. byte[] DypherTextBArray;
  123. string Result;
  124. System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  125. rsa.FromXmlString(xmlPrivateKey);
  126. DypherTextBArray = rsa.Decrypt(DecryptString, false);
  127. Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
  128. return Result;
  129. }
  130. catch (Exception ex)
  131. {
  132. throw ex;
  133. }
  134. }
  135. #endregion
  136. #endregion
  137. #region RSA数字签名
  138. #region 获取Hash描述表
  139. /// <summary>
  140. /// 获取Hash描述表
  141. /// </summary>
  142. /// <param name="strSource">待签名的字符串</param>
  143. /// <param name="HashData">Hash描述</param>
  144. /// <returns></returns>
  145. public bool GetHash(string strSource, ref byte[] HashData)
  146. {
  147. try
  148. {
  149. byte[] Buffer;
  150. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  151. Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(strSource);
  152. HashData = MD5.ComputeHash(Buffer);
  153. return true;
  154. }
  155. catch (Exception ex)
  156. {
  157. throw ex;
  158. }
  159. }
  160. /// <summary>
  161. /// 获取Hash描述表
  162. /// </summary>
  163. /// <param name="strSource">待签名的字符串</param>
  164. /// <param name="strHashData">Hash描述</param>
  165. /// <returns></returns>
  166. public bool GetHash(string strSource, ref string strHashData)
  167. {
  168. try
  169. {
  170. //从字符串中取得Hash描述
  171. byte[] Buffer;
  172. byte[] HashData;
  173. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  174. Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(strSource);
  175. HashData = MD5.ComputeHash(Buffer);
  176. strHashData = Convert.ToBase64String(HashData);
  177. return true;
  178. }
  179. catch (Exception ex)
  180. {
  181. throw ex;
  182. }
  183. }
  184. /// <summary>
  185. /// 获取Hash描述表
  186. /// </summary>
  187. /// <param name="objFile">待签名的文件</param>
  188. /// <param name="HashData">Hash描述</param>
  189. /// <returns></returns>
  190. public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)
  191. {
  192. try
  193. {
  194. //从文件中取得Hash描述
  195. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  196. HashData = MD5.ComputeHash(objFile);
  197. objFile.Close();
  198. return true;
  199. }
  200. catch (Exception ex)
  201. {
  202. throw ex;
  203. }
  204. }
  205. /// <summary>
  206. /// 获取Hash描述表
  207. /// </summary>
  208. /// <param name="objFile">待签名的文件</param>
  209. /// <param name="strHashData">Hash描述</param>
  210. /// <returns></returns>
  211. public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
  212. {
  213. try
  214. {
  215. //从文件中取得Hash描述
  216. byte[] HashData;
  217. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  218. HashData = MD5.ComputeHash(objFile);
  219. objFile.Close();
  220. strHashData = Convert.ToBase64String(HashData);
  221. return true;
  222. }
  223. catch (Exception ex)
  224. {
  225. throw ex;
  226. }
  227. }
  228. #endregion
  229. #region RSA签名
  230. /// <summary>
  231. /// RSA签名
  232. /// </summary>
  233. /// <param name="strKeyPrivate">私钥</param>
  234. /// <param name="HashbyteSignature">待签名Hash描述</param>
  235. /// <param name="EncryptedSignatureData">签名后的结果</param>
  236. /// <returns></returns>
  237. public bool SignatureFormatter(string strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
  238. {
  239. try
  240. {
  241. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  242. RSA.FromXmlString(strKeyPrivate);
  243. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  244. //设置签名的算法为MD5
  245. RSAFormatter.SetHashAlgorithm("MD5");
  246. //执行签名
  247. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  248. return true;
  249. }
  250. catch (Exception ex)
  251. {
  252. throw ex;
  253. }
  254. }
  255. /// <summary>
  256. /// RSA签名
  257. /// </summary>
  258. /// <param name="strKeyPrivate">私钥</param>
  259. /// <param name="HashbyteSignature">待签名Hash描述</param>
  260. /// <param name="m_strEncryptedSignatureData">签名后的结果</param>
  261. /// <returns></returns>
  262. public bool SignatureFormatter(string strKeyPrivate, byte[] HashbyteSignature, ref string strEncryptedSignatureData)
  263. {
  264. try
  265. {
  266. byte[] EncryptedSignatureData;
  267. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  268. RSA.FromXmlString(strKeyPrivate);
  269. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  270. //设置签名的算法为MD5
  271. RSAFormatter.SetHashAlgorithm("MD5");
  272. //执行签名
  273. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  274. strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
  275. return true;
  276. }
  277. catch (Exception ex)
  278. {
  279. throw ex;
  280. }
  281. }
  282. /// <summary>
  283. /// RSA签名
  284. /// </summary>
  285. /// <param name="strKeyPrivate">私钥</param>
  286. /// <param name="strHashbyteSignature">待签名Hash描述</param>
  287. /// <param name="EncryptedSignatureData">签名后的结果</param>
  288. /// <returns></returns>
  289. public bool SignatureFormatter(string strKeyPrivate, string strHashbyteSignature, ref byte[] EncryptedSignatureData)
  290. {
  291. try
  292. {
  293. byte[] HashbyteSignature;
  294. HashbyteSignature = Convert.FromBase64String(strHashbyteSignature);
  295. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  296. RSA.FromXmlString(strKeyPrivate);
  297. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  298. //设置签名的算法为MD5
  299. RSAFormatter.SetHashAlgorithm("MD5");
  300. //执行签名
  301. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  302. return true;
  303. }
  304. catch (Exception ex)
  305. {
  306. throw ex;
  307. }
  308. }
  309. /// <summary>
  310. /// RSA签名
  311. /// </summary>
  312. /// <param name="strKeyPrivate">私钥</param>
  313. /// <param name="strHashbyteSignature">待签名Hash描述</param>
  314. /// <param name="strEncryptedSignatureData">签名后的结果</param>
  315. /// <returns></returns>
  316. public bool SignatureFormatter(string strKeyPrivate, string strHashbyteSignature, ref string strEncryptedSignatureData)
  317. {
  318. try
  319. {
  320. byte[] HashbyteSignature;
  321. byte[] EncryptedSignatureData;
  322. HashbyteSignature = Convert.FromBase64String(strHashbyteSignature);
  323. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  324. RSA.FromXmlString(strKeyPrivate);
  325. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  326. //设置签名的算法为MD5
  327. RSAFormatter.SetHashAlgorithm("MD5");
  328. //执行签名
  329. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  330. strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
  331. return true;
  332. }
  333. catch (Exception ex)
  334. {
  335. throw ex;
  336. }
  337. }
  338. #endregion
  339. #region RSA 签名验证
  340. /// <summary>
  341. /// RSA签名验证
  342. /// </summary>
  343. /// <param name="strKeyPublic">公钥</param>
  344. /// <param name="HashbyteDeformatter">Hash描述</param>
  345. /// <param name="DeformatterData">签名后的结果</param>
  346. /// <returns></returns>
  347. public bool SignatureDeformatter(string strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
  348. {
  349. try
  350. {
  351. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  352. RSA.FromXmlString(strKeyPublic);
  353. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  354. //指定解密的时候HASH算法为MD5
  355. RSADeformatter.SetHashAlgorithm("MD5");
  356. if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
  357. {
  358. return true;
  359. }
  360. else
  361. {
  362. return false;
  363. }
  364. }
  365. catch (Exception ex)
  366. {
  367. throw ex;
  368. }
  369. }
  370. /// <summary>
  371. /// RSA签名验证
  372. /// </summary>
  373. /// <param name="strKeyPublic">公钥</param>
  374. /// <param name="strHashbyteDeformatter">Hash描述</param>
  375. /// <param name="DeformatterData">签名后的结果</param>
  376. /// <returns></returns>
  377. public bool SignatureDeformatter(string strKeyPublic, string strHashbyteDeformatter, byte[] DeformatterData)
  378. {
  379. try
  380. {
  381. byte[] HashbyteDeformatter;
  382. HashbyteDeformatter = Convert.FromBase64String(strHashbyteDeformatter);
  383. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  384. RSA.FromXmlString(strKeyPublic);
  385. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  386. //指定解密的时候HASH算法为MD5
  387. RSADeformatter.SetHashAlgorithm("MD5");
  388. if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
  389. {
  390. return true;
  391. }
  392. else
  393. {
  394. return false;
  395. }
  396. }
  397. catch (Exception ex)
  398. {
  399. throw ex;
  400. }
  401. }
  402. /// <summary>
  403. /// RSA签名验证
  404. /// </summary>
  405. /// <param name="strKeyPublic">公钥</param>
  406. /// <param name="HashbyteDeformatter">Hash描述</param>
  407. /// <param name="strDeformatterData">签名后的结果</param>
  408. /// <returns></returns>
  409. public bool SignatureDeformatter(string strKeyPublic, byte[] HashbyteDeformatter, string strDeformatterData)
  410. {
  411. try
  412. {
  413. byte[] DeformatterData;
  414. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  415. RSA.FromXmlString(strKeyPublic);
  416. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  417. //指定解密的时候HASH算法为MD5
  418. RSADeformatter.SetHashAlgorithm("MD5");
  419. DeformatterData = Convert.FromBase64String(strDeformatterData);
  420. if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
  421. {
  422. return true;
  423. }
  424. else
  425. {
  426. return false;
  427. }
  428. }
  429. catch (Exception ex)
  430. {
  431. throw ex;
  432. }
  433. }
  434. /// <summary>
  435. /// RSA签名验证
  436. /// </summary>
  437. /// <param name="strKeyPublic">公钥</param>
  438. /// <param name="strHashbyteDeformatter">Hash描述</param>
  439. /// <param name="strDeformatterData">签名后的结果</param>
  440. /// <returns></returns>
  441. public bool SignatureDeformatter(string strKeyPublic, string strHashbyteDeformatter, string strDeformatterData)
  442. {
  443. try
  444. {
  445. byte[] DeformatterData;
  446. byte[] HashbyteDeformatter;
  447. HashbyteDeformatter = Convert.FromBase64String(strHashbyteDeformatter);
  448. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  449. RSA.FromXmlString(strKeyPublic);
  450. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  451. //指定解密的时候HASH算法为MD5
  452. RSADeformatter.SetHashAlgorithm("MD5");
  453. DeformatterData = Convert.FromBase64String(strDeformatterData);
  454. if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
  455. {
  456. return true;
  457. }
  458. else
  459. {
  460. return false;
  461. }
  462. }
  463. catch (Exception ex)
  464. {
  465. throw ex;
  466. }
  467. }
  468. #endregion
  469. #endregion
  470. }
  471. }