点击下载 RSACryption.zip

这个类是关于加密,解密的操作,文件的一些高级操作
1.RSACryption RSA 的密钥产生
2.RSACryption RSA的加密函数
3.RSACryption RSA的解密函数
4.RSACryption 获取Hash描述表 
5.RSACryption RSA签名 
6.RSACryption RSA签名认证
看下面代码吧

  1. /// <summary>
  2. /// 类说明:Assistant
  3. /// 编 码 人:苏飞
  4. /// 联系方式:361983679
  5. /// 更新网站:[url=http://www.cckan.net/thread-655-1-1.html]http://www.cckan.net/thread-655-1-1.html[/url]
  6. /// </summary>
  7. using System;
  8. using System.Text;
  9. using System.Security.Cryptography;
  10. namespace DotNet.Utilities
  11. {
  12. /// <summary>
  13. /// RSA加密解密及RSA签名和验证
  14. /// </summary>
  15. public class RSACryption
  16. {
  17. public RSACryption()
  18. {
  19. }
  20.  
  21. #region RSA 加密解密
  22.  
  23. #region RSA 的密钥产生
  24.  
  25. /// <summary>
  26. /// RSA 的密钥产生 产生私钥 和公钥
  27. /// </summary>
  28. /// <param name="xmlKeys"></param>
  29. /// <param name="xmlPublicKey"></param>
  30. public void RSAKey(out string xmlKeys,out string xmlPublicKey)
  31. {
  32. System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  33. xmlKeys=rsa.ToXmlString(true);
  34. xmlPublicKey = rsa.ToXmlString(false);
  35. }
  36. #endregion
  37.  
  38. #region RSA的加密函数
  39. //##############################################################################
  40. //RSA 方式加密
  41. //说明KEY必须是XML的行式,返回的是字符串
  42. //在有一点需要说明!!该加密方式有 长度 限制的!!
  43. //##############################################################################
  44.  
  45. //RSA的加密函数 string
  46. public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString )
  47. {
  48.  
  49. byte[] PlainTextBArray;
  50. byte[] CypherTextBArray;
  51. string Result;
  52. RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  53. rsa.FromXmlString(xmlPublicKey);
  54. PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);
  55. CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
  56. Result=Convert.ToBase64String(CypherTextBArray);
  57. return Result;
  58.  
  59. }
  60. //RSA的加密函数 byte[]
  61. public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString )
  62. {
  63.  
  64. byte[] CypherTextBArray;
  65. string Result;
  66. RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  67. rsa.FromXmlString(xmlPublicKey);
  68. CypherTextBArray = rsa.Encrypt(EncryptString, false);
  69. Result=Convert.ToBase64String(CypherTextBArray);
  70. return Result;
  71.  
  72. }
  73. #endregion
  74.  
  75. #region RSA的解密函数
  76. //RSA的解密函数 string
  77. public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString )
  78. {
  79. byte[] PlainTextBArray;
  80. byte[] DypherTextBArray;
  81. string Result;
  82. System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  83. rsa.FromXmlString(xmlPrivateKey);
  84. PlainTextBArray =Convert.FromBase64String(m_strDecryptString);
  85. DypherTextBArray=rsa.Decrypt(PlainTextBArray, false);
  86. Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
  87. return Result;
  88.  
  89. }
  90.  
  91. //RSA的解密函数 byte
  92. public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString )
  93. {
  94. byte[] DypherTextBArray;
  95. string Result;
  96. System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  97. rsa.FromXmlString(xmlPrivateKey);
  98. DypherTextBArray=rsa.Decrypt(DecryptString, false);
  99. Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
  100. return Result;
  101.  
  102. }
  103. #endregion
  104.  
  105. #endregion
  106.  
  107. #region RSA数字签名
  108.  
  109. #region 获取Hash描述表
  110. //获取Hash描述表
  111. public bool GetHash(string m_strSource, ref byte[] HashData)
  112. {
  113. //从字符串中取得Hash描述
  114. byte[] Buffer;
  115. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  116. Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
  117. HashData = MD5.ComputeHash(Buffer);
  118.  
  119. return true;
  120. }
  121.  
  122. //获取Hash描述表
  123. public bool GetHash(string m_strSource, ref string strHashData)
  124. {
  125.  
  126. //从字符串中取得Hash描述
  127. byte[] Buffer;
  128. byte[] HashData;
  129. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  130. Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
  131. HashData = MD5.ComputeHash(Buffer);
  132.  
  133. strHashData = Convert.ToBase64String(HashData);
  134. return true;
  135.  
  136. }
  137.  
  138. //获取Hash描述表
  139. public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)
  140. {
  141.  
  142. //从文件中取得Hash描述
  143. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  144. HashData = MD5.ComputeHash(objFile);
  145. objFile.Close();
  146.  
  147. return true;
  148.  
  149. }
  150.  
  151. //获取Hash描述表
  152. public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
  153. {
  154.  
  155. //从文件中取得Hash描述
  156. byte[] HashData;
  157. System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  158. HashData = MD5.ComputeHash(objFile);
  159. objFile.Close();
  160.  
  161. strHashData = Convert.ToBase64String(HashData);
  162.  
  163. return true;
  164.  
  165. }
  166. #endregion
  167.  
  168. #region RSA签名
  169. //RSA签名
  170. public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
  171. {
  172.  
  173. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  174.  
  175. RSA.FromXmlString(p_strKeyPrivate);
  176. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  177. //设置签名的算法为MD5
  178. RSAFormatter.SetHashAlgorithm("MD5");
  179. //执行签名
  180. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  181.  
  182. return true;
  183.  
  184. }
  185.  
  186. //RSA签名
  187. public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData)
  188. {
  189.  
  190. byte[] EncryptedSignatureData;
  191.  
  192. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  193.  
  194. RSA.FromXmlString(p_strKeyPrivate);
  195. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  196. //设置签名的算法为MD5
  197. RSAFormatter.SetHashAlgorithm("MD5");
  198. //执行签名
  199. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  200.  
  201. m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
  202.  
  203. return true;
  204.  
  205. }
  206.  
  207. //RSA签名
  208. public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData)
  209. {
  210.  
  211. byte[] HashbyteSignature;
  212.  
  213. HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
  214. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  215.  
  216. RSA.FromXmlString(p_strKeyPrivate);
  217. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  218. //设置签名的算法为MD5
  219. RSAFormatter.SetHashAlgorithm("MD5");
  220. //执行签名
  221. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  222.  
  223. return true;
  224.  
  225. }
  226.  
  227. //RSA签名
  228. public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData)
  229. {
  230.  
  231. byte[] HashbyteSignature;
  232. byte[] EncryptedSignatureData;
  233.  
  234. HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
  235. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  236.  
  237. RSA.FromXmlString(p_strKeyPrivate);
  238. System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  239. //设置签名的算法为MD5
  240. RSAFormatter.SetHashAlgorithm("MD5");
  241. //执行签名
  242. EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  243.  
  244. m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
  245.  
  246. return true;
  247.  
  248. }
  249. #endregion
  250.  
  251. #region RSA 签名验证
  252.  
  253. public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
  254. {
  255.  
  256. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  257.  
  258. RSA.FromXmlString(p_strKeyPublic);
  259. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  260. //指定解密的时候HASH算法为MD5
  261. RSADeformatter.SetHashAlgorithm("MD5");
  262.  
  263. if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  264. {
  265. return true;
  266. }
  267. else
  268. {
  269. return false;
  270. }
  271.  
  272. }
  273.  
  274. public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData)
  275. {
  276.  
  277. byte[] HashbyteDeformatter;
  278.  
  279. HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
  280.  
  281. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  282.  
  283. RSA.FromXmlString(p_strKeyPublic);
  284. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  285. //指定解密的时候HASH算法为MD5
  286. RSADeformatter.SetHashAlgorithm("MD5");
  287.  
  288. if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  289. {
  290. return true;
  291. }
  292. else
  293. {
  294. return false;
  295. }
  296.  
  297. }
  298.  
  299. public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData)
  300. {
  301.  
  302. byte[] DeformatterData;
  303.  
  304. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  305.  
  306. RSA.FromXmlString(p_strKeyPublic);
  307. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  308. //指定解密的时候HASH算法为MD5
  309. RSADeformatter.SetHashAlgorithm("MD5");
  310.  
  311. DeformatterData =Convert.FromBase64String(p_strDeformatterData);
  312.  
  313. if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  314. {
  315. return true;
  316. }
  317. else
  318. {
  319. return false;
  320. }
  321.  
  322. }
  323.  
  324. public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
  325. {
  326.  
  327. byte[] DeformatterData;
  328. byte[] HashbyteDeformatter;
  329.  
  330. HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
  331. System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  332.  
  333. RSA.FromXmlString(p_strKeyPublic);
  334. System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  335. //指定解密的时候HASH算法为MD5
  336. RSADeformatter.SetHashAlgorithm("MD5");
  337.  
  338. DeformatterData =Convert.FromBase64String(p_strDeformatterData);
  339.  
  340. if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  341. {
  342. return true;
  343. }
  344. else
  345. {
  346. return false;
  347. }
  348.  
  349. }
  350.  
  351. #endregion
  352.  
  353. #endregion
  354.  
  355. }
  356. }

[DEncrypt] RSACryption--RSA加密/解密字符串 (转载)的更多相关文章

  1. RSA加密解密及RSA签名和验证及证书

    RSA加密解密及RSA签名和验证及证书 公钥是给别人的 发送密文使用公钥加密 验证签名使用公钥验证 私钥是自己保留的 接受密文使用私钥解密 发送签名使用私钥签名 上述过程逆转是不行的,比如使用私钥加密 ...

  2. RSA加密解密及RSA签名和验证

    原文:RSA加密解密及RSA签名和验证 1.RSA加密解密: (1)获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥 (2)加密 (3)解密2.RSA签名和验证 (1)获取密钥,这里是 ...

  3. 最通俗易懂的RSA加密解密指导

    前言 RSA加密算法是一种非对称加密算法,简单来说,就是加密时使用一个钥匙,解密时使用另一个钥匙. 因为加密的钥匙是公开的,所又称公钥,解密的钥匙是不公开的,所以称为私钥. 密钥 关于RSA加密有很多 ...

  4. C# Java间进行RSA加密解密交互

    原文:C# Java间进行RSA加密解密交互 这里,讲一下RSA算法加解密在C#和Java之间交互的问题,这两天纠结了很久,也看了很多其他人写的文章,颇受裨益,但没能解决我的实际问题,终于,还是被我捣 ...

  5. C# Java间进行RSA加密解密交互(二)

    原文:C# Java间进行RSA加密解密交互(二) 接着前面一篇文章C# Java间进行RSA加密解密交互,继续探讨这个问题. 在前面,虽然已经实现了C# Java间进行RSA加密解密交互,但是还是与 ...

  6. C# Java间进行RSA加密解密交互(三)

    原文:C# Java间进行RSA加密解密交互(三) 接着前面一篇C# Java间进行RSA加密解密交互(二)说吧,在上篇中为了实现 /** * RSA加密 * @param text--待加密的明文 ...

  7. RSA加密解密和读取公钥、私钥

    /// <summary>     /// RSA加密解密及RSA签名和验证    /// </summary>     public class RSADE    {    ...

  8. RSA加密解密及RSA加签验签

    RSA安全性应用场景说明 在刚接触RSA的时候,会混淆RSA加密解密和RSA加签验签的概念.简单来说加密解密是公钥加密私钥解密,持有公钥(多人持有)可以对数据加密,但是只有持有私钥(一人持有)才可以解 ...

  9. RSA加密解密算法

    /** * RSA加密解密算法 * Class Rsa */ class Rsa { /** * 获取pem格式的公钥 * @param $public_key 公钥文件路径或者字符串 * @retu ...

随机推荐

  1. [LeetCode#218] The Skyline Problem

    Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that ...

  2. statspack系列3

    原文:http://jonathanlewis.wordpress.com/2006/12/27/analysing-statspack-3/ 作者:Jonathan Lewis 下面的例子中的结果并 ...

  3. Android Loader详解三:重启与回调

    重启装载器 当你使用initLoader()时,如果指定ID的装载器已经存在,则它使用这个装载器.如果不存在呢,它将创建一个新的.但是有时你却是想丢弃旧的然后开始新的数据. 要想丢弃旧数据,你应使用r ...

  4. RobotFramework+Selenium2library+Appium+Python+RIDE安装指南

    最近在测试APP+WEB,想找一个好的自动化测试工具.然后发现RIDE这工具,框架比较自由,支持中文,有测试报告. 一个好的自动化测试就应该包含:Case管理+脚本的编写+自动生产报告. 如此一想,这 ...

  5. AppStore IAP 客户端校验代码

    -(BOOL)putStringToItunes:(SKPaymentTransaction*)transaction { NSData * iapData = transaction.transac ...

  6. Python的模块,模块的使用、安装,别名,作用域等概念

    所谓的模块就是将不同功能的函数分别放到不同的文件中,这样不仅有利于函数的维护,也方便了函数的调用.在Python中,一个.py文件就是一个模块(Module). 在模块的上层有一个叫做包(Packag ...

  7. C++ 把输出结果写入文件/从文件中读取数据

    先包含头文文件 #include<fstream> 输出到文件 ofstream fout;  //声明一个输出流对象 fout.open("output.txt"); ...

  8. Yii rabc角色权限管理文章推荐

    yii的这个rbac太通用,太灵活,有时候理解起来有困难.也是初学这个,推荐一个不错的文章:http://www.yiiframework.com/wiki/136/getting-to-unders ...

  9. 利用URL重写实现参数目录化

    参数目录化,就是将 类似 http://www.abc.com/store/store.aspx?id=1024 这样的网址,对外改为 http://www.abc.com/1024. 要实现这种功能 ...

  10. 加速数组操作(Array)

    Measure-Command { $ar = @() for ($x=0; $x -lt 10000; $x++) { $ar += $x } }执行结果:3.301s Measure-Comman ...