C # - an example of symmetrical encryption

zhaozj2021-02-16  79

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 required designer variable. 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 instance RijndaelManaged rij = new RijndaelManaged (); // // global variables, labeled Document Extension Private Static String Ext = NULL; // Tag Encryption Successfully Whether Private Static Bool EnResult = FALSE; / / Tag Decryption Success Mark 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 (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code ///

/// Designer supports the required method - Do not use the code editor to modify the // / this method. ///

Private vidinitiRizeComponent ()

{

This.TextBox1 = new system.windows.Forms.TextBox ();

THISTBOX2 = 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 ();

//

// textbox1

//

This.TextBox1.Location = new 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.TextBox2.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 = "Encrypt";

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 = "Decrypt";

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.hor;

//

// 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, 23);

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 main entry point for 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-14799.html

New Post(0)