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

📄 form1.cs

📁 C#做的加密解密的东西
💻 CS
📖 第 1 页 / 共 4 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace jmjm
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private SymmetricAlgorithm mCSP;
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton radioButton2;
		private System.Windows.Forms.RadioButton optDES;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textKey;
		private System.Windows.Forms.TextBox textIv;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox textOriginal;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox textEncrypted;
		private System.Windows.Forms.TextBox text11;
		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.TextBox textPass;
		private System.Windows.Forms.TextBox textName;
		private System.Windows.Forms.Button button9;
		private System.Windows.Forms.Button button8;
		private System.Windows.Forms.Button button7;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Button button6;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox textMd52;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textMd51;
		private System.Windows.Forms.TextBox textHax;
		private System.Windows.Forms.TabPage tabPage3;
		private System.Windows.Forms.TextBox textSee;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Button button11;
		private System.Windows.Forms.Button button12;
		private System.Windows.Forms.Button button13;
		private System.Windows.Forms.Button button14;
		private System.Windows.Forms.RichTextBox richText1;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.RichTextBox richText3;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.RichTextBox richText2;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.Button button15;
		private System.Windows.Forms.Button button16;
		private System.Windows.Forms.Button button17;
		private System.Windows.Forms.Button button18;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Button button19;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Button button20;
		private System.Windows.Forms.RichTextBox richText4;
		private System.Windows.Forms.Button button21;
		private System.Windows.Forms.Button button22;
		private System.Windows.Forms.Button button23;
		private System.Windows.Forms.Button button24;
		private System.Windows.Forms.Button button25;

		private static RSACryptoServiceProvider crypt;
		private static string  privatekey; 
		private static string  publickey; 
		private static byte [] bytes;
		//private static string  publicinfo;
		//private static string  privateinfo;
		//private static string  readpublickey;
		//private static string  readprivatekey;
		//文件打开和是否读取钥匙的标记
		private bool openla = false;
		private bool savela = false;
		private bool readpubkey=false;
		private bool readprikey=false;

		private static string  publicK;
		private static string  privateK;

		private System.Windows.Forms.Button button26;
		private System.Windows.Forms.RichTextBox richTextBox1;
		private System.Windows.Forms.Button button27;
		private System.Windows.Forms.RichTextBox richTextBox2;
		private System.Windows.Forms.TextBox textPri;
		private System.Windows.Forms.TextBox textPub;
		private System.Windows.Forms.RichTextBox richText5;
		private System.Windows.Forms.Button button10;
		private System.Windows.Forms.Button button28;
		private System.Windows.Forms.Label label14;
		private System.Windows.Forms.TextBox textName1;
		private System.Windows.Forms.TextBox textPass1;
		private System.Windows.Forms.Label label15;
		/// <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 );
		}


		
		private void WriteTxt()
		{

		}

		//加密文本文件  
		private string EncryptFile( string textinfo, string filename )
		{     
			UTF8Encoding enc=new UTF8Encoding();   
			byte [] bytes=enc.GetBytes( textinfo );
			int blockSize=0;
			if(crypt.KeySize==1024)
				blockSize=16;
			else 
				blockSize=8;
			//同时写入到内存中去
			MemoryStream ms=new MemoryStream();
			//写入文件中去
			FileStream fs=new FileStream(@filename,FileMode.OpenOrCreate);
			byte[]rawblock,encryblock;
			for(int i=0;i<bytes.Length;i+=blockSize)
			{
				if((bytes.Length-i)>blockSize)
					rawblock=new byte[blockSize];
				else 
					rawblock =new byte[bytes.Length-i];
				Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
				encryblock=crypt.Encrypt(rawblock,false);    
				fs.Write(encryblock,0,encryblock.Length);
				//写入到内存中去
				ms.Write(encryblock,0,encryblock.Length);
			}
			//写入到内存中
			ms.Position = 0;
			byte [] decode = new byte[ms.Length];
			ms.Read(decode,0,(int)ms.Length);
			string decodeinfo = enc.GetString(decode,0,decode.Length);   
			fs.Flush();
			fs.Close(); 
			ms.Close();   
			return decodeinfo;
		}

		//解密文本文件
		private string DecryptFile(string encryptfile,string decryptfile)
		{
			//打开加密后的文件,读取文件内容
			FileStream fs=new FileStream(@encryptfile,FileMode.Open);
			byte [] bytes=new byte[fs.Length];
			fs.Read(bytes,0,(int)fs.Length);
			fs.Close();
			//先写入到内存
			MemoryStream ms=new MemoryStream();
			//最好写入到解密后的一个文件
			StreamWriter sw=new StreamWriter(decryptfile);
			int keySize=crypt.KeySize/8;
			byte[]rawblock,decryptblock;
			for(int i=0;i<bytes.Length;i+=keySize)
			{
				if( ( bytes.Length-i ) > keySize )
				{
					rawblock=new byte[keySize];
				}
				else
				{rawblock =new byte[bytes.Length-i];}

				Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
				decryptblock = crypt.Decrypt( rawblock,false );
				ms.Write(decryptblock,0,decryptblock.Length);
			}
			ms.Position=0;
			byte [] decode=new byte[ms.Length];
			ms.Read(decode,0,(int)ms.Length);
			UTF8Encoding enc=new UTF8Encoding();
			string text=enc.GetString(decode);
			//写入到内存中去
			sw.Write(text);
			sw.Flush();  
			ms.Close();
			sw.Close();
			return text; 
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.richTextBox2 = new System.Windows.Forms.RichTextBox();
			this.button26 = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label10 = new System.Windows.Forms.Label();
			this.radioButton2 = new System.Windows.Forms.RadioButton();
			this.optDES = new System.Windows.Forms.RadioButton();
			this.button4 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.textKey = new System.Windows.Forms.TextBox();
			this.textIv = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.textOriginal = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.textEncrypted = new System.Windows.Forms.TextBox();
			this.text11 = new System.Windows.Forms.TextBox();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.button27 = new System.Windows.Forms.Button();
			this.richTextBox1 = new System.Windows.Forms.RichTextBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label14 = new System.Windows.Forms.Label();
			this.textPass1 = new System.Windows.Forms.TextBox();
			this.textName1 = new System.Windows.Forms.TextBox();
			this.button28 = new System.Windows.Forms.Button();
			this.button10 = new System.Windows.Forms.Button();
			this.label9 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.textPass = new System.Windows.Forms.TextBox();
			this.textName = new System.Windows.Forms.TextBox();
			this.button9 = new System.Windows.Forms.Button();
			this.button8 = new System.Windows.Forms.Button();
			this.button7 = new System.Windows.Forms.Button();
			this.label7 = new System.Windows.Forms.Label();
			this.button6 = new System.Windows.Forms.Button();
			this.label6 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.textMd52 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.textMd51 = new System.Windows.Forms.TextBox();
			this.textHax = new System.Windows.Forms.TextBox();
			this.textSee = new System.Windows.Forms.TextBox();
			this.tabPage3 = new System.Windows.Forms.TabPage();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.richText5 = new System.Windows.Forms.RichTextBox();
			this.textPub = new System.Windows.Forms.TextBox();
			this.textPri = new System.Windows.Forms.TextBox();
			this.button21 = new System.Windows.Forms.Button();
			this.button22 = new System.Windows.Forms.Button();
			this.button23 = new System.Windows.Forms.Button();
			this.button24 = new System.Windows.Forms.Button();
			this.button25 = new System.Windows.Forms.Button();
			this.richText4 = new System.Windows.Forms.RichTextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.button19 = new System.Windows.Forms.Button();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.button20 = new System.Windows.Forms.Button();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.button15 = new System.Windows.Forms.Button();
			this.button16 = new System.Windows.Forms.Button();
			this.button17 = new System.Windows.Forms.Button();
			this.button18 = new System.Windows.Forms.Button();
			this.label11 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.richText3 = new System.Windows.Forms.RichTextBox();
			this.label12 = new System.Windows.Forms.Label();
			this.richText2 = new System.Windows.Forms.RichTextBox();
			this.label13 = new System.Windows.Forms.Label();
			this.richText1 = new System.Windows.Forms.RichTextBox();
			this.button11 = new System.Windows.Forms.Button();
			this.button12 = new System.Windows.Forms.Button();
			this.button13 = new System.Windows.Forms.Button();
			this.button14 = new System.Windows.Forms.Button();
			this.label15 = new System.Windows.Forms.Label();
			this.tabControl1.SuspendLayout();
			this.tabPage1.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.tabPage2.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.tabPage3.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.Add(this.tabPage1);
			this.tabControl1.Controls.Add(this.tabPage2);
			this.tabControl1.Controls.Add(this.tabPage3);
			this.tabControl1.Location = new System.Drawing.Point(0, 8);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(448, 352);
			this.tabControl1.TabIndex = 18;
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.Add(this.richTextBox2);
			this.tabPage1.Controls.Add(this.button26);
			this.tabPage1.Controls.Add(this.groupBox1);
			this.tabPage1.Location = new System.Drawing.Point(4, 21);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(440, 327);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "对称DES";
			// 
			// richTextBox2
			// 
			this.richTextBox2.Location = new System.Drawing.Point(320, 16);
			this.richTextBox2.Name = "richTextBox2";
			this.richTextBox2.Size = new System.Drawing.Size(112, 240);
			this.richTextBox2.TabIndex = 6;
			this.richTextBox2.Text = "";
			// 
			// button26
			// 
			this.button26.Location = new System.Drawing.Point(320, 264);
			this.button26.Name = "button26";
			this.button26.Size = new System.Drawing.Size(112, 56);
			this.button26.TabIndex = 5;
			this.button26.Text = "检验";
			this.button26.Click += new System.EventHandler(this.button26_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label10);
			this.groupBox1.Controls.Add(this.radioButton2);
			this.groupBox1.Controls.Add(this.optDES);
			this.groupBox1.Controls.Add(this.button4);
			this.groupBox1.Controls.Add(this.button3);
			this.groupBox1.Controls.Add(this.button2);
			this.groupBox1.Controls.Add(this.button5);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.textKey);
			this.groupBox1.Controls.Add(this.textIv);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.textOriginal);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.textEncrypted);
			this.groupBox1.Controls.Add(this.text11);
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(304, 312);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "对称类型";
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(8, 264);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(72, 16);
			this.label10.TabIndex = 17;
			this.label10.Text = "解密后内容";
			// 
			// radioButton2
			// 
			this.radioButton2.Location = new System.Drawing.Point(160, 16);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.Size = new System.Drawing.Size(88, 24);

⌨️ 快捷键说明

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