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

📄 form1.cs

📁 我用C#做的一个对字符串加密的程序
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace 文件和加密和解密处理
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private static Form1  form1;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem8;
		private System.Windows.Forms.MenuItem menuItem9;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox2;
		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.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.Label label5;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		static int ccc;

		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.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem8 = new System.Windows.Forms.MenuItem();
			this.menuItem9 = new System.Windows.Forms.MenuItem();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox2 = 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.textBox4 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.label5 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem8});
			// 
			// menuItem8
			// 
			this.menuItem8.Index = 0;
			this.menuItem8.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem9});
			this.menuItem8.Text = "帮助";
			// 
			// menuItem9
			// 
			this.menuItem9.Index = 0;
			this.menuItem9.Text = "程序说明";
			this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(56, 59);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "原  码";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(136, 56);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(392, 23);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(56, 99);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "密  码";
			// 
			// textBox2
			// 
			this.textBox2.ForeColor = System.Drawing.Color.Black;
			this.textBox2.Location = new System.Drawing.Point(136, 96);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(392, 23);
			this.textBox2.TabIndex = 3;
			this.textBox2.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(120, 152);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(88, 32);
			this.button1.TabIndex = 4;
			this.button1.Text = "加 密";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(392, 152);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(88, 32);
			this.button2.TabIndex = 5;
			this.button2.Text = "退 出";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(392, 320);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(88, 32);
			this.button3.TabIndex = 11;
			this.button3.Text = "核 查";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(120, 320);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(88, 32);
			this.button4.TabIndex = 10;
			this.button4.Text = "解 密";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// textBox4
			// 
			this.textBox4.ForeColor = System.Drawing.Color.Black;
			this.textBox4.Location = new System.Drawing.Point(136, 264);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(392, 23);
			this.textBox4.TabIndex = 9;
			this.textBox4.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(56, 267);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 16);
			this.label3.TabIndex = 8;
			this.label3.Text = "原  码";
			// 
			// textBox3
			// 
			this.textBox3.ForeColor = System.Drawing.Color.Black;
			this.textBox3.Location = new System.Drawing.Point(136, 224);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(392, 23);
			this.textBox3.TabIndex = 7;
			this.textBox3.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(56, 227);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 16);
			this.label4.TabIndex = 6;
			this.label4.Text = "密  码";
			// 
			// comboBox1
			// 
			this.comboBox1.Items.AddRange(new object[] {
														   "1",
														   "2",
														   "3",
														   "4",
														   "5",
														   "6"});
			this.comboBox1.Location = new System.Drawing.Point(348, 13);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(121, 22);
			this.comboBox1.TabIndex = 12;
			this.comboBox1.Text = "5";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(116, 16);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(200, 16);
			this.label5.TabIndex = 13;
			this.label5.Text = "选择替代法要移动的位数";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(7, 16);
			this.ClientSize = new System.Drawing.Size(584, 375);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.comboBox1);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label1);
			this.Cursor = System.Windows.Forms.Cursors.Default;
			this.Font = new System.Drawing.Font("新宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.HelpButton = true;
			this.ImeMode = System.Windows.Forms.ImeMode.Alpha;
			this.MaximizeBox = false;
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "欢迎进入C#的世界!";
			this.TopMost = true;
			this.TransparencyKey = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255)));
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

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

		/*
		private void menuItem2_Click(object sender, System.EventArgs e)
		{
			//System.IO.Stream mystream;
			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			openFileDialog1.InitialDirectory="c:\\";
			openFileDialog1.Filter="txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
			openFileDialog1.FilterIndex=4;
			openFileDialog1.RestoreDirectory=true;
			if(openFileDialog1.ShowDialog()==DialogResult.OK)
			{
				if(openFileDialog1.FileName!="")
				{
					//textBox1.Text=openFileDialog1.FileName;
					//statusBar1.Text=openFileDialog1.FileName;
				}
			}
		}*/

		private void menuItem9_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("这是一个实现简单文件的加密和解密的程序,也是我的第一个C#程序!\n本程序主要实现传统的加密算法对字符串加密!", "信息提示",
				MessageBoxButtons.OK, MessageBoxIcon.Information);

		}

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

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

		private void button1_Click(object sender, System.EventArgs e)
		{
			string OriginalWord=textBox1.Text.Trim();
			char[] c=new char[OriginalWord.Length];
			c=OriginalWord.ToCharArray();
			ccc=Convert.ToInt32(comboBox1.SelectedItem);
			for(int i=0;i<c.Length;i++)
			{
				byte b = (byte)c[i];
				//b=(byte)(b+3);
				if ( (byte) (b + ccc) < (byte) 'a' && (byte) (b + ccc) > (byte) 'Z') 
				{
					b = (byte) (b + ccc);
					b = (byte) ( (byte) 'A' + (b - (byte) 'Z') - 1);
					c[i] = (char) b;
				}
				else if ( (byte) (b + ccc) > (byte) 'z') 
				{
					b = (byte) (b + ccc);
					b = (byte) ( (byte) 'a' + (b - (byte) 'z') - 1);
					c[i] = (char) b;
				}
				else if ( (byte) (b + ccc) > (byte) '9' && (byte) (b + ccc) < (byte) 'A') 
				{
					b = (byte) (b + ccc);
					b = (byte) ( (byte) '0' + (b - (byte) '9') - 1);
					c[i] = (char) b;
				}
				else if((b>=(byte)'a'&& b<=(byte)'z') || (b>=(byte)'0' && b<=(byte)'9') || (b>=(byte)'A' && b<=(byte)'Z'))
				{
					b = (byte) (b + ccc);
					c[i] = (char) (b);
				}
				else
				{
					//如果输入其它的字符,则提示出错,并且将相应的输入框内容清除
					MessageBox.Show("你输入了非法字符,请输入26个英文字母或0到9之间的字符","输入出错了",MessageBoxButtons.OK,MessageBoxIcon.Warning);
					textBox2.Text="";
					textBox3.Text="";
					textBox4.Text="";
					goto error1;
				}
			}
			string s=new string(c);
			textBox2.Text=s;
			error1:
			{
				//如果出错转到这里将密码和解密的两个textBox清除内容,完成此方法
			}
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			if(textBox2.Text.Trim()!="")
			{
				textBox3.Text=textBox2.Text.Trim();
				string sss=textBox3.Text.Trim();
				char[] c=new char[sss.Length];
				c=sss.ToCharArray();
				for(int i=0;i<sss.Length;i++)
				{
					byte b=(byte)c[i];
					if ( (byte) (b - ccc) < (byte) 'a' && (byte) (b - ccc) > (byte) 'Z') 
					{
						b = (byte) (b - ccc);
						b = (byte) ( (byte) 'z' - ((byte) 'a'- b )+1);
						c[i] = (char) b;
					}
					else if ( (byte) (b - ccc) < (byte) '0') 
					{
						b = (byte) (b - ccc);
						b = (byte) ( (byte) '9' - ((byte) '0'-b)+1);
						c[i] = (char) b;
					}
					else if ( (byte) (b - ccc) > (byte) '9' && (byte) (b - ccc) < (byte) 'A') 
					{
						b = (byte) (b - ccc);
						b = (byte) ( (byte) 'Z' - ((byte)'A'-b)+1);
						c[i] = (char) b;
					}
					else if((b>=(byte)'a'&& b<=(byte)'z') || (b>=(byte)'0' && b<=(byte)'9') || (b>=(byte)'A' && b<=(byte)'Z'))
					{
						b = (byte) (b - ccc);
						c[i] = (char) (b);
					}
				}
				textBox4.Text=(new string(c));
			}
			else
			{
				MessageBox.Show("没有要解的密码","错误提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
			}
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			if(textBox1.Text.Trim()=="")
			{
				MessageBox.Show("请输入要加密的字符串!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			else if(textBox1.Text==textBox4.Text)
			{
				MessageBox.Show(" 加密解密成功!!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			else
			{
				MessageBox.Show("对不起,发生了错误,加密减密没有成功,\n输入26个英文字母,或者0到9之间的数字\n有可能你输入了;符号,请将其删掉再加密","出错了",MessageBoxButtons.OK,MessageBoxIcon.Warning);
			}
		}
	}
}

⌨️ 快捷键说明

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