Http://www.divcss.org/Article/Jsp/c/200608/19135.html
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 symmetric encryption application
{
///
/// Summary of form1.
///
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;
///
/// Required designer variables.
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, marking the file extension
Private Static string ext = NULL;
// Indicates whether the encryption is successful.
Private Static bool enresult = false;
// Mark whether the decryption is successful or not
Private Static bool deresult = false;
///
Private system. componentmodel. Container components = NULL;
Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
//
// Todo: add Any constructor code after initializecomponent calls
//
}
///
/// Clear all resources in use.
///
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
///
/// The designer supports the required methods-do not use the code editor to modify
/// Content of 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 ();
//
// 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 = "encrypted ";
This. button1.click + = new system. eventhandler (this. button#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 = "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
///
/// Main entry point of the application.
///
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
// Encrypted file button
Private void button#click (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 );
}
// Encryption function
Public void encryption (string Textbox, string readfile, string writefile)
{
Try
{
If (textbox. length> = 8 & textbox. Length <= 16) // you can specify the password size.
{
Byte [] Key = system. Text. encoding. Default. getbytes (textbox );
Byte [] IV = rij. IV;
Rijndael crypt = Rijndael. Create ();
Icryptotransform transform = crypt. createencryptor (Key, IV );
// Write the file
Filestream fswrite = new filestream (writefile, filemode. Create );
Cryptostream cs = new cryptostream (fswrite, transform, cryptostreammode. Write );
// Open the file
Filestream fsread = new filestream (readfile, filemode. Open );
Int length;
While (length = fsread. readbyte ())! =-1)
CS. writebyte (byte) length );
Fsread. Close ();
CS. Close ();
Fswrite. Close ();
Enresult = true; // encrypted successfully
MessageBox. Show ("encryption task completed successfully! ");
}
Else
{
MessageBox. Show ("the minimum length of the password is 8 characters, and the maximum length is 16 characters! ");
Return;
}
}
Catch (exception E)
{
MessageBox. Show (E. tostring ());
}
}
// Decryption Function
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 );
// Write the decrypted result into the 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; // decryption successful
MessageBox. Show ("the decryption task has been completed successfully! ");
}
Else
{
MessageBox. Show ("the minimum length of the password is 8 characters, and the maximum length is 16 characters! ");
Return;
}
}
Catch (exception E)
{
MessageBox. Show (E. tostring ());
}
}
// Decrypt the file
Private void button2_click (Object sender, system. eventargs E)
{
Decryption (textbox3.text, textbox1.text, textbox2.text );
If (checkbox1.checked = true & deresult = true)
Deletefile (textbox1.text );
}
// The button used to open the file
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 = SaveFile. filename;
}
// Get the file extension
Private string getfileext (string filename)
{
Try
{
Char [] Point = new char [] {'.'};
String [] filename2 = filename. Split (point );
Return filename2 [1];
}
Catch
{
Return NULL;
}
}
// Functions used to delete objects
Public void deletefile (string filename)
{
Try
{
File. Delete (filename );
}
Catch (exception E)
{
MessageBox. Show (E. tostring ());
}
}
}
}