C # - asymmetric encryption: encrypted file

zhaozj2021-02-16  59

C # - asymmetric encryption: encrypted file ★★★★★★ ☆ ☆ ★★★★★★ ● ○ ○ ○ 永 ○ ○ ● §§§§§e ● ● §§ §§ee@163.com §§ §◆ □□□ Nie.yong@126.com □ ◆◆ - padownload Noiennium No Noise: 1. You have to pay attention to the two different ways of reading the file; 2. From already saved Read the content in the key file; 3. This program is very rough (to prepare foreign language six levels, you must prepare for the final exam, it is very busy!), I hope that you can improve, and then published, thank you under; 4. Reference Bibliography: "C # Data Security Manual"; 5. Have friends who do this, willing to intersect. Code section:

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; using System. Threading;

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.TabPage tabPage3; 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.Window s.Forms.Button button7; private System.Windows.Forms.Button button8; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label4; private System.Windows.Forms.Button button9; private System. Windows.Forms.TextBox textBox3; private System.Windows.Forms.Label label5; private System.Windows.Forms.Button button10; private System.Windows.Forms.Label label6; private System.Windows.Forms.Button button11; private System. Windows.Forms.Button button12; private System.Windows.Forms.RichTextBox richtext4; private System.Windows.Forms.Button button13; private System.Windows.Forms.Button button14; private System.Windows.Forms.Button button15; private System. Windows.forms.tabpage tabpage4;

private System.Windows.Forms.TextBox textBox4; private System.Windows.Forms.Button button16; private System.Windows.Forms.TextBox textBox5; private System.Windows.Forms.Button button17; private System.Windows.Forms.Button button18; private System.Windows.Forms.Button button19; private System.Windows.Forms.Button button20; private System.Windows.Forms.Button button21; private System.Windows.Forms.RichTextBox richtext5; ///

/// required Designer variable. 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; // if file opening and reading the key labeled private bool openla = false; private bool savela = false; private bool readpubkey = false; private bool readprikey = false; private Thread th; /// private System.ComponentModel.Container components = null; public Form1 () {// // Windows Form Designer Support for // InitializationComponent ();

// // Todo: Add any constructor code / /} ///

///} /// ///} /// ///} /// /// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose (4);

#REGION Windows Form Designer The code ///

/// 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.tabpage3 = new system.windows.forms.tabpage (); this.button15 = new system.windows.Forms.Button (); this.button14 = new system.windows.Forms.Button (); This.Button13 = new system.windows.Forms.Button (); this.Button12 = new system.windows.Forms.Button (); this.button11 = new system.windows.Forms.Button (); this. Richtext4 = new system.windows.forms.richtextbox (); this.label6 = new system.windows.forms.Label (); this.button10 = new system.windows.Forms.Button (); this.label5 = new system. Windows.Forms.Label (); this.textbox3 = new system.windows.forms.textbox (); this.button9 =

New system.windows.Forms.Button (); this.label4 = new system.windows.forms.label (); this.textbox2 = new system.windows.forms.textbox (); this.tabpage4 = new system.windows. Forms.TabPage (); this.save = new System.Windows.Forms.SaveFileDialog (); this.open = new System.Windows.Forms.OpenFileDialog (); this.textBox4 = new System.Windows.Forms.TextBox () This.button16 = new system.windows.Forms.Button (); this.textbox5 = new system.windows.forms.textbox (); this.button17 = new system.windows.Forms.Button (); this.button18 = New system.windows.Forms.Button (); this.button19 = new system.windows.Forms.Button (); this.button20 = new system.windows.Forms.Button (); this.button21 = new system.windows. Forms.Button (); this.richtext5 = new System.Windows.Forms.RichTextBox (); this.tabControl1.SuspendLayout (); this.tabPage1.SuspendLayout (); this.tabPage2.SuspendLayout (); this.tabPage3.SuspendLayout (); This.tabpage4.suspendlayout (); this.suspendlayout (); // // Tabco ntrol1 // this.tabControl1.Controls.Add (this.tabPage1); this.tabControl1.Controls.Add (this.tabPage2); this.tabControl1.Controls.Add (this.tabPage3); this.tabControl1.Controls.Add ( this.tabPage4); this.tabControl1.Location = new System.Drawing.Point (8, 64); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System. Drawing.size (568, 368); 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 (560, 343); this.tabpage1.tabindex = 0; this.tabpage1.text = "Get the key"; // // Button6 // this.button6.Location = new system.drawing.point (216, 288); 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 (456, 16); this.button3.name = "button3"; this.button3.tabindex = 3; this.button3.text = " Saving Private; this.Button3.click = new system.eventhandler (this.button3_click); // // Button2 // this.button2.location = new system.drawing.point (320, 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 (520, 184); this.richtext.tabindex = 1; this.richtext.text = "richtextbox1"; // // button1 // this.button1.location = new system.drawing.point (136, 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 (560, 343); this.tabpage2.tabindex = 1 This.tabpage2.text = "Encryption Decryption Text"; // // Button8 // this.button8.Location = New System.drawing.Point (368, 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 (120, 72); this.button7.name = "button7"; this.button7.tabindex = 8; this.button7.text = "Read the public"; this .Button7.click = new system.eventhandler (this.button7_click); // // label3 // this.label3.location = new system.drawing.point (304, 128); 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 (280, 152); this.richtext3.name = "richtext3";

this.richtext3.size = new system.drawing.size (272, 160); this.richtext3.tabindex = 6; this.richtext3.text = "richtextbox2"; // // label2 // this.label2.location = new System.drawing.point (48, 128); 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 (8, 152); this.richtext2.name = "richtext2"; this.richtext2.size = New system.drawing.size (256, 160); this.richtext2.tabindex = 4; this.richtext2.text = "richtextbox2"; // // button5 // this.button5.location = new system.drawing.point ( 352, 96); this.button5.name = "button5"; this.button5.size = new system.drawing.size (112, 23); this.button5.tabindex = 3; this.button5.text = "Use private Jackicer; this.button5.click = new system.eventhandler (this.button5_click); // // button4 // this.button4.location = new system.drawing.point (112, 96 ); This.button4.name = "button4"; this.button4.size = new system.drawing.size (96, 23); this.button4.tabindex = 2; this.button4.text = "Using a public key encryption" 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 (264, 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"; /// TabPage3 // this.tabpage3.controls.add (this.button15); this.tabpage3.controls .Add (this.button14); this.tabpage3.controls.add (this.button13); this.tabpage3.controls.add (this.button12); this.tabpage3.controls.add (this.button11); this.tabpage3 .Controls.add (this.richtext4); this.tabpage3.controls.add (this.label6); this.tabpage3.controls.add (this.button10); this.tabpage3.controls.add (this.label5); this. .tabpage3.controls.add (this.textbox3); this.tabpage3.controls.add (this.button9); this.tabpage3.controls.add (this.label4); this.tabpage3.controls.add (this.textbox2) Im.tabpage3.location = new system.drawing.point (4, 21); this.tabpage3.name = "tabpage3"; this.tabpage3.size = new system.drawing.size (560, 343); this.tabpage 3.Tabindex = 2; this.tabpage3.text = "Encryption Decryption File"; // // Button15 // this.button15.location = new system.drawing.point (208, 128); this.button15.name = " Button15 "; this.button15.tabindex = 12; this.button15.text =" Empty text "; this.button15.click = new system.eventhandler (this.button15_click); // // button14 // this.button14. Location = new system.drawing.point (432, 88); this.button14.name = "button14"; this.button14.tabindex = 11; this.button14.text = "Read Private"; this.button14.click = new system.eventhandler (this.button14_click);

// // button13 // this.button13.location = new system.drawing.point (432, 32); this.button13.name = "button13"; this.button13.tabindex = 10; this.button13.text = " Reading a pincker; this.button13.click = new system.eventhandler (this.button13_click); /// button12 // this.button12.location = new system.drawing.point (320, 88); this. Button12.name = "button12"; this.button12.tabindex = 9; this.button12.text = "Start Decryption"; this.button12.click = new system.eventhandler (this.button12_click); // // Button11 / / This.button11.location = new system.drawing.point (320, 32); this.button11.name = "button11"; this.button11.tabindex = 8; this.button11.text = "Start encryption"; Button11.click = new system.eventhandler (this.button11_click); // // richtext4 // this.richtext4.location = new system.drawing.point (8, 152); this.richtext4.name = "richtext4"; this.richtext4.size = new system.drawing.size (496, 168); this.richtext4.tabindex = 7; this.richtext4.tex T = ""; // // label6 // this.label6.location = new system.drawing.point (16, 120); this.label6.name = "label6"; this.Label6.size = new system.drawing .Size (168, 23); this.label6.tabindex = 6; this.label6.text = "Encryption and decryption window:"; // // button10 // this.button10.location = new system.drawing.point ( 224, 88); this.button10.name = "button10"; this.button10.tabindex = 5; this.button10.text = "save ..."; this.button10.click = new system.eventhandler (this. Button10_click;

/// Label5 // this.label5.location = new system.drawing.point (16, 64); this.label5.name = "label5"; this.label5.size = new system.drawing.size (216, 16); this.label5.tabindex = 4; this.label5.text = "Please save the file after adding or decryption"; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////> , 88); this.TextBox3.name = "textbox3"; this.textbox3.size = new system.drawing.size (184, 21); this.TextBox3.TabINDEX = 3; this.TextBox3.text = "textbox3"; // // button9 // this.button9.location = new system.drawing.point (224, 32); this.button9.name = "button9"; this.button9.tabindex = 2; this.button9.text = " Open ... "; this.button9.click = new system.eventhandler (this.button9_click); // // label4 // this.label4.location = new system.drawing.point (16, 8); this. Label4.name = "label4"; this.label4.size = new system.drawing.size (216, 16); this.Label4.Tabindex = 1; this.label4.text = "Please select text file to encryption or decrypted "; /// textbox2 // this.te Xtbox2.location = new system.drawing.point (16, 32); this.TextBox2.name = "textbox2"; this.textbox2.size = new system.drawing.size (184, 21); this.textBox2.tabindex = 0; this.TextBox2.text = "textbox2"; // // tabpage4 // this.tabpage4.controls.add (this.richtext5); this.tabpage4.controls.add (this.button21); this.tabpage4.controls .Add (this.button20); this.tabpage4.controls.add (this.button19); this.tabpage4.controls.add (this.button18); this.tabpage4.controls.add (this.button17); this.tabpage4 .Controls.add (this.textbox5);

THIS.TABPAGE4.CONTROLS.ADD (this.button16); this.tabpage4.controls.add (THISTEXTBOX4); this.tabpage4.location = new system.drawing.point (4, 21); this.tabpage4.name = "tabpage4"; this.tabpage4.size = new system.drawing.size (560, 343); this.tabpage4.tabindex = 3; this.tabpage4.text = "Encryption Decryption Other Files"; // // TextBox4 // THISTBOX4.LOCATION = New System.drawing.point (24, 24); this.textBox4.name = "textbox4"; this.textbox4.size = new system.drawing.size (240, 21); this.TextBox4. TabINDEX = 0; this.textBox4.text = "textbox4"; // // button16 // this.button16.location = new system.drawing.point (304, 24); this.button16.name = "button16"; THIS .button16.TabIndex = 1; this.button16.Text = "open"; this.button16.Click = new System.EventHandler (this.button16_Click); // // textBox5 // this.textBox5.Location = new System. Drawing.point (24, 56); this.textbox5.name = "textbox5"; this.textbox5.size = new system.drawing.size (240, 21); this.TextBox5.ta BINDEX = 2; this.textBox5.text = "textbox5"; // // button17 // this.button17.location = new system.drawing.point (304, 56); this.button17.name = "button17"; this .Button17.tabindex = 3; this.button17.text = "save"; this.button17.click = new system.eventhandler (this.button17_click); // // button18 // this.button18.location = new system. Drawing.point (48, 88); this.button18.name = "button18"; this.button18.tabindex = 4; this.button18.text = "Read the public"; this.button18.click =

New system.eventhandler (this.button18_click); // // Button19 // this.button19.location = new system.drawing.point (160, 88); this.button19.name = "button19"; this.button19.tabindex = 5; this.button19.text = "Read Private"; this.Button19.Click = new system.eventhandler (this.button19_click; // // Button20 // this.button20.location = new system.drawing .Point (112, 144); this.button20.name = "button20"; this.button20.tabindex = 6; this.button20.text = "encrypted file"; this.button20.click = new system.eventhandler (this .button20_click; // // Button21 // this.button21.location = new system.drawing.point (232, 144); this.button21.name = "button21"; this.button21.tabindex = 7; this.button21 .Text = "Decrypt file"; this.button21.click = new system.eventhandler (this.button21_click); // // richtext5 // this.richtext5.location = new system.drawing.point (88, 200); This.richtext5.name = "richtext5"; this.richtext5.size = new system.drawing.size (304, 112); this.richtext5.tabindex = 8; this.richtext5.text = ""; /// Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (THISTABCONTROL1); this.Name = "form1"; this.text = "form1"; this.tabcontrol1.ResumeLayout; this.tabpage1.ResumeLayout (False) This.tabpage2.resumeLayout (false); this.tabpage3.resumeLayout (FALSE); this.tabpage4.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;} // encrypted with a public void button 4_click (object sender, system.eventargs e) {ix (TextBox1.te) Xt == "") {MessageBox.show ("Encrypted text information cannot be empty! "); Return;}} {MessageBox.show (" Please open the file where the public key is located! "); Return;} try {crypt = new} tryptoserviceProvider (); uTF8encoding enc = new utf8encoding (); bytes = enc.GetBytes (textBox1.Text); crypt.FromXmlString (readpublickey); bytes = crypt.Encrypt (bytes, false); string encryttext = enc.GetString (bytes); richtext2.Text = "encryption result: / n" EncrytText "/ n" "encryption end! "

} Catch {messagebox.show ("Please check if there is a public key or no mark!");}} // Use Private Void Button5_Click (Object Sender, System.EventArgs E) {if (Readprikey == False ) {MessageBox.Show ( "open file key located!"); return;} try {UTF8Encoding enc = new UTF8Encoding (); byte [] decryptbyte; crypt.FromXmlString (readprivatekey); decryptbyte = crypt.Decrypt (bytes , False); String Decrypttext = enc.getstring (decryptbyte); richtext3.text = "Decryption results: / n" DecryptText "/ n" "Decryption end!";} catch {messagebox.show ("Please check if Open Private Nobi or Private Whether it is damaged! ");}} // Put the 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 (); one.flush (); one.close (); two (); one.close (); two.close (); MessageBox.show ("Successful Solving Solver!");} // Read the Scheel Information from the Focus Private Void Button7_Click ( object sender, System.EventArgs e) {readpublickey = ReadPublicKey (); readpubkey = true;} // read the private key information private void button8_Click (object sender, System.EventArgs e) {readprivatekey = ReadPrivateKey () from the file; readprikey = True;} // Open Encrypted or Decryred file private void button9_click (Object sender, system.eventargs e) {open = new openfiledialog (); open.filter = "text file (* .txt) | * .txt | all Files (*. *) | *. * "; Open.showdialog (); textbox2.text = open.filename; OpenLA = true;

} // Save the encrypted or decrypted file private void button10_click (Object sender, system.eventargs e) {Try {save = new savefiledialog (); save.filter = "File text (* .txt) | * .txt | all file (*. *) | *. * "; Save.showdialog (); TextBox3.text = save.FileName; savela = true;} catch {messagebox.show (" Please enter the file name! "); Return;}

} // Encrypted text file private void button11_click (Object sender, system.eventargs e) {if (readpubkey! = True) {messagebox.show ("Please open the file in the public key!"); Return;} if (OpenLA = = true && savela == true) {crypt = new RSACryptoServiceProvider (); crypt.FromXmlString (readpublickey); UTF8Encoding enc = new UTF8Encoding (); // read the original document to a string to ah StreamReader sr = new StreamReader (textBox2.Text, Encoding.default; string textinfo = sr.readToend (); sr.close (); // richtext4.AppendText ("/ n original file content: / n" textinfo "/ n"); // Start encryption String ReadInfo = EncryptFile (TextInfo, TextBox3.text); Richtext4.AppendText ("Encryption file has been saved to:" TextBox3.text "/ n"); Richtext4.AppendText ("Encryption results: / n" readinfo "/ N encryption end! ");} else messagebox.show (" Please select the file you want to encrypted or the file you want to save! ");} // Decryption file private void button12_click (Object sender, System.EventArgs E) {IF ( Readprikey == false) {MessageBox.show ("Please open the file!"); return;} if (OpenLA == true && savela == true) {TRY { crypt = new RSACryptoServiceProvider (); crypt.FromXmlString (readprivatekey); string decryptinfo = DecryptFile (textBox2.Text, textBox3.Text); richtext4.AppendText ( "to decrypt the file has been saved:" textBox3.Text "/ n"); Richtext4.AppendText ("Decryptive Result: / N" Decryptinfo "/ N" "decryption end! / n ");} catch {messagebox.show (" Please check if the key file corresponds to the public or key file damage! "); return;}} else messagebox.show ("

Please select the file you want to decrypt or save the file! ");} // Encrypted file When you want to open the public key private void button13_click (object sender, system.eventargs e) {readpublicKey = readpublicKey (); readpubkey = true;} // Decipse Secure file, the same private void button14_Click (object sender, System.EventArgs e) {readprivatekey = ReadPrivateKey (); readprikey = true;} private void button15_Click (object sender, System.EventArgs e) {richtext4.Clear ();} // open the file private void Button16_click (object sender, system.eventAndargs e) {Open = new openfiledialog (); open.filter = "all files (*. *) | *. *"; open.showdialog (); textbox4.text = open.filename; OpenLA = true;} // Save file private void button17_click (object sender, system.eventargs e) {save = new savefiledialog (); save.filter = "all file (*. *) | *. *"; save.showdialog (); textBox5.Text = save.FileName; savela = true;} // other non-text files encrypted private void EncryptOther (string file1, string file2) {FileStream picfs = new FileStream (@ file1, FileMode.Open); FileStream fs = New FileStream (@ file2, filemode.openorcreate); Byte [] bytes = new byte [picfs.length]; picfs.read (bytes, 0, (int)); picfs.close (); int blocksize = 0; IF (crypt.keysize == 1024) blocksize = 16; else blocksize = 8; byte [] Rawblock, Encryblock; for (int i = 0; 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);

} Fs.flush (); fs.close ();} // Decryption other non-text file private void decryptother (String fileone, string filestream); @ fileOne, filemode.open; filestream Writopic = New FileStream (@ filetwo, filemode.create); byte [] bytes = new byte [fs.length]; fs.read (bytes, 0, (int) fs.length); fs.close (); memorystream ms = new memoryStream (); Int keySize = crypt.keysize / 8; Byte [] Rawblock, DecryptBlock; for (int i = 0; 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.Length);} ms.position = 0; Byte [] decode = new byte [ms.length]; ms.read (decode, 0, (int) ms.length) Ms.close (); Writopic.write (decode, 0, decode.length); Writopic.Close ();

} // create a function that can use private void DecryptUsingThread () {DecryptOther (textBox4.Text, textBox5.Text) in the thread;} ​​// read the public key private void button18_Click (object sender, System.EventArgs e) {readpublickey = ReadPublicKey (); readpubkey = true;} // read private key private void button19_Click (object sender, System.EventArgs e) {readprivatekey = ReadPrivateKey (); readprikey = true;} // other non-text files encrypted private void friends Button20_click (object desnder, system.eventargs e) {if (readpubkey == false) {messagebox.show ("Please open the file in the public!"); return;} if (OpenLA == true&& savela == true) {TRY {crypt = new RSACryptoServiceProvider (); crypt.FromXmlString (readpublickey); EncryptOther (textBox4.Text, textBox5.Text); richtext5.AppendText ( "encryption process ends:! / n" "encrypted files are saved to: / n" TextBox5.text "/ n");} catch {messagebox.show ("Please check if the key file corresponds to the public or key file damage!"); return;}} else messagebox.show ("Please Select the file you want to decrypted or save the file! ");} // Decrypt other files PRIVATE VOI D button21_click (object sender, system.eventargs e) {if (readprikey == false) {messagebox.show ("Please open the file in the key! "); Return;}}}} {try {crypt = new {type {crypt = new {type {crypt = new {type {crypt = new {type {crypt = new {type {crypt = new {type {crypt = new {type {crypt = new RSacryptoserviceProvider (); crypt.fromlstring (readprivateKey); // After using the thread, there is no effect // reason? Clear // can be converted, but the conversion should be a few minutes before you can really get what you want.

TH = New Thread (DecryptusingThread); th.start (); // Decryptother (TextBox4.text, textbox5.text); richtext5.AppendText ("Decryption end! / n" "decrypted file has been saved: " TextBox5.text " / n ");} catch {messagebox.show (" Please check if the key file corresponds to the public or key file damage! "); return;}} else messagebox.show (" please Select the file you want to decrypt or save the file! ");

// The following is a function of which you define it. // Separate a function of reading a public string readpublicKey () {string publickey; open = new openfileDialog (); open.filter = "text file (* .txt) | *. TXT | All File (*. *) | *. * "; open.showdialog (); streamreader sr = new streamreader (open.filename, utf8encoding.utf8); publickey = sr.readToEnd (); sr.close (); Return publickey;} // Separate a function private string readprivateKey () {string privateKey; open = new openfiledialog (); open.filter = "text file (* .txt) | * .txt | all file *. *) | *. * "; Open.showdialog (); streamreader sr = new streamreader (Open.FileName, UTF8Encoding.utf8); privateKey = sr.readToEnd (); sr.close (); returnizrn privatekey; / text file encryption 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; / / Write to MemoryStream ms = new memorystream (); // Write file Go. FILESTREAM FS = New FileStream (@ filename, filemode.openorcreate); Byte [] Rawblock, EncryBlock; for (int i = 0; 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); // Write to memory.

Ms.write (Encryblock.Length);} // Write to memory 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;} // Decryption text file private String DecryptFile (String DecryptFile) {// Open the encrypted file, read the file content filestream fs = new filestream (@ encryptfile, filemode.open); byte [] bytes = new byte [fs.length]; fs .Read (Bytes, 0, (int) fs.length); fs.close (); // Write to memory MemoryStream ms = new memoryStream (); // Preferably, a file STAMWRITER SW = new streamwriter; int key = crypt.keysize / 8; Byte [] Rawblock, DecryptBlock; for (int i = 0; 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); // Write to Go in memory sw.write (text); sw.flush (); ms.close (); res. close (); Return text;}

// encrypt other non-text files, including text files may of course private void EncryptOther (string file1, string file2) {FileStream picfs = new FileStream (@ file1, FileMode.Open); FileStream fs = new FileStream (@ file2, FileMode.OpenOrCreate ); Byte [] bytes = new byte [picfs.length]; picfs.read (bytes, 0, (int)); picfs.close (); int blocksize = 0; if (Crypt.keysize == 1024 Blocksize = 16; Else Blocksize = 8; Byte [] Rawblock, EncryBlock; for (INT I = 0; 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;} fs.flush (); fs.close ();} // Decryption other non-text files, of course, text file private void decryptother (String Fileone, String Filetwo) {FILESTREAM FS = New filestream (@ fileone, filemode.open); filestream Writopic = New fileStream (@ filetwo, filemode.create); byte [] bytes = new byte [fs.length]; fs. Read (Bytes, 0, (INT) fs.length; fs.close (); memorystream ms = new memoryStream (); int key = crypt.keysize / 8; Byte [] Rawblock, DecryptBlock; for (int i = 0 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); ms.close (); Writopic.write (decode, 0, decode.length); Writopic.close );} // Create a function, can be used in the thread, experiment, the effect does not obvious private void decryptusingthread () {decryptother (TextBox4.text, textbox5.text);}}}

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

New Post(0)