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

📄 form1.cs

📁 des加密 C#写的
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace myDES
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox4;
		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.TextBox Tkey;
		private System.Windows.Forms.Button button2;
		/// <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.textBox1 = new System.Windows.Forms.TextBox();
			this.Tkey = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.button2 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(72, 24);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(256, 21);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// Tkey
			// 
			this.Tkey.Location = new System.Drawing.Point(72, 64);
			this.Tkey.Name = "Tkey";
			this.Tkey.Size = new System.Drawing.Size(256, 21);
			this.Tkey.TabIndex = 1;
			this.Tkey.Text = "";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(72, 104);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(256, 21);
			this.textBox3.TabIndex = 2;
			this.textBox3.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(352, 24);
			this.button1.Name = "button1";
			this.button1.TabIndex = 3;
			this.button1.Text = " 加密";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(72, 144);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(256, 21);
			this.textBox4.TabIndex = 4;
			this.textBox4.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 64);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(40, 23);
			this.label1.TabIndex = 5;
			this.label1.Text = "密钥";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(24, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(40, 23);
			this.label2.TabIndex = 6;
			this.label2.Text = "明文";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 104);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(40, 23);
			this.label3.TabIndex = 7;
			this.label3.Text = "密文";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 144);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(40, 23);
			this.label4.TabIndex = 8;
			this.label4.Text = "明文";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(352, 80);
			this.button2.Name = "button2";
			this.button2.TabIndex = 9;
			this.button2.Text = "解密";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(512, 365);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.Tkey);
			this.Controls.Add(this.textBox1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void button1_Click(object sender, System.EventArgs e)
		{
			DES des = new DESCryptoServiceProvider();
			byte[]  inputByteArray  =  Encoding.Default.GetBytes(textBox1.Text);
			string key = Tkey.Text;
			int keylength;
			keylength = key.Length;
			if (keylength<8)
				for(int i=1; i<=(8-keylength); i++) key = key+' ';//补足8位
			else
				key = key.Substring(0,8);     //只取8位   
		    	
			des.Key  =  ASCIIEncoding.ASCII.GetBytes(key);  
			des.IV  =   ASCIIEncoding.ASCII.GetBytes(key); //初始化向量
			
			MemoryStream  ms  =  new  MemoryStream();      //内存流
			CryptoStream  cs  =  new  CryptoStream(ms,  des.CreateEncryptor(),CryptoStreamMode.Write);
			
			cs.Write(inputByteArray,  0,  inputByteArray.Length); //把数组写入流
			
			cs.FlushFinalBlock();  //更新缓冲区
			StringBuilder  ret  =  new  StringBuilder(); //定义可变字符串 
			foreach(byte  b  in  ms.ToArray())  
			{  
				ret.AppendFormat("{0:X2}",  b); //ms内的内容加到ret字符串中 
			
					
		     }  
			textBox3.Text = ret.ToString();    
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			DESCryptoServiceProvider  des  =  new  DESCryptoServiceProvider();  
			string pToDecrypt = textBox3.Text;  //取出密文
			byte[]  inputByteArray  =  new  byte[pToDecrypt.Length / 2];  
			
			for(int  x  =  0;  x  <  pToDecrypt.Length / 2;  x++)  
			{  
				int  i  =  (Convert.ToInt32(pToDecrypt.Substring(x  *  2,  2),  16));  
				
				inputByteArray[x]  =  (byte)i;  
			}  
			string key = Tkey.Text;
			int keylength;
			keylength = key.Length;
			if (keylength<8)
				for(int i=1; i<=(8-keylength); i++) key = key+' ';
			else
				key = key.Substring(0,8);
			des.Key  =  ASCIIEncoding.ASCII.GetBytes(key);  
			des.IV  =  ASCIIEncoding.ASCII.GetBytes(key);  
			MemoryStream  ms  =  new  MemoryStream();  
			CryptoStream  cs  =  new  CryptoStream(ms,  des.CreateDecryptor(),CryptoStreamMode.Write);  
			cs.Write(inputByteArray,  0,  inputByteArray.Length);  
			cs.FlushFinalBlock();  
			textBox4.Text = System.Text.Encoding.Default.GetString(ms.ToArray()); 
		 
		}

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

		
	}
}

⌨️ 快捷键说明

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