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

📄 form1.cs

📁 对文件加解密
💻 CS
📖 第 1 页 / 共 2 页
字号:
			// 
			this.label6.Location = new System.Drawing.Point(32, 112);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(80, 23);
			this.label6.TabIndex = 14;
			this.label6.Text = "输入密码";
			// 
			// button7
			// 
			this.button7.Location = new System.Drawing.Point(280, 68);
			this.button7.Name = "button7";
			this.button7.TabIndex = 17;
			this.button7.Text = "浏览...";
			this.button7.Click += new System.EventHandler(this.button7_Click);
			// 
			// textBox6
			// 
			this.textBox6.Location = new System.Drawing.Point(144, 72);
			this.textBox6.Name = "textBox6";
			this.textBox6.Size = new System.Drawing.Size(120, 21);
			this.textBox6.TabIndex = 16;
			this.textBox6.Text = "";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(32, 72);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(88, 23);
			this.label7.TabIndex = 15;
			this.label7.Text = "输出文件名";
			// 
			// label8
			// 
			this.label8.ForeColor = System.Drawing.Color.Red;
			this.label8.Location = new System.Drawing.Point(24, 104);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(168, 23);
			this.label8.TabIndex = 12;
			this.label8.Text = "请注意:密码必须大于6位";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(440, 469);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox2,
																		  this.groupBox1});
			this.Name = "Form1";
			this.Text = "文件的加密解密";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void button3_Click(object sender, System.EventArgs e)
		{
			//获得待加密文件名
			string inName=this.textBox1.Text;
			//获得保存文件名
			string outName=this.textBox2.Text;

            //设定初始向量
			byte[]  desIV={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
			byte[]  desKey={};
            //根据密码算出密钥
			string keyString=this.textBox3.Text;
			if (keyString.Length>=8)
			{
				desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
				(byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
				(byte)keyString[6],(byte)keyString[7]};
			}
				    
			if (keyString.Length==6)
			{
				desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
								  (byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
								  0x07,0x08};
			}
			if (keyString.Length==7)
			{
				desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
								  (byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
								  (byte)keyString[6],0x07};
			
			}
		
			//创建文件流分别指向输入和输出文件
			FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
			FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
			fout.SetLength(0);
		    
			//每次的中间流.
			byte[] bin = new byte[100]; 
			//代表已经加密的流的大小
			int complete=0;
			//代表要加密文件总的大小
			long totlen = fin.Length; 
			//每次写入的大小
			int len;   
		
			//创建DES对象
			DES des = new DESCryptoServiceProvider();     
			//创建加密流	
			CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
		            
			//从输入文件中读取流,然后加密到输出文件中
			while(complete < totlen)
			{
				len = fin.Read(bin, 0, 100);
				encStream.Write(bin, 0, len);
				complete = complete + len;
			}
		    
			//关闭流
			encStream.Close();  
			fout.Close();
			fin.Close();                   

	    	MessageBox.Show("文件加密已经完成","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
	    }

		private void button5_Click(object sender, System.EventArgs e)
		{
			//获得要解密的文件名
			string inName=this.textBox5.Text;
			//获得要保存的文件名
			string outName=this.textBox6.Text;

			//设定初始向量
			byte[]  desIV={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
			byte[]  desKey={};
			//根据密码算出密钥
			string keyString=this.textBox7.Text;
			if (keyString.Length>=8)
			{
				desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
									 (byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
									 (byte)keyString[6],(byte)keyString[7]};
			}
				    
			if (keyString.Length==6)
			{
				desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
									 (byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
									 0x07,0x08};
			}
			if (keyString.Length==7)
			{
				desKey=new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
									 (byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
									 (byte)keyString[6],0x07};
			
			}

			try
			{

				//创建文件流分别指向输入和输出文件
				FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
				FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
				fout.SetLength(0);
       
				//每次的中间流.
				byte[] bin = new byte[100]; 
				//代表已经解密的流的大小
				int complete=0;
				//代表要解密文件总的大小
				long totlen = fin.Length; 
				//每次写入的大小
				int len;                   
                
				//创建DES对象
				DES des = new DESCryptoServiceProvider();     
				//创建解密流
				CryptoStream decStream = new  CryptoStream(fout, des.CreateDecryptor(desKey,desIV), CryptoStreamMode.Write);
                
 
				//从输入文件中读取流,然后解密到输出文件中
				while(complete < totlen)
				{
					len = fin.Read(bin, 0, 100);
					decStream.Write(bin, 0, len);
					complete=complete+len;
				}
 
				  //关闭流		
				decStream.Close();  
				fout.Close();
				fin.Close();     

				MessageBox.Show("文件解密已经完成","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			catch (Exception error)
			{
            //密码不正确的警告
			MessageBox.Show("操作有误: "+error.Message,"警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
			
			}
		
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog ofDialog=new OpenFileDialog();
			if (ofDialog.ShowDialog()==DialogResult.OK)
			{
				//将待加密文件的路径写到文本框
				this.textBox1.Text=ofDialog.FileName;
			};
		}

		private void button6_Click(object sender, System.EventArgs e)
		{
		
		
			SaveFileDialog sfDialog=new SaveFileDialog();
			//设置过滤器
			sfDialog.Filter="My encrypt file(*.mef)|*.mef|All files(*.*)|*.*";
			if (sfDialog.ShowDialog()==DialogResult.OK)
			{
			   //将加密文件的保存路径写到文本框
				this.textBox2.Text=sfDialog.FileName;
			}
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog ofDialog=new OpenFileDialog();
			//设置过滤器
			ofDialog.Filter="My encrypt file(*.mef)|*.mef|All files(*.*)|*.*";
			if (ofDialog.ShowDialog()==DialogResult.OK)
			{
				//将待解密文件的路径写到文本框
				this.textBox5.Text=ofDialog.FileName;
			};
		}

		private void button7_Click(object sender, System.EventArgs e)
		{
			SaveFileDialog sfDialog=new SaveFileDialog();
			if (sfDialog.ShowDialog()==DialogResult.OK)
			{
				//将解密文件的保存路径写到文本框
				this.textBox6.Text=sfDialog.FileName;
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			//确认密码是否输入正确
			if (this.textBox3.Text.Length<6 | this.textBox3.Text!=this.textBox4.Text)
			{
				MessageBox.Show("请选择正确的密码","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
			else
			{
				//确认加密文件名和保存文件名是否正确
				if (this.textBox1.Text=="")
				{
					MessageBox.Show("请选择加密文件","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
				}
				else
				{
					if (this.textBox2.Text=="")
					{
						MessageBox.Show("请输入保存文件名","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
					}			
					else
					{
					//现在可以进行加密了
					this.button3.Enabled=true;
					}
				}			
			}
		}
	}
}

⌨️ 快捷键说明

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