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

📄 form1.cs

📁 AES加密和解密源码!
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
using AES类库;

namespace AES加密
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
        private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.Button button6;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		public string s0,s1,s2,s3,s4;
		public byte[]key1=new byte[16];
		public byte[]b1 =new byte[16];
		public byte[]b2 =new byte[16];
        public byte[]b3 =new byte[16];
        public byte[]b4 =new byte[16];
		public char[]c0 ;
		public char[]c1 =new char[16];
		public char[]c2 =new char[16];


		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.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.button6 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label1.Location = new System.Drawing.Point(184, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(168, 32);
			this.label1.TabIndex = 0;
			this.label1.Text = "AES 加 密";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(24, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 24);
			this.label2.TabIndex = 1;
			this.label2.Text = "密钥:";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(24, 88);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(480, 21);
			this.textBox1.TabIndex = 2;
			this.textBox1.Text = "";
			this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 136);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(72, 24);
			this.label3.TabIndex = 3;
			this.label3.Text = "原文路径:";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 177);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 24);
			this.label4.TabIndex = 4;
			this.label4.Text = "加密路径:";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(24, 259);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(72, 24);
			this.label5.TabIndex = 5;
			this.label5.Text = "密文路径:";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(24, 300);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(72, 24);
			this.label6.TabIndex = 6;
			this.label6.Text = "解密路径:";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(112, 136);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(320, 21);
			this.textBox2.TabIndex = 7;
			this.textBox2.Text = "";
			this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(112, 176);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(320, 21);
			this.textBox3.TabIndex = 8;
			this.textBox3.Text = "";
			this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(112, 264);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(320, 21);
			this.textBox4.TabIndex = 9;
			this.textBox4.Text = "";
			this.textBox4.TextChanged += new System.EventHandler(this.textBox4_TextChanged);
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(112, 304);
			this.textBox5.Name = "textBox5";
			this.textBox5.Size = new System.Drawing.Size(320, 21);
			this.textBox5.TabIndex = 10;
			this.textBox5.Text = "";
			this.textBox5.TextChanged += new System.EventHandler(this.textBox5_TextChanged);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(448, 136);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(56, 24);
			this.button1.TabIndex = 11;
			this.button1.Text = "浏 览";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(448, 177);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(56, 24);
			this.button2.TabIndex = 12;
			this.button2.Text = "浏 览";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(448, 218);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(56, 24);
			this.button3.TabIndex = 13;
			this.button3.Text = "加 密";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(448, 259);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(56, 24);
			this.button4.TabIndex = 14;
			this.button4.Text = "浏 览";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button5
			// 
			this.button5.Location = new System.Drawing.Point(448, 300);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(56, 24);
			this.button5.TabIndex = 15;
			this.button5.Text = "浏 览";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// button6
			// 
			this.button6.Location = new System.Drawing.Point(448, 341);
			this.button6.Name = "button6";
			this.button6.Size = new System.Drawing.Size(56, 24);
			this.button6.TabIndex = 16;
			this.button6.Text = "解 密";
			this.button6.Click += new System.EventHandler(this.button6_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(536, 381);
			this.Controls.Add(this.button6);
			this.Controls.Add(this.button5);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox5);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.label6);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Name = "Form1";
			this.Text = "AES算法";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

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

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

		private void button1_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog f=new OpenFileDialog();
			f.Title="原文文件位置!";
			if(f.ShowDialog()==DialogResult.OK)
			{
				textBox2.Text=f.FileName;
			}
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog f=new OpenFileDialog();
			f.Title="密文文件位置!";
			if(f.ShowDialog()==DialogResult.OK)
			{
				textBox4.Text=f.FileName ;
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			SaveFileDialog f=new SaveFileDialog();
			f.Title="加密后文件位置!";
			f.Filter="txt files(*.txt)|*.txt|Word文档(*.doc)|*.doc|pdf文件(*.pdf)|*.pdf";
			if(f.ShowDialog()==DialogResult.OK)
			{  
				textBox3.Text=f.FileName+".aes";
			}
		}

		private void button5_Click(object sender, System.EventArgs e)
		{
			SaveFileDialog f=new SaveFileDialog();
			f.Title="解密后文件位置!";
			f.Filter="txt files(*.txt)|*.txt|Word文档(*.doc)|*.doc|pdf文件(*.pdf)|*.pdf";
			if(f.ShowDialog()==DialogResult.OK)
			{  
				textBox5.Text=f.FileName ;
			}
		}

//************************************获取密钥并转换为字节*****************************************

		private void textBox1_TextChanged(object sender, System.EventArgs e)
		{  
                s0=textBox1.Text;
			    c0=s0.ToCharArray();
			if(c0.Length==16)
			{	
				Encoder q=Encoding.UTF8.GetEncoder();
			    q.GetBytes(c0,0,c0.Length,key1,0,true);
			}
			
		}

//*************************************************************************************************

//************************************调用AES加密算法**********************************************
		private void button3_Click(object sender, System.EventArgs e)
		{
			if((textBox1.Text=="")||(textBox2.Text=="")||(textBox3.Text==""))
				MessageBox.Show("请输入原文路径、密钥和保存路径!");	
			else if (!File.Exists(s1))
				MessageBox.Show("原文文件不存在!");
	        else if (c0.Length!=16)
				{
					MessageBox.Show("密钥长度不对,请输入128位的密钥!");
			    }
			else   
			{ 
				FileStream a=new FileStream(s1,FileMode.Open);
				FileStream b=new FileStream(s3,FileMode.Create);
				if (a.Length%16!=0)                                           //求出原文件长,不足128位的补1
				{
					byte[]add=new byte[16-a.Length%16];
					for(int i=0;i<16-a.Length%16;i++)
					{
						add[i]=0x01;
					}
					a.Seek(0,SeekOrigin.End);
					a.Write(add,0,15-(int)(a.Length%16));
				}

				for(int i=0;i<a.Length;i+=16)
				{
				    a.Seek(i,SeekOrigin.Begin);
				    a.Read(b1,0,16);
					AES加密算法 f=new AES加密算法(b1,key1,ref b2);
					b.Seek(i,SeekOrigin.Begin);
					b.Write(b2,0,16);
				}
				MessageBox.Show("成功加密!");
			}                                     
		
		}
//*************************************************************************************************

//************************************调用AES解密算法**********************************************
		private void button6_Click(object sender, System.EventArgs e)
		{
			if((textBox1.Text=="")||(textBox4.Text=="")||(textBox5.Text==""))
				MessageBox.Show("请输入密文路径、密钥和保存路径!");	
			else if (!File.Exists(s2))
				MessageBox.Show("密文文件不存在!");
			else if (c0.Length!=16)
			{
				MessageBox.Show("密钥长度不对,请输入128位的密钥!");
			}
			else  
			{
				FileStream c=new FileStream(s2,FileMode.Open);
				FileStream d=new FileStream(s4,FileMode.Create);
				for(int i=0;i<c.Length;i+=16)
				{
					c.Seek(i,SeekOrigin.Begin);
					c.Read(b3,0,16);
					AES解密算法 f=new AES解密算法(b3,key1,ref b4);
					d.Seek(i,SeekOrigin.Begin);
					d.Write(b4,0,16);
				}
				MessageBox.Show("成功解密!");
			}                                       
		
		}
//*************************************************************************************************

		private void textBox2_TextChanged(object sender, System.EventArgs e)
		{
			s1=textBox2.Text;
		
		}

		private void textBox4_TextChanged(object sender, System.EventArgs e)
		{
			s2=textBox4.Text;
		}

		private void textBox3_TextChanged(object sender, System.EventArgs e)
		{
			s3=textBox3.Text;
		}

		private void textBox5_TextChanged(object sender, System.EventArgs e)
		{
			s4=textBox5.Text;
		}
	}
}

⌨️ 快捷键说明

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