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

📄 form1.cs

📁 本源码为C#编程
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security; 
using System.Security.Cryptography; 
using System.IO; 
using System.Text; 

namespace 文件加密
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		private string key="1234567890"; //默认密钥 

		private byte[] sKey;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.RadioButton radioButton1;
		private System.Windows.Forms.RadioButton radioButton2; 
		private byte[] sIV; 

		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.textBox1 = 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.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.button4 = new System.Windows.Forms.Button();
			this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.radioButton2 = new System.Windows.Forms.RadioButton();
			this.radioButton1 = new System.Windows.Forms.RadioButton();
			this.button5 = new System.Windows.Forms.Button();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 72);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "输入路径:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(80, 72);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(160, 21);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(248, 72);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(48, 23);
			this.button1.TabIndex = 2;
			this.button1.Text = "Open";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(24, 160);
			this.button2.Name = "button2";
			this.button2.TabIndex = 3;
			this.button2.Text = "加密";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Enabled = false;
			this.button3.Location = new System.Drawing.Point(128, 160);
			this.button3.Name = "button3";
			this.button3.TabIndex = 4;
			this.button3.Text = "解密";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 120);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 23);
			this.label2.TabIndex = 5;
			this.label2.Text = "输出路径:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(80, 120);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(160, 21);
			this.textBox2.TabIndex = 6;
			this.textBox2.Text = "";
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(248, 120);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(48, 23);
			this.button4.TabIndex = 7;
			this.button4.Text = "Save";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.radioButton2);
			this.groupBox1.Controls.Add(this.radioButton1);
			this.groupBox1.Controls.Add(this.button5);
			this.groupBox1.Controls.Add(this.button4);
			this.groupBox1.Controls.Add(this.textBox1);
			this.groupBox1.Controls.Add(this.button1);
			this.groupBox1.Controls.Add(this.textBox2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.button2);
			this.groupBox1.Controls.Add(this.button3);
			this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.groupBox1.Location = new System.Drawing.Point(0, 0);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(328, 213);
			this.groupBox1.TabIndex = 8;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "加密解密文件";
			// 
			// radioButton2
			// 
			this.radioButton2.Location = new System.Drawing.Point(176, 24);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.TabIndex = 10;
			this.radioButton2.Text = "解密文件";
			this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
			// 
			// radioButton1
			// 
			this.radioButton1.Checked = true;
			this.radioButton1.Location = new System.Drawing.Point(48, 24);
			this.radioButton1.Name = "radioButton1";
			this.radioButton1.TabIndex = 9;
			this.radioButton1.TabStop = true;
			this.radioButton1.Text = "加密文件";
			this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
			// 
			// button5
			// 
			this.button5.Location = new System.Drawing.Point(224, 160);
			this.button5.Name = "button5";
			this.button5.TabIndex = 8;
			this.button5.Text = "关闭";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 191);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(328, 22);
			this.statusBar1.TabIndex = 9;
			this.statusBar1.Visible = false;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(328, 213);
			this.Controls.Add(this.statusBar1);
			this.Controls.Add(this.groupBox1);
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "文件加密与解密";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		#region 解密
		private void button3_Click(object sender, System.EventArgs e)
		{
			if(this.textBox1.Text!=""&&this.textBox2.Text!="")
			{
				statusBar1.Visible=true;
				statusBar1.Text="正在解密文件,请等待.....";
				
				
				
				if(DecryptFile(this.textBox1.Text,this.textBox2.Text,"123456"))
				{
					statusBar1.Text="解密完成。";
					
					MessageBox.Show("解密成功!");
					
				}
				statusBar1.Visible=false;
				

			}
			else
			{
				MessageBox.Show("文件路径不能为空");
			}
			

		}
		#endregion

		#region 加密
		private void button2_Click(object sender, System.EventArgs e)
		{
			if(this.textBox1.Text!=""&&this.textBox2.Text!="")
			{
				
				statusBar1.Visible=true;
				statusBar1.Text="正在加密文件,请等待.....";
				
				
			  
				if(EncryptFile(this.textBox1.Text,this.textBox2.Text,"123456"))
				{
					statusBar1.Text="加密完成。";
					MessageBox.Show("加密成功!");
				}
				statusBar1.Visible=false;
				
			}
			else
			{
				MessageBox.Show("文件路径不能为空");
			}
		
		}
		#endregion

		#region 打开保存文件
		private void button1_Click(object sender, System.EventArgs e)
		{
			openFileDialog1.ShowDialog();
			string filename=openFileDialog1.FileName;
			int i=filename.LastIndexOf(".");
			if(i!=-1)
			{
				string filetype=filename.Substring(filename.LastIndexOf("."));
				if(this.radioButton1.Checked)
				{
					filename=filename.Replace("(解密文件)","");
					textBox2.Text=filename.Substring(0,filename.LastIndexOf("."))+"(加密文件)"+filetype;
				}
				else
				{
					filename=filename.Replace("(加密文件)","");
					textBox2.Text=filename.Substring(0,filename.LastIndexOf("."))+"(解密文件)"+filetype;

				}
			}
			if(filename!="")
			{
				textBox1.Text=openFileDialog1.FileName;
				
			}
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			this.saveFileDialog1.ShowDialog();
			if(saveFileDialog1.FileName!="")
			{
				this.textBox2.Text=saveFileDialog1.FileName;
			}

		} 
		#endregion

		#region 加密方法
		public bool EncryptFile(string filePath,string savePath,string keyStr) 
		{ 
			DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
			if(keyStr=="") 
				keyStr=key; 
			FileStream fs=File.OpenRead(filePath); 
			byte[] inputByteArray =new byte[fs.Length]; 
			fs.Read(inputByteArray,0,(int)fs.Length); 
			fs.Close(); 
			byte[] keyByteArray=Encoding.Default.GetBytes(keyStr); 
			SHA1 ha=new SHA1Managed(); 
			byte[] hb=ha.ComputeHash(keyByteArray); 
			sKey=new byte[8]; 
			sIV=new byte[8]; 
			for(int i=0;i<8;i++) 
				sKey[i]=hb[i]; 
			for(int i=8;i<16;i++) 
				sIV[i-8]=hb[i]; 
			des.Key=sKey; 
			des.IV=sIV; 
			MemoryStream ms = new MemoryStream(); 
			CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write); 
			cs.Write(inputByteArray, 0, inputByteArray.Length); 
			cs.FlushFinalBlock(); 
			fs=File.OpenWrite(savePath); 
			
			
			
			foreach(byte b in ms.ToArray()) 
			{ 
				fs.WriteByte(b); 
				
			} 
       
			
			fs.Close(); 
			cs.Close(); 
			ms.Close(); 
			return true; 
		}
		#endregion

		#region 解密方法
		public bool DecryptFile(string filePath,string savePath,string keyStr) 
		{ 
			DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
			if(keyStr=="") 
				keyStr=key; 
			FileStream fs=File.OpenRead(filePath); 
			byte[] inputByteArray =new byte[fs.Length]; 
			fs.Read(inputByteArray,0,(int)fs.Length); 
			fs.Close(); 
			byte[] keyByteArray=Encoding.Default.GetBytes(keyStr); 
			SHA1 ha=new SHA1Managed(); 
			byte[] hb=ha.ComputeHash(keyByteArray); 
			sKey=new byte[8]; 
			sIV=new byte[8]; 
			for(int i=0;i<8;i++) 
				sKey[i]=hb[i]; 
			for(int i=8;i<16;i++) 
				sIV[i-8]=hb[i]; 
			des.Key=sKey; 
			des.IV=sIV; 
			MemoryStream ms = new MemoryStream(); 
			CryptoStream cs = new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write); 
			cs.Write(inputByteArray, 0, inputByteArray.Length); 
			cs.FlushFinalBlock(); 
			fs=File.OpenWrite(savePath); 
			foreach(byte b in ms.ToArray()) 
			{ 
				fs.WriteByte(b); 
			} 
			fs.Close(); 
			cs.Close(); 
			ms.Close(); 
			return true; 
		} 
		#endregion

		#region 关闭
		private void button5_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
		#endregion

		#region 单选按钮
		private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
		{
			if(radioButton1.Checked==true)
			{
				this.button2.Enabled=true;
				this.button3.Enabled=false;
			}
		}

		private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
		{
			if(radioButton2.Checked==true)
			{
				this.button2.Enabled=false;
				this.button3.Enabled=true;
			}
		
		}
		#endregion




		
	}
}

⌨️ 快捷键说明

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