C #---A program for asymmetric encryption

Source: Internet
Author: User
Tags filter crypt decrypt decrypt text flush asymmetric encryption
Program | encryption 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;

Namespace Asymmetric Encryption
{
<summary>
Summary description of the Form1.
</summary>
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.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.Windows.Forms.Button button7;
Private System.Windows.Forms.Button Button8;
<summary>
The 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;
private static byte [] onebytes;


</summary>
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
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.Windows.Forms.Label ();
This.save = new System.Windows.Forms.SaveFileDialog ();
This.open = new System.Windows.Forms.OpenFileDialog ();
This.tabControl1.SuspendLayout ();
This.tabPage1.SuspendLayout ();
This.tabPage2.SuspendLayout ();
This. SuspendLayout ();
//
TabControl1
//
THIS.TABCONTROL1.CONTROLS.ADD (THIS.TABPAGE1);
THIS.TABCONTROL1.CONTROLS.ADD (This.tabpage2);
This.tabControl1.Location = new System.Drawing.Point (32, 64);
This.tabControl1.Name = "TabControl1";
This.tabControl1.SelectedIndex = 0;
This.tabControl1.Size = new System.Drawing.Size (416, 280);
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 (408, 255);
This.tabPage1.TabIndex = 0;
This.tabPage1.Text = "Get the key";
//
Button6
//
This.button6.Location = new System.Drawing.Point (72, 168);
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 (200, 16);
This.button3.Name = "Button3";
This.button3.TabIndex = 3;
This.button3.Text = "Save private key";
This.button3.Click + = new System.EventHandler (This.button3_click);
//
Button2
//
This.button2.Location = new System.Drawing.Point (128, 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 (256, 96);
This.richtext.TabIndex = 1;
This.richtext.Text = "RichTextBox1";
//
Button1
//
This.button1.Location = new System.Drawing.Point (8, 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 (408, 255);
This.tabPage2.TabIndex = 1;
This.tabPage2.Text = "Encrypt and decrypt text";
//
Button8
//
This.button8.Location = new System.Drawing.Point (216, 72);
This.button8.Name = "Button8";
This.button8.TabIndex = 9;
This.button8.Text = "Read Private key";
This.button8.Click + = new System.EventHandler (This.button8_click);
//
Button7
//
This.button7.Location = new System.Drawing.Point (56, 72);
This.button7.Name = "button7";
This.button7.TabIndex = 8;
This.button7.Text = "reading public key";
This.button7.Click + = new System.EventHandler (This.button7_click);
//
Label3
//
This.label3.Location = new System.Drawing.Point (200, 120);
This.label3.Name = "Label3";
This.label3.Size = new System.Drawing.Size (100, 16);
This.label3.TabIndex = 7;
This.label3.Text = "decryption result";
//
Richtext3
//
This.richtext3.Location = new System.Drawing.Point (200, 144);
This.richtext3.Name = "RICHTEXT3";
This.richtext3.Size = new System.Drawing.Size (120, 72);
This.richtext3.TabIndex = 6;
This.richtext3.Text = "RichTextBox2";
//
Label2
//
This.label2.Location = new System.Drawing.Point (48, 120);
This.label2.Name = "Label2";
This.label2.Size = new System.Drawing.Size (128, 16);
This.label2.TabIndex = 5;
This.label2.Text = "Encrypt window:";
//
Richtext2
//
This.richtext2.Location = new System.Drawing.Point (40, 144);
This.richtext2.Name = "Richtext2";
This.richtext2.Size = new System.Drawing.Size (120, 72);
This.richtext2.TabIndex = 4;
This.richtext2.Text = "RichTextBox2";
//
Button5
//
This.button5.Location = new System.Drawing.Point (200, 96);
This.button5.Name = "Button5";
This.button5.Size = new System.Drawing.Size (112, 23);
This.button5.TabIndex = 3;
This.button5.Text = "Decryption using private key";
This.button5.Click + = new System.EventHandler (This.button5_click);
//
Button4
//
This.button4.Location = new System.Drawing.Point (48, 96);
This.button4.Name = "Button4";
This.button4.Size = new System.Drawing.Size (96, 23);
This.button4.TabIndex = 2;
This.button4.Text = "Use 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 (168, 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";
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (504, 357);
This. Controls.Add (THIS.TABCONTROL1);
This. Name = "Form1";
This. Text = "Form1";
This.tabControl1.ResumeLayout (FALSE);
This.tabPage1.ResumeLayout (FALSE);
This.tabPage2.ResumeLayout (FALSE);
This. ResumeLayout (FALSE);

}
#endregion

<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
Get key information
private void Button1_Click (object sender, System.EventArgs e)
{
Crypt=new RSACryptoServiceProvider ();
Publickey=crypt. Toxmlstring (FALSE);
RichText. text= "Export secret key: \ n" +publickey+ "\ n";
Privatekey=crypt. Toxmlstring (TRUE);
String info= "Only export the public key: \ n" +privatekey+ "\ n";
RichText. AppendText (info);
Crypt. Clear ();

}
Save Public Key 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;

}
Write key information to a 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 (Privatekey);
One. Flush ();
Two. Flush ();
One. Close ();
Two. Close ();
MessageBox.Show ("Save the public key and keys successfully!") ");

}
Encrypt with a public key
private void Button4_Click (object sender, System.EventArgs e)
{

Crypt=new RSACryptoServiceProvider ();
UTF8Encoding enc=new utf8encoding ();
Bytes=enc. GetBytes (TextBox1.Text);
Crypt. Fromxmlstring (Readpublickey);
bytes = Crypt. Encrypt (Bytes,false);
String Encryttext=enc. GetString (bytes);//encryptbyte);
Richtext2. text= "Encryption result: \ n" +encryttext+ "\ n" + "Encryption end!" ";
}

private void Button5_click (object sender, System.EventArgs e)
{

UTF8Encoding enc=new utf8encoding ();
byte [] decryptbyte;
Crypt. Fromxmlstring (Readprivatekey);
Decryptbyte = Crypt. Decrypt (Bytes,false);
String Decrypttext=enc. GetString (Decryptbyte);
Richtext3. Text = "decryption result: \ n" + decrypttext + "\ n" + "decryption end!" " ;

}
Reading public key information from a file
private void Button7_click (object sender, System.EventArgs e)
{
StreamReader SR;
Open = new OpenFileDialog ();
Open. Filter= "Text File (*.txt) |*.txt| All File (*.*) |*.* ";
Open. ShowDialog ();
if (open. ShowDialog () ==dialogresult.ok)
{
sr = new StreamReader (open. FILENAME,UTF8ENCODING.UTF8);
}
Else
{
MessageBox.Show ("An error has occurred!") ");
Return
}
Readpublickey = Sr. ReadToEnd ();
Sr. Close ();
}
Reading private key information from a file
private void Button8_click (object sender, System.EventArgs e)
{
Open = new OpenFileDialog ();
Open. Filter= "Text File (*.txt) |*.txt| All File (*.*) |*.* ";
Open. ShowDialog ();
StreamReader sr = new StreamReader (open. FILENAME,UTF8ENCODING.UTF8);
Readprivatekey = Sr. ReadToEnd ();
Sr. Close ();
}



}
}



Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.