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

📄 form1.cs

📁 c#文件加密传输程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
			this.label7.TabIndex = 13;
			this.label7.Text = "解密密码:";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(8, 136);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(104, 16);
			this.label6.TabIndex = 12;
			this.label6.Text = "解密输出文件名:";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 104);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(96, 16);
			this.label5.TabIndex = 11;
			this.label5.Text = "已加密文件名:";
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(352, 168);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(80, 24);
			this.button4.TabIndex = 10;
			this.button4.Text = "解密文件";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(352, 96);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(80, 24);
			this.button3.TabIndex = 9;
			this.button3.Text = "选择文件";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button8
			// 
			this.button8.Location = new System.Drawing.Point(0, 0);
			this.button8.Name = "button8";
			this.button8.TabIndex = 0;
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
			// 
			// button5
			// 
			this.button5.Location = new System.Drawing.Point(240, 496);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(80, 24);
			this.button5.TabIndex = 2;
			this.button5.Text = "刷新";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// button6
			// 
			this.button6.Location = new System.Drawing.Point(376, 496);
			this.button6.Name = "button6";
			this.button6.Size = new System.Drawing.Size(80, 24);
			this.button6.TabIndex = 3;
			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(520, 541);
			this.Controls.Add(this.button6);
			this.Controls.Add(this.button5);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(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 button1_Click(object sender, System.EventArgs e)
		{//选择要加密的文件
			if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
				this.textBox1.Text = this.openFileDialog1.FileName;
		}

		private void button2_Click(object sender, System.EventArgs e)
		{//加密文件
			if (this.textBox1.Text == "")
			{
				MessageBox.Show("请选择被加密文件!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			if (this.textBox2.Text == "")
			{
				MessageBox.Show("请输入加密输出文件名!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			if (this.textBox3.Text == ""|this.textBox3.Text.Length < 6|this.textBox3.Text.Length > 8|this.textBox3.Text != this.textBox4.Text)
			{
				MessageBox.Show("密码不符合规定,请输入正确的密码!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			try
			{
				//加密读入文件名
				string myInfilename = this.textBox1.Text;
				//加密输出文件名
				string myOutfilename = this.textBox2.Text;
				//设置DES算法初始向量
				byte [] myDESIV = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
				byte [] myDESKEY = {};
            
				string mykeystring = this.textBox3.Text;
				//根据密码设置密钥大小
				if (mykeystring.Length == 6)
				{
					myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
											   (byte) mykeystring[4],(byte) mykeystring[5],0x07,0x08};
				}
				if (mykeystring.Length == 7)
				{
					myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
											   (byte) mykeystring[4],(byte) mykeystring[5],(byte)mykeystring[6],0x08};
				}
				if (mykeystring.Length >=8)
				{
					myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
											   (byte) mykeystring[4],(byte) mykeystring[5],(byte)mykeystring[6],(byte)mykeystring[7]};
				}
				//输入输出的文件流            
				FileStream myInfilestream = new FileStream(myInfilename,System.IO.FileMode.Open,System.IO.FileAccess.Read);
				FileStream myOutfilestream = new FileStream(myOutfilename,FileMode.OpenOrCreate,FileAccess.Write);
				myOutfilestream.SetLength(0);
				//加密文件的中间流
				byte [] Inerdata = new byte[100];
				//已加密文件流
				int completesize = 0;
				//总共要加密的文件流大小
				long myfilelength = myInfilestream.Length;
				//创建DES
				DES mydes = new System.Security.Cryptography.DESCryptoServiceProvider();
				//创建加密流
				CryptoStream Encrytstream = new CryptoStream(myOutfilestream,mydes.CreateEncryptor(myDESKEY,myDESIV),CryptoStreamMode.Write);

				while(completesize < myfilelength)
				{//每次写入加密文件的数据大小
					int length = myInfilestream.Read(Inerdata,0,100);
					Encrytstream.Write(Inerdata,0,length);
					completesize = completesize + length;
				}
				Encrytstream.Close();
				myOutfilestream.Close();
				myInfilestream.Close();
				MessageBox.Show("文件加密成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				this.textBox8.Text = this.textBox2.Text;
			}
			catch (Exception Err)
			{
				MessageBox.Show("文件加密操作有误: "+Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);			
			}	

		}

		private void button3_Click(object sender, System.EventArgs e)
		{//选择要解密的文件
			if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
				this.textBox5.Text = this.openFileDialog1.FileName;
		
		}

		private void button4_Click(object sender, System.EventArgs e)
		{//解密文件
			if (this.textBox5.Text == "")
			{
				MessageBox.Show("请选择要解密的0文件!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			if (this.textBox6.Text == "")
			{
				MessageBox.Show("请输入解密输出文件名!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			if (this.textBox7.Text.Length < 6|this.textBox7.Text.Length > 8)
			{
				MessageBox.Show("密码不符合规定,请输入正确的密码!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			try
			{
				//解密读入文件名
				string myInfilename = this.textBox5.Text;
				//解密输出文件名
				string myOutfilename = this.textBox6.Text;
				//设置DES算法初始向量
				byte [] myDESIV = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
				byte [] myDESKEY = {};
            
				string mykeystring = this.textBox7.Text;
				//根据密码设置密钥大小
				if (mykeystring.Length == 6)
				{
					myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
											   (byte) mykeystring[4],(byte) mykeystring[5],0x07,0x08};
				}
				if (mykeystring.Length == 7)
				{
					myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
											   (byte) mykeystring[4],(byte) mykeystring[5],(byte)mykeystring[6],0x08};
				}
				if (mykeystring.Length >=8)
				{
					myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
											   (byte) mykeystring[4],(byte) mykeystring[5],(byte)mykeystring[6],(byte)mykeystring[7]};
				}
				//输入输出的文件流            
				FileStream myInfilestream = new FileStream(myInfilename,System.IO.FileMode.Open,System.IO.FileAccess.Read);
				FileStream myOutfilestream = new FileStream(myOutfilename,FileMode.OpenOrCreate,FileAccess.Write);
				myOutfilestream.SetLength(0);
				//解密文件的中间流
				byte [] Inerdata = new byte[100];
				//已解密文件流
				int completesize = 0;
				//总共要解密的文件流大小
				long myfilelength = myInfilestream.Length;
				//创建DES
				DES mydes = new System.Security.Cryptography.DESCryptoServiceProvider();
				//创建加密流
				CryptoStream Decrytstream = new CryptoStream(myOutfilestream,mydes.CreateDecryptor(myDESKEY,myDESIV),CryptoStreamMode.Write);

				while(completesize < myfilelength)
				{//每次写入加密文件的数据大小
					int length = myInfilestream.Read(Inerdata,0,100);
					Decrytstream.Write(Inerdata,0,length);
					completesize = completesize + length;
				}
				Decrytstream.Close();
				myOutfilestream.Close();
				myInfilestream.Close();
				MessageBox.Show("文件解密成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			catch (Exception Err)
			{
				MessageBox.Show("文件解密操作有误: "+Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
//				flag = 1;
			}
		}

		private void button5_Click(object sender, System.EventArgs e)
		{//刷新对话框
			this.textBox1.Text = "";
			this.textBox2.Text = "";
			this.textBox3.Text = "";
			this.textBox4.Text = "";
			this.textBox5.Text = "";
			this.textBox6.Text = "";
			this.textBox7.Text = "";
			this.textBox8.Text = "";
			this.textBox9.Text = "//192.168.0.2/movie/";
			this.textBox10.Text = "";
			this.textBox11.Text = "//192.168.0.2/";
		}

		private void button6_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void button9_Click(object sender, System.EventArgs e)
		{//下载文件
			this.textBox5.Text = this.textBox10.Text;
			if(this.textBox10.Text==""|this.textBox11.Text=="")
			{
				MessageBox.Show("请输入你要下载的文件名字!","错误:", MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			else
			{

				WebClient MyClient=new WebClient();
				string URL=this.textBox11.Text;
				string SaveFileName=this.textBox10.Text;
				try
				{
					MyClient.DownloadFile(URL , SaveFileName);
					MessageBox.Show("文件下载成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
					this.textBox5.Text = this.textBox10.Text;
				}
				catch(Exception Err)
				{
					MessageBox.Show("文件下载失败!错误是:"+Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
					return;
				}
			}
		}

		private void button7_Click(object sender, System.EventArgs e)
		{//上传文件
			if(this.textBox8.Text==""|this.textBox9.Text=="")
			{
				MessageBox.Show("请输入你要上载的文件名字!","错误:", MessageBoxButtons.OK,MessageBoxIcon.Information);
				return;
			}
			else
			{
				/// 得到文件名,文件扩展名字,服务器路径
				string fileNamePath = textBox8.Text.Trim();
				string uriString = textBox9.Text.Trim();
				string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1); 
				string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
				if(uriString.EndsWith("/") == false) uriString = uriString + "/";

				uriString = uriString + fileName;

				WebClient MyClient=new WebClient();

				string URL=uriString;
				string FileName=fileName;

				MyClient.Credentials = CredentialCache.DefaultCredentials;

				// 要上传的文件
				FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
				BinaryReader r = new BinaryReader(fs);

				try
				{
					byte[] postArray = r.ReadBytes((int)fs.Length);
					Stream postStream = MyClient.OpenWrite(URL,"PUT");
					if(postStream.CanWrite)
					{
						postStream.Write(postArray,0,postArray.Length);
						MessageBox.Show("文件上载成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
						//return;
					}
					else
					{
						label1.Text = "文件目前不可写!";
					}
					postStream.Close();
				}
				catch(Exception Err)
				{
					MessageBox.Show("文件上载失败!错误是:"+Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
				}
//				finally
//				{
//					MessageBox.Show("\n服务器的回复为: \n"+System.Text.Encoding.ASCII.GetString(MyResponseArray));
				    //return;
//				}
			}
		}

		private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
		
		}

	}

}

⌨️ 快捷键说明

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