C # --- A program of asymmetric encryption

zhaozj2021-02-16  59

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Text; using System.Security.Cryptography;

A summary description of NameSpace asymmetric encrypted {///

/// form1. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.TabControl tabControl1; private System.Windows.Forms.TabPage tabPage1; private System.Windows.Forms.TabPage tabPage2; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.SaveFileDialog save; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.Label label2; private System.Windows.Forms.RichTextBox richtext2; private System.Windows.Forms.OpenFileDialog open; private System.Windows.Forms.Label label3; private System.Windows.Forms.RichTextBox richtext3; private System.Windows.Forms.RichTextBox richtext; private System.Windows.Forms.Button button6; private System.Windows.Forms.Button Button7; Private System.Windows . Form.Button Button8; /// // / The necessary designer variables. 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 static byte [] Onebytes; /// private system.componentmodel.container components = null; public form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {Components.dispose ();}} Base.Dispose (DISPOSION);} #Region Windows Form The code generated by the designer /// /// designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.tabControl1 = new System.Windows.Forms.TabControl (); this.tabPage1 = new System.Windows.Forms.TabPage (); this.button6 = new System. Windows.Forms.Button (); this.button3 = new system.windows.forms.button (); this.button2 = new system.windows.Forms.Button (); this.richtext = new system.Windows.Forms.richtextbox (); This.button1 = new system.windows.Forms.Button (); this.tabpage2 = new system.windows.forms.tabpage (); this.button8 = new system.windows.Forms.Button (); this. Button7 = new system.windows.Forms.Button (); this.label3 = new system.windows.forms.label (); this.richtext3 = new system.windows.forms.richtextbox (); this.label2 = new system. Windows.Forms.Label (); this.richtext2 = new system.windows.forms.richtextbox (); this.button5 = new system.windows.Forms.Button (); this.button4 = new system.windows.Forms.Button (); This.textBox1 = new system.windows.Forms.TextBox (); this.label1 = new system.windo Ws.Forms.label (); this.save = new system.windows.forms.savefiledialog (); this.open = new system.windows.forms.openfiledialog (); this.tabcontrol1.suspendlayout (); this.tabpage1. SuspendLayout (); this.tabPage2.SuspendLayout (); this.SuspendLayout (); // // tabControl1 // this.tabControl1.Controls.Add (this.tabPage1); this.tabControl1.Controls.Add (this.tabPage2) ; this.tabControl1.Location = new System.Drawing.Point (32, 64); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System.Drawing.Size ( 416, 280); this.tabcontrol1.tabindex = 0;

// // tabpage1 // this.tabpage1.controls.add (this.button6); this.tabpage1.controls.add (this.button3); this.tabpage1.controls.add (this.button2); this.tabpage1. Controls.add (this.richtext); this.tabpage1.controls.add (this.button1); this.tabpage1.location = new system.drawing.point (4, 21); this.tabpage1.name = "tabpage1"; THIS.TABPAGE1.SIZE = New System.drawing.size (408, 255); this.tabpage1.tabindex = 0; this.tabpage1.text = "Get Key"; // // Button6 // this.button6.Location = New system.drawing.point (72, 168); this.button6.name = "button6"; this.button6.tabindex = 4; this.button6.text = "write file"; this.button6.click = new System.EventHandler (this.button6_click); // // Button3 // this.button3.location = new system.drawing.point (200, 16); this.button3.name = "button3"; this.button3.tabindex = 3; this.button3.text = "Save Private"; this.Button3.Click = New System.EventHandler (this.button3_click; // // Button2 // this.button2.locatio n = new system.drawing.point (128, 16); this.button2.name = "button2"; this.button2.tabindex = 2; this.button2.text = "save public key"; this.button2.click = New System.EventHandler (this.button2_click); // // richtext // this.richtext.location = new system.drawing.point (16, 56); this.richtext.name = "richtext"; this.richtext. Size = new system.drawing.size (256, 96); this.richtext.tabindex = 1; this.richtext.text = "richtextbox1"; // // button1 // this.button1.location = new system.drawing. POINT (8, 16);

This.button1.name = "button1"; this.button1.size = new system.drawing.size (88, 23); this.button1.tabindex = 0; this.button1.text = "Get Key Information"; this. Button1.click = new system.eventhandler (this.button1_click); // // tabpage2 // this.tabpage2.controls.add (this.button8); this.tabpage2.controls.add (this.button7); this. Tabpage2.controls.add (this.label3); this.tabpage2.controls.add (this.richtext3); this.tabpage2.controls.add (this.label2); this.tabpage2.controls.add (this.richtext2); THIS.TABPAGE2.CONTROLS.ADD (this.button5); this.tabpage2.controls.add (this.button4); this.tabpage2.controls.add (this.TextBox1); this.tabpage2.controls.add (this.label1 ); This.tabpage2.Location = new system.drawing.point (4, 21); this.tabpage2.name = "tabpage2"; this.tabpage2.size = new system.drawing.size (408, 255); this. Tabpage2.tabindex = 1; this.tabpage2.text = "Encrypt Decryption Text"; // // Button8 // This.Button8.Location = New System.drawing.Point (216, 72); this.button8 .Name = "button8"; this.button8.tabindex = 9; this.button8.text = "Read Private"; this.Button8.Click = new system.eventhandler (this.button8_click); // // Button7 // this.button7.location = new system.drawing.point (56, 72); this.button7.name = "button7"; this.button7.tabindex = 8; this.button7.text = "Read the public" BTTON7.Click = new system.eventhandler (this.button7_click); // // label3 // this.label3.location = new system.drawing.point (200, 120); this.label3.name = " Label3 "; this.label3.size = new system.drawing.size (100, 16);

THIS.Label3.Tabindex = 7; this.label3.text = "Decryptive Results"; // // Richtext3 // this.richtext3.Location = New System.drawing.Point (200, 144); this.richtext3.name = "richtext3"; this.richtext3.size = new system.drawing.size (120, 72); this.richtext3.tabindex = 6; this.richtext3.text = "richtextbox2"; // // label2 // this.label2 .Location = new system.drawing.point (48, 120); this.label2.name = "label2"; this.label2.size = new system.drawing.size (128, 16); this.Label2.tabindex = 5 This.label2.text = "Encrypted window:"; // // richtext2 // this.richtext2.location = new system.drawing.point (40, 144); this.richtext2.name = "richtext2"; this. Richtext2.size = new system.drawing.size (120, 72); this.richtext2.tabindex = 4; this.richtext2.text = "richtextbox2"; // // button5 // this.button5.location = new system. Drawing.point (200, 96); this.button5.name = "button5"; this.button5.size = new system.drawing.size (112, 23); this.button5.tabindex = 3; This.button5.text = "Use Private Nortal Decryption"; this.Button5.Click = New System.EventHandler (this.Button5_Click); // // Button4 // this.button4.location = new system.drawing.point ( 48, 96); this.Button4.name = "button4"; this.button4.size = new system.drawing.size (96, 23); this.button4.tabindex = 2; this.button4.text = "Used public encryption key "; this.button4.Click = new System.EventHandler (this.button4_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point (104, 32); this.textBox1. MultiLine =

True; this.textBox1.name = "textbox1"; this.textBox1.size = new system.drawing.size (168, 32); this.textBox1.tabindex = 1; this.TextBox1.text = "textbox1"; // // label1 // this.label1.location = new system.drawing.point (128, 8); this.label1.name = "label1"; this.label1.size = new system.drawing.size (120, 16) This.label1.tabindex = 0; this.Label1.text = "Please enter the encrypted text"; // // Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = New system.drawing.size (504, 357); this.controls.add (this.tabcontrol1); this.name = "form1"; this.text = "form1"; this.tabcontrol1.ResumeLayout (False); this. TabPage1.ResumeLayout (false); this.tabpage2.resumeLayout (false); this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application.

/// [STAThread] static void Main () {Application.Run (new Form1 ());} // get the key information private void button1_Click (object sender, System.EventArgs e) {crypt = new RSACryptoServiceProvider ( ); Publickey = crypt.toxmlstring (false); richtext.text = "Exporting the secret: / n" publickey "/ n"; privatekey = crypt.toxmlstring (true); string info = "only exporting a lasp In the case of: / n " privatekey " / n "; richtext.appendtext (info); crypt.clear ();} / / Save auning information private void button2_click (object sender, system.eventargs e) {save = new SavefileDialog (); save.filter = "file text (* .txt) | * .txt | all file (*. *) | *. *"; Save.showdialog (); publicinfo = save.filename;} // Save Key Information Private Void Button3_Click (Object Sender, System.Eventargs E) {save = new savefileDialog (); save.filter = "File text (* .txt) | * .txt | all file (*. *) | *. * "; Save.showdialog (); privateInfo = save.filename;} // Write key information to file private void button6_click (Object sender, system.eventargs e) {streamwriter one = new StreamWriter (publicinfo, true, UTF8Encoding.UTF8); one.Write (publickey); StreamWriter two = new StreamWriter (privateinfo, true, UTF8Encoding.UTF8); two.Write (privatekey); one.Flush (); two .Flush (); one.close (); two.close (); messagebox.show ("Successfully saved the pincology! ");} // use of public key cryptography private void button4_Click (object sender, System.EventArgs e) {crypt = new RSACryptoServiceProvider (); UTF8Encoding enc = new UTF8Encoding (); bytes = enc.GetBytes (textBox1.Text); crypt .Fromlstring (readpublicKey); Bytes = crypt.encrypt (bytes, false);

string encryttext = enc.GetString (bytes); // encryptbyte); richtext2.Text = "encryption result: / n" encryttext "/ n" "end of encryption!";} private void button5_Click (object sender, System.EventArgs e) {UTF8Encoding enc = new UTF8Encoding (); byte [] decryptbyte; crypt.FromXmlString (readprivatekey); decryptbyte = crypt.Decrypt (bytes, false); string decrypttext = enc.GetString (decryptbyte); richtext3.Text = "decryption Result: / n " DecryptText " / n " " Decryption End! "; (); Open.filter = "text file (* .txt) | * .txt | all file (*. *) | *. *"; // open.showdialog (); if (Open.ShowDialog () == DialogResult.ok) {sr = new streamreader (Open.FileName, UTF8Encoding.utf8);} else {messagebox.show ("An error!"); Returnite (); sr.close (); sr.Close (); } // Read Private Void Button8_Click (Object Sender, System.Eventargs E) {Open = New Openfil Edialog (); open.filter = "text file (* .txt) | * .txt | all file (*. *) | *. *"; open.showdialog (); streamreader sr = new streamreader (Open.FileName, Utf8encoding.utf8); readprivateKey = sr.readToend (); sr.close ();}}}

转载请注明原文地址:https://www.9cbs.com/read-21872.html

New Post(0)