C # -rijndaelmanaged

zhaozj2021-02-16  94

This applet is nothing. In order to save time. Please see the code!

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

Namespace symmetrical encryption application {//

/// Form1 summary description. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button button1; private System.windows.Forms.Button Button2; /// // / / / The designer variable is required. private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.OpenFileDialog openfile; private System.Windows.Forms.Button button3; private System.Windows.Forms.SaveFileDialog savefile; private System.Windows.Forms.Button button4; Private system.windows.forms.label label1; private system.windows.forms.label label2; private system.windows.Forms.checkbox checkbox1;

// Create an instance rijndaelManaged Rij = new rijndaelManaged (); // // global variable, tag file extension private static string ext = null; // Markup encryption success or no private static bool enResult = false; // Mark Decryption success Markup private static bool deresult = false; /// 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

#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.textBox1 = new System.Windows.Forms.TextBox (); this.textBox2 = new System.Windows.Forms.TextBox (); this.button1 = new System. Windows.Forms.Button (); this.button2 = new System.Windows.Forms.Button (); this.textBox3 = new System.Windows.Forms.TextBox (); this.openfile = new System.Windows.Forms.OpenFileDialog (); This.button3 = new system.windows.Forms.Button (); this.savefile = new system.windows.forms.savefiledialog (); this.button4 = new system.windows.Forms.Button (); this. Label1 = new system.windows.forms.label (); this.label2 = new system.windows.forms.label (); this.checkbox1 = new system.windows.forms.checkbox (); this.suspendlayout (); / / //////////////////////Ww system.drawing.point (88, 96); this.textBox1.name = "textbox1"; this.textbox1.size = new system.drawing.size (168, 21 ); This.TextBox1.tabindex = 0; this.textBox1.text = ""; /// textbox2 // this.textb OX2.Location = new system.drawing.point (88, 136); this.textbox2.name = "textbox2"; this.textbox2.size = new system.drawing.size (168, 21); this.TextBox2.tabindex = 1; this.TextBox2.text = ""; // // button1 // this.button1.location = new system.drawing.point (88, 200); this.button1.name = "button1"; this.button1. TabINDEX = 2; this.button1.text = "encryption"; this.button1.click = new system.EventHandler (this.button1_click); // // button2 // this.button2.location = new system.drawing.point (192, 200); this.button2.name = "button2";

This.Button2.Tabindex = 3; this.button2.text = "Decryption"; this.button2.click = new system.eventhandler (this.button2_click); //// textbox3 // this.TextBox3.hideselection = false; This.TextBox3.Location = new system.drawing.point (88, 48); this.TextBox3.maxlength = 17; this.textbox3.name = "textbox3"; this.textbox3.passwordchar = '$'; this.TextBox3. TabIndex = 4; this.textBox3.Text = ""; this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // // button3 // this.button3.Location = new System.Drawing.Point (280 , 96); this.button3.name = "button3"; this.button3.size = new system.drawing.size (40, 23); this.button3.tabindex = 5; this.button3.text = "open"; This.button3.click = new system.eventhandler (this.button3_click); // // button4 // this.button4.location = new system.drawing.point (280, 136); this.button4.name = "Button4 "; This.button4.size = new system.drawing.size (40, 23); this.button4.tabindex = 6; this. Button4.text = "save"; this.button4.click = new system.eventhandler (this.button4_click); // // label1 // this.label1.location = new system.drawing.point (88, 24); THIS.Label1.name = "label1"; this.label1.size = new system.drawing.size (136, 23); this.label1.tabindex = 7; this.label1.text = "Please enter your password:" ; // // label2 // this.label2.Location = new system.drawing.point (216, 48); this.label2.name = "label2"; this.label2.size = new system.drawing.size (184 , twenty three);

THIS.Label2.Tabindex = 8; this.label2.text = "(password size 8 to 16 characters)"; /// Checkbox1 // this.checkbox1.location = new system.drawing.point (88, 168) This.checkbox1.name = "CheckBox1"; this.checkbox1.size = new system.drawing.size (216, 24); this.checkbox1.tabindex = 9; this.checkbox1.text = "Delete source file"; / / // form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (456, 278); this.Controls.add (this.checkbox1); this .Controls.add (this.label2); this.controls.add (this.label1); this.controls.add (this.button4); this.controls.add (this.button3); this.controls.add (this .TextBox3); this.controls.add (this.button2); this.controls.add (this.button1); this.Controls.add (this.TextBox2); this.Controls.add (this.TextBox1); this. Name = "form1"; this.text = "form1"; this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.run (new form1 ());}

// button private void button1_Click encrypted file (object sender, System.EventArgs e) {if (textBox1.Text! = Null || textBox2.Text! = Null) encryption (textBox3.Text, textBox1.Text, textBox2.Text) ; if (checkBox1.Checked == true && enresult == true) DeleteFile (textBox1.Text);} // function used for the encryption public void encryption (string textBox, string readfile, string writefile) {try {if (textBox. Length> = 8 && textBox.LENGTH <= 16) // Determine the size of the character of the password {byte [] key = system.text.Encoding.default.getBytes (textbox); byte [] iv = rij.iv; rijndael Crypt = Rijndael.Create (); ICryptoTransform transform = crypt.CreateEncryptor (key, iv); // file written FileStream fswrite = new FileStream (writefile, FileMode.Create); CryptoStream cs = new CryptoStream (fswrite, transform, CryptoStreamMode.Write ); // Open file filestream fsread = new filestream (readfile, filemode.open); intlene; while ((length = fsread.readByte ())! = -1) cs.writebyTe (Byte) Length);

FSRead.close (); cs.close (); fswrite.close (); enResult = true; // successfully encrypted MessageBox.show ("successfully completed encrypted task!");} else {messagebox.show ("password The minimum length is 8 characters, the maximum length is 16 characters! "); Return;}} catch (e.tostring ());}}}}}} // Used to decrypt the function public void Decryption, PUBLIC VOID DECRYPTION (String textBox, string readfile, string writefile) {try {if (TextBox.Length> = 8 && textBox.Length <= 16) {byte [] key = system.text.encoding.default.getbytes (TextBox); byte [ ] iv = rij.IV; Rijndael crypt = Rijndael.Create (); ICryptoTransform transform = crypt.CreateDecryptor (key, iv); // read the encrypted file FileStream fsopen = new FileStream (readfile, FileMode.Open); CryptoStream cs = new CryptoStream (fsopen, transform, CryptoStreamMode.Read); // the results written into the decrypted file FileStream fswrite = new FileStream (writefile, FileMode.OpenOrCreate); int length; while ((length = cs.ReadByte () )! = -1) fswrite.writebyte (Byte) Length);

fswrite.close (); cs.close (); fsopen.close (); deresult = true; // successfully decrypting MessageBox.show ("Successfully completed the decryption task!");} else {messagebox.show ("password The minimum length is 8 characters, the maximum length is 16 characters! "); Return;}} catch (e.tostring ());}} // Decryption file private void button2_click (Object Sender , System.Eventargs e) {decryption (TextBox3.text, textbox1.text); if (checkbox1.checked == true) deletefile (TextBox1.text);} // Used to open the file button private void button3_Click (object sender, System.EventArgs e) {openfile = new OpenFileDialog (); openfile.Filter = "All files (* *.) | * *."; openfile.ShowDialog (); textBox1.Text = openfile.FileName; ext = getfileext (openfile.FileName);} private void button4_Click (object sender, System.EventArgs e) {savefile = new SaveFileDialog (); savefile.Filter = ext "files" "(*." EXT ") | *." ext "| all files (*. *) | *. *"; savefile.showdialog (); textbox2.text = SAV Efile.FileName;} // Get extension of the file Private string getFileExt (string filename) {try {char [] Point = new char [] {'.'}; string [] filename2 = filename.split (point); return Filename2 [1];} Catch}}} // Delete file function public void deletefile (string filename) {Try {file.delete (filename);} catch (Exception E) {MessageBox (E.TOSTRING) ());}}

}

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

New Post(0)