从网上找来的代码,顺手改改,用起来更方便。

配置文件

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Xml.Serialization;
  6.  
  7. namespace Encoder
  8. {
  9. [Serializable]
  10. public class Cfg
  11. {
  12. public string Pub = "";
  13. public string Pri = "";
  14.  
  15. public void Creat()
  16. {
  17. RSAKey keys = Encoder.CreateRSAKey();
  18. Pub = keys.PrivateKey;
  19. Pri = keys.PublicKey;
  20. }
  21.  
  22. public void Save()
  23. {
  24. try
  25. {
  26. string fileName = "cfg.txt";//文件名称与路径
  27. using (Stream fStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
  28. {
  29. XmlSerializer xmlFormat = new XmlSerializer(typeof(Cfg));//创建XML序列化器,需要指定对象的类型
  30. xmlFormat.Serialize(fStream, this);
  31. fStream.Close();
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. throw ex;
  37. }
  38. }
  39.  
  40. public bool Read()
  41. {
  42. try
  43. {
  44. string fileName = "cfg.txt";//文件名称与路径
  45. using (Stream fStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
  46. {
  47. XmlSerializer xmlFormat = new XmlSerializer(typeof(Cfg));//创建XML序列化器,需要指定对象的类型
  48. Cfg c = (Cfg)xmlFormat.Deserialize(fStream);
  49. this.Pub = c.Pub;
  50. this.Pri = c.Pri;
  51. fStream.Close();
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. //throw ex;
  57. }
  58. if (Pub != "" && Pri != "")
  59. return true;
  60. return false;
  61.  
  62. }
  63.  
  64. }
  65. }

  

生成密钥

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace Encoder
  10. {
  11. public partial class CreateRSAKeysForm : Form
  12. {
  13. public CreateRSAKeysForm()
  14. {
  15. InitializeComponent();
  16. }
  17.  
  18. private void CreateRSAKeysForm_Load(object sender, EventArgs e)
  19. {
  20. Cfg c = new Cfg();
  21. c.Read();
  22. txtPrivateKey.Text = c.Pri;
  23. txtPublishKey.Text = c.Pub;
  24. }
  25.  
  26. private void button1_Click(object sender, EventArgs e)
  27. {
  28. RSAKey keys = Encoder.CreateRSAKey();
  29. this.txtPrivateKey.Text = keys.PrivateKey;
  30. this.txtPublishKey.Text = keys.PublicKey;
  31. }
  32.  
  33. private void button2_Click(object sender, EventArgs e)
  34. {
  35. if (txtPrivateKey.Text == "")
  36. {
  37. MessageBox.Show("先生成密钥。");
  38. return;
  39. }
  40. Cfg c = new Cfg();
  41. c.Pri = txtPrivateKey.Text;
  42. c.Pub = txtPublishKey.Text;
  43. try
  44. {
  45. c.Save();
  46. MessageBox.Show("保存成功。");
  47. }
  48. catch (Exception ex)
  49. {
  50. MessageBox.Show( "文件保存失败:"+ex.Message);
  51. }
  52. }
  53.  
  54. private void button3_Click(object sender, EventArgs e)
  55. {
  56. Cfg c = new Cfg();
  57. c.Read();
  58. txtPrivateKey.Text=c.Pri;
  59. txtPublishKey.Text=c.Pub;
  60. }
  61. }
  62. }

  加密解密算法,我没动哦

  1. /**
  2. * Author:张浩华
  3. */
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.Security.Cryptography;
  9. using System.IO;
  10. namespace Encoder
  11. {
  12. public class Encoder
  13. {
  14. #region DES 加(解)密。(对称加密)
  15.  
  16. /// <summary>
  17. /// DES 加密(对称加密)。使用密钥将明文加密成密文
  18. /// </summary>
  19. /// <param name="code">明文</param>
  20. /// <param name="sKey">密钥</param>
  21. /// <returns>密文</returns>
  22. public static string DESEncrypt(string code, string sKey)
  23. {
  24. /* 创建一个DES加密服务提供者 */
  25. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  26.  
  27. /* 将要加密的内容转换成一个Byte数组 */
  28. byte[] inputByteArray = Encoding.Default.GetBytes(code);
  29.  
  30. /* 设置密钥和初始化向量 */
  31. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  32. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  33.  
  34. /* 创建一个内存流对象 */
  35. MemoryStream ms = new MemoryStream();
  36.  
  37. /* 创建一个加密流对象 */
  38. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  39.  
  40. /* 将要加密的文本写到加密流中 */
  41. cs.Write(inputByteArray, 0, inputByteArray.Length);
  42.  
  43. /* 更新缓冲 */
  44. cs.FlushFinalBlock();
  45.  
  46. /* 获取加密过的文本 */
  47. StringBuilder ret = new StringBuilder();
  48. foreach (byte b in ms.ToArray())
  49. {
  50. ret.AppendFormat("{0:X2}", b);
  51. }
  52. /* 释放资源 */
  53. cs.Close();
  54. ms.Close();
  55.  
  56. /* 返回结果 */
  57. return ret.ToString();
  58. }
  59.  
  60. /// <summary>
  61. /// DES 解密(对称加密)。使用密钥将密文解码成明文
  62. /// </summary>
  63. /// <param name="code">密文</param>
  64. /// <param name="sKey">密钥</param>
  65. /// <returns>明文</returns>
  66. public static string DESDecrypt(string code, string sKey)
  67. {
  68. /* 创建一个DES加密服务提供者 */
  69. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  70.  
  71. /* 将要解密的内容转换成一个Byte数组 */
  72. byte[] inputByteArray = new byte[code.Length / 2];
  73.  
  74. for (int x = 0; x < code.Length / 2; x++)
  75. {
  76. int i = (Convert.ToInt32(code.Substring(x * 2, 2), 16));
  77. inputByteArray[x] = (byte)i;
  78. }
  79.  
  80. /* 设置密钥和初始化向量 */
  81. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  82. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  83.  
  84. /* 创建一个内存流对象 */
  85. MemoryStream ms = new MemoryStream();
  86.  
  87. /* 创建一个加密流对象 */
  88. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  89.  
  90. /* 将要解密的文本写到加密流中 */
  91. cs.Write(inputByteArray, 0, inputByteArray.Length);
  92.  
  93. /* 更新缓冲 */
  94. cs.FlushFinalBlock();
  95.  
  96. /* 返回结果 */
  97. return System.Text.Encoding.Default.GetString(ms.ToArray());
  98. }
  99.  
  100. #endregion
  101.  
  102. #region RSA 加(解)密。(不对称加密)
  103. /// <summary>
  104. /// 创建一对 RSA 密钥(公钥&私钥)。
  105. /// </summary>
  106. /// <returns></returns>
  107. public static RSAKey CreateRSAKey()
  108. {
  109. RSAKey rsaKey = new RSAKey(); //声明一个RSAKey对象
  110.  
  111. /* 创建一个RSA加密服务提供者 */
  112. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  113. rsaKey.PrivateKey = rsa.ToXmlString(true); //创建私钥
  114. rsaKey.PublicKey = rsa.ToXmlString(false); //创建公钥
  115.  
  116. return rsaKey; //返回结果
  117. }
  118.  
  119. /// <summary>
  120. /// RSA 加密(不对称加密)。使用公钥将明文加密成密文
  121. /// </summary>
  122. /// <param name="code">明文</param>
  123. /// <param name="key">公钥</param>
  124. /// <returns>密文</returns>
  125. public static string RSAEncrypt(string code, string key)
  126. {
  127. /* 将文本转换成byte数组 */
  128. byte[] source = Encoding.Default.GetBytes(code);
  129. byte[] ciphertext; //密文byte数组
  130.  
  131. /* 创建一个RSA加密服务提供者 */
  132. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  133. rsa.FromXmlString(key); //设置公钥
  134. ciphertext = rsa.Encrypt(source, false); //加密,得到byte数组
  135.  
  136. /* 对字符数组进行转码 */
  137. StringBuilder sb = new StringBuilder();
  138. foreach (byte b in ciphertext)
  139. {
  140. sb.AppendFormat("{0:X2}", b);
  141. }
  142. return sb.ToString(); //返回结果
  143. }
  144.  
  145. /// <summary>
  146. /// RSA 解密(不对称加密)。使用私钥将密文解密成明文
  147. /// </summary>
  148. /// <param name="code">密文</param>
  149. /// <param name="key">私钥</param>
  150. /// <returns>明文</returns>
  151. public static string RSADecrypt(string code, string key)
  152. {
  153. /* 将文本转换成byte数组 */
  154. byte[] ciphertext = new byte[code.Length / 2];
  155. for (int x = 0; x < code.Length / 2; x++)
  156. {
  157. int i = (Convert.ToInt32(code.Substring(x * 2, 2), 16));
  158. ciphertext[x] = (byte)i;
  159. }
  160. byte[] source; //原文byte数组
  161.  
  162. /* 创建一个RSA加密服务提供者 */
  163. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  164. rsa.FromXmlString(key); //设置私钥
  165. source = rsa.Decrypt(ciphertext, false); //解密,得到byte数组
  166.  
  167. return Encoding.Default.GetString(source); //返回结果
  168. }
  169.  
  170. #endregion
  171.  
  172. #region MD5 加密(散列码 Hash 加密)
  173. /// <summary>
  174. /// MD5 加密(散列码 Hash 加密)
  175. /// </summary>
  176. /// <param name="code">明文</param>
  177. /// <returns>密文</returns>
  178. public static string MD5Encrypt(string code)
  179. {
  180. /* 获取原文内容的byte数组 */
  181. byte[] sourceCode = Encoding.Default.GetBytes(code);
  182. byte[] targetCode; //声明用于获取目标内容的byte数组
  183.  
  184. /* 创建一个MD5加密服务提供者 */
  185. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  186. targetCode = md5.ComputeHash(sourceCode); //执行加密
  187.  
  188. /* 对字符数组进行转码 */
  189. StringBuilder sb = new StringBuilder();
  190. foreach (byte b in targetCode)
  191. {
  192. sb.AppendFormat("{0:X2}", b);
  193. }
  194.  
  195. return sb.ToString();
  196. }
  197. #endregion
  198. }
  199.  
  200. /// <summary>
  201. /// RSA 密钥。公钥&私钥
  202. /// </summary>
  203. public class RSAKey
  204. {
  205. public string PrivateKey { get; set; }
  206. public string PublicKey { get; set; }
  207. }
  208.  
  209. }

版本说明

=====================================================
v0.2 2016年6月18日11:52:47
by  李工 qq 1222698
1、增加rsa的密钥生成,保存,读取
2、调整明文和密文的框位置。
3、增加关于,版本说明
4、有net3.5改成net2.0
5、感谢原作者的慷慨源代码

=====================================================
v0.1 原作者:张浩华
下载地址:http://www.cnblogs.com/zhhh/archive/2013/04/13/3018437.html

本版本下载:---------点这里哦----------

未实现功能:aes加密,base64加密

c#加密解密源码,md5、des、rsa的更多相关文章

  1. AES在线加密解密-附AES128,192,256,CBC,CFB,ECB,OFB,PCBC各种加密解密源码

    一.AES在线加密解密:AES 128/192/256位CBC/CFB/ECB/OFB/PCBC在线加密解密|在线工具|在线助手|在线生成|在线制作 http://www.it399.com/aes ...

  2. des加密解密源码 C# key值问题

    公司协议安全需求.需要对传输内容做des.md5加密. 因为是新人.刚交给我这个任务的时候有点眩晕.就开始在网上找各种des加密的内容.因为不懂以为需要把原理也搞明白,最后误了时间.把自己也搞糊涂了. ...

  3. php代码加密|PHP源码加密——实现方法

    Encipher - PHP代码加密 | PHP源码加密下载地址:https://github.com/uniqid/encipher 该加密程序是用PHP代码写的,加密后代码无需任何附加扩展,无需安 ...

  4. NET实现RSA AES DES 字符串 加密解密以及SHA1 MD5加密

    本文列举了    数据加密算法(Data Encryption Algorithm,DEA) 密码学中的高级加密标准(Advanced EncryptionStandard,AES)RSA公钥加密算法 ...

  5. 学习笔记: MD5/DES/RSA三类加密,SSL协议解析

    1. 不对称可逆加密的 的2种用法 (1)保证信息不被篡改 (2) 保证信息只能被我看到 2. CA证书的基本原理 流程如下:  百度公司 向CA机构报备 持有者姓名, 有效期, 要发布的公钥 , 扩 ...

  6. hihttps教你在Wireshark中提取旁路https解密源码

    大家好,我是hihttps,专注SSL web安全研究,今天本文就是教大家怎样从wireshark源码中,提取旁路https解密的源码,非常值得学习和商业应用. 一.旁路https解密条件 众所周知, ...

  7. java 加密工具类(MD5、RSA、AES等加密方式)

    1.加密工具类encryption MD5加密 import org.apache.commons.codec.digest.DigestUtils; /** * MD5加密组件 * * @autho ...

  8. .Net(c#)加密解密之Aes和Des

    .Net(c#)加密解密工具类: /// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelp ...

  9. openssl之aes加密(源码分析 AES_encrypt 与 AES_cbc_encrypt ,加密模式)

    首先要了解AES加密是什么,以及几种加密模式的区别.之后才是编程.具体的编程案例,在下面的链接. openssl之aes加密(AES_cbc_encrypt 与 AES_encrypt 的编程案例) ...

随机推荐

  1. Python学习一

    安装时遇到的问题 安装anaconda3.0到D盘之后,配置好两个环境变量:D:\anaconda和D:\anaconda\Scripts.发现在命令行中执行python指令可以,但conda指令却是 ...

  2. nginx反向代理node.js获取客户端IP

    使用Nginx做node.js程序的反向代理,会有这么一个问题:在程序中获取的客户端IP永远是127.0.0.1 如果想要拿到真实的客户端IP改怎么办呢? 一.首先配置Nginx的反向代理 proxy ...

  3. Mysql--触发器的操作

    1.为什么使用触发器 2.创建触发器 2.1 创建有一条执行语句的触发器 例子:在数据库company中存在两个表对象:部门表(t_dept)和日记表(t_diary),创建触发器实现向部门表中插入记 ...

  4. CCF-201312-2-ISBN号码

    问题描述 试题编号: 201312-2 试题名称: ISBN号码 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包 ...

  5. Android 项目使用TensorFlow

    Android 项目使用TensorFlow 首先需要搭建TensorFlow编译环境 参考:(http://www.cnblogs.com/dyufei/p/8027764.html) Tensor ...

  6. C语言socket编程----实现UDP通信

    TCP/IP协议叫做传输控制/网际协议,又叫做网络通信协议.实际上,它包括上百个功能的协议. 套接字(socket):在网络中用来描述计算机中不同程序与其他计算程序的通信方式. 套接字分为三类; 流式 ...

  7. 计算器的单元测试dome

    通过上一篇文章,Appium环境已经搭建好了,下面我们通过Python脚本来测试原生的计算器程序 --------------------------------------------------- ...

  8. Foxmail 7.0破解版,拷贝到新机器后,发送邮件乱码问题

    申请了新机器,挺开心,键盘和鼠标也好用了,但是新机器也随之而来一些不便,以前存储的数据需要重新拷贝.还有一些邮件,有些邮件标记了*号. Foxmail7.0绿色版本还挺好用,直接拷贝到新机器上就能直接 ...

  9. netty使用从0到1

    本周强总在组内做了netty分享,内容相当不错,趁着这次分享记录的,以及以前研究,进行一下记录. java io形式存在三种,一种是BIO传统IO是阻塞IO,面向字符.字节服务都属于这一种.NIO官方 ...

  10. java内存模型个人理解总结

    现阶段线程之间的通讯主要有两种:内存共享和消息传递,而且在java中是采用的内存共享.简单说下内存共享: 假设现在有a线程和b线程,在a和b线程之间的通讯是依靠a线程将相关数据刷新到共享内存,然后b线 ...