⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 c#开发的加密解密程序
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Security.Cryptography;
using System.Xml;

namespace StandSoftRSA
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.RichTextBox richTextBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.RichTextBox richTextBox2;
		private System.Windows.Forms.Button button3;
		private RSACryptoServiceProvider RSA;
		private byte[] Hashbyte;
		private byte[] EncryptedData;
		private RSAParameters PrivateKey;
		private RSAParameters PublicKey;
		private XmlDocument objXml;
		private XmlNodeList objXmlNode;
	
  
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.richTextBox1 = new System.Windows.Forms.RichTextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.richTextBox2 = new System.Windows.Forms.RichTextBox();
			this.button3 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// richTextBox1
			// 
			this.richTextBox1.BackColor = System.Drawing.Color.Black;
			this.richTextBox1.ForeColor = System.Drawing.Color.LawnGreen;
			this.richTextBox1.Location = new System.Drawing.Point(10, 9);
			this.richTextBox1.Name = "richTextBox1";
			this.richTextBox1.Size = new System.Drawing.Size(460, 86);
			this.richTextBox1.TabIndex = 0;
			this.richTextBox1.Text = "";
			this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(192, 112);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(67, 26);
			this.button1.TabIndex = 1;
			this.button1.Text = "哈稀码";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(10, 198);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(460, 26);
			this.button2.TabIndex = 3;
			this.button2.Text = "加密";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// richTextBox2
			// 
			this.richTextBox2.BackColor = System.Drawing.Color.Black;
			this.richTextBox2.ForeColor = System.Drawing.Color.LawnGreen;
			this.richTextBox2.Location = new System.Drawing.Point(10, 233);
			this.richTextBox2.Name = "richTextBox2";
			this.richTextBox2.Size = new System.Drawing.Size(460, 86);
			this.richTextBox2.TabIndex = 4;
			this.richTextBox2.Text = "";
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(10, 327);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(460, 26);
			this.button3.TabIndex = 5;
			this.button3.Text = "解密";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(480, 453);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.richTextBox2);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.richTextBox1);
			this.Name = "Form1";
			this.Text = "RSA&MD5";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		//获得文件的哈稀码
		private void GetFileHash()
		{
			FileStream objFile = File.OpenRead("c:\\hash.doc");
			HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
			Hashbyte = MD5.ComputeHash(objFile);
			objFile.Close();
			foreach(byte b in Hashbyte)
			{
				richTextBox1.AppendText(b.ToString()+" ");
			}
		}
    
		private void button1_Click(object sender, System.EventArgs e)
		{
			GetFileHash();
		}

		//设置公钥和私钥对(都包含)
		public void SetPrivateKey()
		{
			PrivateKey = new RSAParameters();
			objXml = new XmlDocument();
			objXml.Load("PublicKey.xml");
			//指定RSA参数Exponent的值(公钥)
			objXmlNode = objXml.SelectNodes("//Exponent");
			PrivateKey.Exponent = Convert.FromBase64String(objXmlNode[0].InnerXml.ToString());   
			//指定RSA参数Modulus的值(公钥)
			objXmlNode = objXml.SelectNodes("//Modulus");
			PrivateKey.Modulus = Convert.FromBase64String(objXmlNode[0].InnerXml.ToString());
			objXml = new XmlDocument();
			objXml.Load("PrivateKey.xml");
			//指定RSA参数D的值(私钥)
			objXmlNode = objXml.SelectNodes("//D");
			PrivateKey.D = Convert.FromBase64String(objXmlNode[0].InnerXml.ToString());
			//指定RSA参数DP的值(私钥)
			objXmlNode = objXml.SelectNodes("//DP");
			PrivateKey.DP = Convert.FromBase64String(objXmlNode[0].InnerXml.ToString());
			//指定RSA参数DQ的值(私钥)
			objXmlNode = objXml.SelectNodes("//DQ");
			PrivateKey.DQ = Convert.FromBase64String(objXmlNode[0].InnerXml.ToString());
			//指定RSA参数InverseQ的值(私钥)
			objXmlNode = objXml.SelectNodes("//InverseQ");
			PrivateKey.InverseQ = Convert.FromBase64String(objXmlNode[0].InnerXml.ToString());
			//指定RSA参数P的值(私钥)
			objXmlNode = objXml.SelectNodes("//P");
			PrivateKey.P = Convert.FromBase64String(objXmlNode[0].InnerXml.ToString());
			//指定RSA参数Q的值(私钥)
			objXmlNode = objXml.SelectNodes("//Q");
			PrivateKey.Q = Convert.FromBase64String(objXmlNode[0].InnerXml.ToString());   
		}

		//执行加密
		private void button2_Click(object sender, System.EventArgs e)
		{
			RSA = new RSACryptoServiceProvider();
			//设置私钥
			SetPrivateKey();
			//导入加密私钥
			RSA.ImportParameters(PrivateKey);
			RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
			//设置签名的算法为MD5
			RSAFormatter.SetHashAlgorithm("MD5");
			//执行签名
			EncryptedData = RSAFormatter.CreateSignature(Hashbyte);
			string strCode = "";
			//现实加密结果
			for(int i = 0; i < EncryptedData.Length; i++)
			{
				strCode += " " + EncryptedData[i].ToString();
			}
			richTextBox2.AppendText(strCode);
		}

		//设置公钥
		public void SetPublicKey()
		{
			PublicKey = new RSAParameters();
			objXml = new XmlDocument();
			objXml.Load("PublicKey.xml");
			//指定私钥Exponent的值
			objXmlNode = objXml.SelectNodes("//Exponent");
			PublicKey.Exponent = Convert.FromBase64String(objXmlNode[0].InnerXml.ToString());   
			//指定私钥Modulus的值
			objXmlNode = objXml.SelectNodes("//Modulus");
			PublicKey.Modulus = Convert.FromBase64String(objXmlNode[0].InnerXml.ToString());
		}
  

		//用公钥解密
		private void button3_Click(object sender, System.EventArgs e)
		{
			RSA = new RSACryptoServiceProvider();
			//设置公钥
			SetPublicKey();
			RSA.ImportParameters(PublicKey);
			RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
			//指定解密的时候HASH算法为MD5
			RSADeformatter.SetHashAlgorithm("MD5");
			if(RSADeformatter.VerifySignature(Hashbyte,EncryptedData))
			{
				MessageBox.Show("身份已经确认!","确认身份",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			else
			{
				MessageBox.Show("确认身份失败","确认身份",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}

		private void richTextBox1_TextChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -