Encryption 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 description of the 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;
///
The required designer variable.
Create an instance
RijndaelManaged Rij = new RijndaelManaged ();//
global variable, tag file extension
private static string Ext=null;
Mark encryption successful or not
private static bool Enresult = FALSE;
tag that marks the success of a decryption
private static bool Deresult = FALSE;
Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
//
TODO: Add any constructor code after the InitializeComponent call
//
}
///
Clean up all resources that are in use.
///
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
Code generated #region the Windows forms Designer
///
Designer supports required methods-do not use the Code editor to modify
The contents 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 = "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 = "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, 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 to encrypt files
private void Button1_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);
}
Functions used for encryption
public void encryption (string textbox,string readfile,string WriteFile)
{
Try
{
if (textbox.length >=8 && textbox.length<=16)//The size of the character that determines the password
{
byte [] key = System.Text.Encoding.Default.GetBytes (TextBox);
byte [] IV = RIJ.IV;
Rijndael crypt = Rijndael.create ();
ICryptoTransform transform = crypt. CreateEncryptor (key, iv);
Write in File
FileStream fswrite = new FileStream (writefile,filemode.create);
CryptoStream cs = new CryptoStream (fswrite, transform, cryptostreammode.write);
Open 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;//Successful encryption
MessageBox.Show ("The encryption task has been successfully completed!") ");
}
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 ());
}
}
Functions for decrypting
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 results 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; Successfully decrypted
MessageBox.Show ("The decryption task has been successfully completed!") ");
}
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 ());
}
}
Decrypting files
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);
}
button to open a 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 name extension
private string Getfileext (string filename)
{
Try
{
char [] point = new char[] {'. '};
string [] filename2 = filename. Split (point);
return filename2[1];
}
Catch
{
return null;
}
}
function to delete a file
public void DeleteFile (string filename)
{
Try
{
File.delete (filename);
}
catch (Exception e)
{
MessageBox.Show (E.tostring ());
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.