C# 关于DSA&RSA两种不同加密方式的例子
常用的两种加密方式.
- DSA 
 

 以下是代码 

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

 {
{
 /**////
 /**//// /// Form1 的摘要说明。
 /// Form1 的摘要说明。 ///
 /// public class Form2 : System.Windows.Forms.Form
 public class Form2 : System.Windows.Forms.Form
 
  {
{ private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button2;
 /**////
  /**//// /// 必需的设计器变量。
  /// 必需的设计器变量。 
  private System.Windows.Forms.TextBox textBox3;
  private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.OpenFileDialog openfile;
  private System.Windows.Forms.OpenFileDialog openfile; private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button3; private System.Windows.Forms.SaveFileDialog savefile;
  private System.Windows.Forms.SaveFileDialog savefile; private System.Windows.Forms.Button button4;
  private System.Windows.Forms.Button button4; private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label2; private System.Windows.Forms.CheckBox checkBox1;
  private System.Windows.Forms.CheckBox checkBox1;
 //创建实例
//创建实例 RijndaelManaged rij = new RijndaelManaged();//
  RijndaelManaged rij = new RijndaelManaged();// //全局变量,标记文件的扩展名
  //全局变量,标记文件的扩展名 private static string ext=null;
  private static string ext=null; //标记加密成功与否
  //标记加密成功与否 private static bool enresult = false;
  private static bool enresult = false; //标记解密成功与否的标记
  //标记解密成功与否的标记 private static bool deresult = false;
  private static bool deresult = false; 
 
 /**////
  /**//// private System.ComponentModel.Container components = null;
  private System.ComponentModel.Container components = null;
 public Form2()
public Form2()
 
   {
{ //
   // // Windows 窗体设计器支持所必需的
   // Windows 窗体设计器支持所必需的 //
   // InitializeComponent();
   InitializeComponent();
 //
// // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 //
   // }
  }

 /**////
/**//// /// 清理所有正在使用的资源。
  /// 清理所有正在使用的资源。 ///
  /// protected override void Dispose( bool disposing )
  protected override void Dispose( bool disposing )
 
   {
{ if( disposing )
   if( disposing )
 
    {
{ if (components != null)
    if (components != null)
 
     {
{ components.Dispose();
     components.Dispose(); }
    } }
   } base.Dispose( disposing );
   base.Dispose( disposing ); }
  }

 Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
 /**////
  /**//// /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。
  /// 此方法的内容。 ///
  /// private void InitializeComponent()
  private void InitializeComponent()
 
   {
{ this.textBox1 = new System.Windows.Forms.TextBox();
   this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox();
   this.textBox2 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button();
   this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button(); this.textBox3 = new System.Windows.Forms.TextBox();
   this.textBox3 = new System.Windows.Forms.TextBox(); this.openfile = new System.Windows.Forms.OpenFileDialog();
   this.openfile = new System.Windows.Forms.OpenFileDialog(); this.button3 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button(); this.savefile = new System.Windows.Forms.SaveFileDialog();
   this.savefile = new System.Windows.Forms.SaveFileDialog(); this.button4 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label();
   this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label(); this.checkBox1 = new System.Windows.Forms.CheckBox();
   this.checkBox1 = new System.Windows.Forms.CheckBox(); this.SuspendLayout();
   this.SuspendLayout(); //
   // // textBox1
   // textBox1 //
   // this.textBox1.Location = new System.Drawing.Point(73, 89);
   this.textBox1.Location = new System.Drawing.Point(73, 89); this.textBox1.Name = "textBox1";
   this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(140, 20);
   this.textBox1.Size = new System.Drawing.Size(140, 20); this.textBox1.TabIndex = 0;
   this.textBox1.TabIndex = 0; this.textBox1.Text = "";
   this.textBox1.Text = ""; //
   // // textBox2
   // textBox2 //
   // this.textBox2.Location = new System.Drawing.Point(73, 126);
   this.textBox2.Location = new System.Drawing.Point(73, 126); this.textBox2.Name = "textBox2";
   this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(140, 20);
   this.textBox2.Size = new System.Drawing.Size(140, 20); this.textBox2.TabIndex = 1;
   this.textBox2.TabIndex = 1; this.textBox2.Text = "";
   this.textBox2.Text = ""; //
   // // button1
   // button1 //
   // this.button1.Location = new System.Drawing.Point(73, 186);
   this.button1.Location = new System.Drawing.Point(73, 186); this.button1.Name = "button1";
   this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(63, 21);
   this.button1.Size = new System.Drawing.Size(63, 21); this.button1.TabIndex = 2;
   this.button1.TabIndex = 2; this.button1.Text = "加密";
   this.button1.Text = "加密"; this.button1.Click += new System.EventHandler(this.button1_Click);
   this.button1.Click += new System.EventHandler(this.button1_Click); //
   // // button2
   // button2 //
   // this.button2.Location = new System.Drawing.Point(160, 186);
   this.button2.Location = new System.Drawing.Point(160, 186); this.button2.Name = "button2";
   this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(62, 21);
   this.button2.Size = new System.Drawing.Size(62, 21); this.button2.TabIndex = 3;
   this.button2.TabIndex = 3; this.button2.Text = "解密";
   this.button2.Text = "解密"; this.button2.Click += new System.EventHandler(this.button2_Click);
   this.button2.Click += new System.EventHandler(this.button2_Click); //
   // // textBox3
   // textBox3 //
   // this.textBox3.HideSelection = false;
   this.textBox3.HideSelection = false; this.textBox3.Location = new System.Drawing.Point(73, 45);
   this.textBox3.Location = new System.Drawing.Point(73, 45); this.textBox3.MaxLength = 17;
   this.textBox3.MaxLength = 17; this.textBox3.Name = "textBox3";
   this.textBox3.Name = "textBox3"; this.textBox3.PasswordChar = '$';
   this.textBox3.PasswordChar = '$'; this.textBox3.Size = new System.Drawing.Size(84, 20);
   this.textBox3.Size = new System.Drawing.Size(84, 20); this.textBox3.TabIndex = 4;
   this.textBox3.TabIndex = 4; this.textBox3.Text = "";
   this.textBox3.Text = ""; this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
   this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; //
   // // button3
   // button3 //
   // this.button3.Location = new System.Drawing.Point(233, 89);
   this.button3.Location = new System.Drawing.Point(233, 89); this.button3.Name = "button3";
   this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(34, 21);
   this.button3.Size = new System.Drawing.Size(34, 21); this.button3.TabIndex = 5;
   this.button3.TabIndex = 5; this.button3.Text = "Open";
   this.button3.Text = "Open"; this.button3.Click += new System.EventHandler(this.button3_Click);
   this.button3.Click += new System.EventHandler(this.button3_Click); //
   // // button4
   // button4 //
   // this.button4.Location = new System.Drawing.Point(233, 126);
   this.button4.Location = new System.Drawing.Point(233, 126); this.button4.Name = "button4";
   this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(34, 22);
   this.button4.Size = new System.Drawing.Size(34, 22); this.button4.TabIndex = 6;
   this.button4.TabIndex = 6; this.button4.Text = "Save";
   this.button4.Text = "Save"; this.button4.Click += new System.EventHandler(this.button4_Click);
   this.button4.Click += new System.EventHandler(this.button4_Click); //
   // // label1
   // label1 //
   // this.label1.Location = new System.Drawing.Point(73, 22);
   this.label1.Location = new System.Drawing.Point(73, 22); this.label1.Name = "label1";
   this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(114, 22);
   this.label1.Size = new System.Drawing.Size(114, 22); this.label1.TabIndex = 7;
   this.label1.TabIndex = 7; this.label1.Text = "请输入你的密码:";
   this.label1.Text = "请输入你的密码:"; //
   // // label2
   // label2 //
   // this.label2.Location = new System.Drawing.Point(180, 45);
   this.label2.Location = new System.Drawing.Point(180, 45); this.label2.Name = "label2";
   this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(153, 21);
   this.label2.Size = new System.Drawing.Size(153, 21); this.label2.TabIndex = 8;
   this.label2.TabIndex = 8; this.label2.Text = "(密码大小8至16个字符)";
   this.label2.Text = "(密码大小8至16个字符)"; //
   // // checkBox1
   // checkBox1 //
   // this.checkBox1.Location = new System.Drawing.Point(73, 156);
   this.checkBox1.Location = new System.Drawing.Point(73, 156); this.checkBox1.Name = "checkBox1";
   this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(180, 22);
   this.checkBox1.Size = new System.Drawing.Size(180, 22); this.checkBox1.TabIndex = 9;
   this.checkBox1.TabIndex = 9; this.checkBox1.Text = "删除源文件";
   this.checkBox1.Text = "删除源文件"; //
   // // Form2
   // Form2 //
   // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(379, 258);
   this.ClientSize = new System.Drawing.Size(379, 258); this.Controls.Add(this.checkBox1);
   this.Controls.Add(this.checkBox1); this.Controls.Add(this.label2);
   this.Controls.Add(this.label2); this.Controls.Add(this.label1);
   this.Controls.Add(this.label1); this.Controls.Add(this.button4);
   this.Controls.Add(this.button4); this.Controls.Add(this.button3);
   this.Controls.Add(this.button3); this.Controls.Add(this.textBox3);
   this.Controls.Add(this.textBox3); this.Controls.Add(this.button2);
   this.Controls.Add(this.button2); this.Controls.Add(this.button1);
   this.Controls.Add(this.button1); this.Controls.Add(this.textBox2);
   this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1);
   this.Controls.Add(this.textBox1); this.Name = "Form2";
   this.Name = "Form2"; this.Text = "DSA";
   this.Text = "DSA"; this.ResumeLayout(false);
   this.ResumeLayout(false);
 }
} #endregion
  #endregion

 /**////
/**//// /// 应用程序的主入口点。
  /// 应用程序的主入口点。 ///
  /// //  [STAThread]
//  [STAThread] //  static void Main()
//  static void Main() //  {
//  { //   Application.Run(new Form1());
//   Application.Run(new Form1()); //  }
//  }
 //加密文件的按钮
//加密文件的按钮 private void button1_Click(object sender, System.EventArgs e)
  private void button1_Click(object sender, System.EventArgs e)
 
   {
{ if(textBox1.Text != null || textBox2.Text != null)
   if(textBox1.Text != null || textBox2.Text != null) encryption(textBox3.Text,textBox1.Text,textBox2.Text);
    encryption(textBox3.Text,textBox1.Text,textBox2.Text); if(checkBox1.Checked==true && enresult==true)
   if(checkBox1.Checked==true && enresult==true) DeleteFile(textBox1.Text);
    DeleteFile(textBox1.Text); }
  } //用于加密的函数
  //用于加密的函数 public void encryption(string textBox,string readfile,string writefile)
  public void encryption(string textBox,string readfile,string writefile)
 
   {
{ try
   try
 
    {
{ if(textBox.Length >=8 && textBox.Length<=16 )//判断密码的字符的大小
    if(textBox.Length >=8 && textBox.Length<=16 )//判断密码的字符的大小
 
     {
{ 
    byte [] key = System.Text.Encoding.Default.GetBytes( textBox );
     byte [] key = System.Text.Encoding.Default.GetBytes( textBox ); byte [] iv  = rij.IV;
     byte [] iv  = rij.IV; Rijndael crypt = Rijndael.Create();
     Rijndael crypt = Rijndael.Create(); ICryptoTransform transform = crypt.CreateEncryptor(key ,iv);
     ICryptoTransform transform = crypt.CreateEncryptor(key ,iv);   //写进文件
     //写进文件 FileStream fswrite = new FileStream(writefile,FileMode.Create);
     FileStream fswrite = new FileStream(writefile,FileMode.Create); CryptoStream cs = new CryptoStream( fswrite ,transform ,CryptoStreamMode.Write );
     CryptoStream cs = new CryptoStream( fswrite ,transform ,CryptoStreamMode.Write );  //打开文件
     //打开文件 FileStream fsread = new FileStream(readfile,FileMode.Open);
     FileStream fsread = new FileStream(readfile,FileMode.Open); int length;
     int length; while((length = fsread.ReadByte() )!= -1)
     while((length = fsread.ReadByte() )!= -1) cs.WriteByte((byte)length);
      cs.WriteByte((byte)length);
 fsread.Close();
fsread.Close(); cs.Close();
     cs.Close(); fswrite.Close();
     fswrite.Close(); enresult = true ;//成功加密
     enresult = true ;//成功加密 MessageBox.Show("已经成功完成加密任务!");
     MessageBox.Show("已经成功完成加密任务!"); }
    } else
    else
 
     {
{ MessageBox.Show("密码的最小长度为8个字符,最大长度为16个字符!");
       MessageBox.Show("密码的最小长度为8个字符,最大长度为16个字符!"); return ;
     return ; }
    } }
   } catch (Exception e)
   catch (Exception e)
 
    {
{ MessageBox.Show(e.ToString());
    MessageBox.Show(e.ToString()); }
   } }
  } //用于解密的函数
  //用于解密的函数 public void decryption(string textBox,string readfile,string writefile)
  public void decryption(string textBox,string readfile,string writefile)
 
   {
{ try
   try
 
    {
{ if(textBox.Length >=8 && textBox.Length<=16 )
    if(textBox.Length >=8 && textBox.Length<=16 )
 
     {
{ byte [] key = System.Text.Encoding.Default.GetBytes( textBox );
     byte [] key = System.Text.Encoding.Default.GetBytes( textBox ); byte [] iv  = rij.IV;
     byte [] iv  = rij.IV; Rijndael crypt = Rijndael.Create();
     Rijndael crypt = Rijndael.Create(); ICryptoTransform transform = crypt.CreateDecryptor(key,iv);
     ICryptoTransform transform = crypt.CreateDecryptor(key,iv);   //读取加密后的文件
     //读取加密后的文件 FileStream fsopen = new FileStream(readfile,FileMode.Open);
     FileStream fsopen = new FileStream(readfile,FileMode.Open); CryptoStream cs = new CryptoStream( fsopen ,transform ,CryptoStreamMode.Read );
     CryptoStream cs = new CryptoStream( fsopen ,transform ,CryptoStreamMode.Read );  //把解密后的结果写进文件
     //把解密后的结果写进文件 FileStream fswrite = new FileStream(writefile,FileMode.OpenOrCreate);
     FileStream fswrite = new FileStream(writefile,FileMode.OpenOrCreate); int length;
     int length; while ((length = cs.ReadByte()) != -1)
     while ((length = cs.ReadByte()) != -1) fswrite.WriteByte((byte)length);
      fswrite.WriteByte((byte)length);
 fswrite.Close();
fswrite.Close(); cs.Close();
     cs.Close(); fsopen.Close();
     fsopen.Close(); deresult=true; //成功解密
     deresult=true; //成功解密 MessageBox.Show("已经成功完成解密任务!");
     MessageBox.Show("已经成功完成解密任务!"); }
    } else
    else
 
     {
{ MessageBox.Show("密码的最小长度为8个字符,最大长度为16个字符!");
       MessageBox.Show("密码的最小长度为8个字符,最大长度为16个字符!"); return ;
     return ; }
    } }
   } catch (Exception e)
   catch (Exception e)
 
    {
{ MessageBox.Show(e.ToString());
    MessageBox.Show(e.ToString()); }
   } }
  } //解密文件
  //解密文件 private void button2_Click(object sender, System.EventArgs e)
  private void button2_Click(object sender, System.EventArgs e)
 
   {
{ decryption( textBox3.Text,textBox1.Text,textBox2.Text ) ;
   decryption( textBox3.Text,textBox1.Text,textBox2.Text ) ; if(checkBox1.Checked==true && deresult==true)
   if(checkBox1.Checked==true && deresult==true) DeleteFile(textBox1.Text);
    DeleteFile(textBox1.Text); }
  } //用于打开文件的按钮
  //用于打开文件的按钮 private void button3_Click(object sender, System.EventArgs e)
  private void button3_Click(object sender, System.EventArgs e)
 
   {
{ openfile = new OpenFileDialog();openfile.Filter ="All files (*.*)|*.*" ;
   openfile = new OpenFileDialog();openfile.Filter ="All files (*.*)|*.*" ;    openfile.ShowDialog();
   openfile.ShowDialog(); textBox1.Text = openfile.FileName;
   textBox1.Text = openfile.FileName; ext = getfileext(openfile.FileName);
   ext = getfileext(openfile.FileName); }
  }
 private void button4_Click(object sender, System.EventArgs e)
private void button4_Click(object sender, System.EventArgs e)
 
   {
{ savefile = new SaveFileDialog();
   savefile = new SaveFileDialog(); savefile.Filter =ext + " files" +"(*."+ext+")|*."+ ext +"|All files (*.*)|*.*" ;
   savefile.Filter =ext + " files" +"(*."+ext+")|*."+ ext +"|All files (*.*)|*.*" ; savefile.ShowDialog();
   savefile.ShowDialog(); textBox2.Text = savefile.FileName;
   textBox2.Text = savefile.FileName; }
  } //得到文件的扩展名
  //得到文件的扩展名 private string getfileext(string filename)
  private string getfileext(string filename)
 
   {
{ try
   try
 
    {
{
 char [] point = new char[]
    char [] point = new char[]  {'.'};
{'.'}; string [] filename2 = filename.Split(point);
    string [] filename2 = filename.Split(point); return filename2[1];
    return filename2[1]; }
   } catch
   catch
 
    {
{ return null;
    return null; }
   } }
  } //删除文件的函数
  //删除文件的函数 public void DeleteFile(string filename)
  public void DeleteFile(string filename)
 
   {
{ try
   try
 
    {
{ File.Delete(filename);
    File.Delete(filename);    }
   } catch (Exception e)
   catch (Exception e)
 
    {
{ MessageBox.Show(e.ToString());
    MessageBox.Show(e.ToString()); }
   } }
  }

 }
 } }
}
- RSA
 
 
以下是代码

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

 namespace Journal_Entry_Sub_System
namespace Journal_Entry_Sub_System


 {
{

 /**//// <summary>
 /**//// <summary>
 /// Form1 的摘要说明。
 /// Form1 的摘要说明。
 /// </summary>
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 public class Form1 : System.Windows.Forms.Form

 
  {
{
 private System.Windows.Forms.TabControl tabControl1;
  private System.Windows.Forms.TabControl tabControl1;
 private System.Windows.Forms.TabPage tabPage1;
  private System.Windows.Forms.TabPage tabPage1;
 private System.Windows.Forms.TabPage tabPage2;
  private System.Windows.Forms.TabPage tabPage2;
 private System.Windows.Forms.TabPage tabPage3;
  private System.Windows.Forms.TabPage tabPage3;
 private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button1;
 private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button2;
 private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button3;
 private System.Windows.Forms.SaveFileDialog save;
  private System.Windows.Forms.SaveFileDialog save;
 private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label1;
 private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.TextBox textBox1;
 private System.Windows.Forms.Button button4;
  private System.Windows.Forms.Button button4;
 private System.Windows.Forms.Button button5;
  private System.Windows.Forms.Button button5;
 private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label2;
 private System.Windows.Forms.RichTextBox richtext2;
  private System.Windows.Forms.RichTextBox richtext2;
 private System.Windows.Forms.OpenFileDialog open;
  private System.Windows.Forms.OpenFileDialog open;
 private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label3;
 private System.Windows.Forms.RichTextBox richtext3;
  private System.Windows.Forms.RichTextBox richtext3;
 private System.Windows.Forms.RichTextBox richtext;
  private System.Windows.Forms.RichTextBox richtext;
 private System.Windows.Forms.Button button6;
  private System.Windows.Forms.Button button6;
 private System.Windows.Forms.Button button7;
  private System.Windows.Forms.Button button7;
 private System.Windows.Forms.Button button8;
  private System.Windows.Forms.Button button8;
 private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.TextBox textBox2;
 private System.Windows.Forms.Label label4;
  private System.Windows.Forms.Label label4;
 private System.Windows.Forms.Button button9;
  private System.Windows.Forms.Button button9;
 private System.Windows.Forms.TextBox textBox3;
  private System.Windows.Forms.TextBox textBox3;
 private System.Windows.Forms.Label label5;
  private System.Windows.Forms.Label label5;
 private System.Windows.Forms.Button button10;
  private System.Windows.Forms.Button button10;
 private System.Windows.Forms.Label label6;
  private System.Windows.Forms.Label label6;
 private System.Windows.Forms.Button button11;
  private System.Windows.Forms.Button button11;
 private System.Windows.Forms.Button button12;
  private System.Windows.Forms.Button button12;
 private System.Windows.Forms.RichTextBox richtext4;
  private System.Windows.Forms.RichTextBox richtext4;
 private System.Windows.Forms.Button button13;
  private System.Windows.Forms.Button button13;
 private System.Windows.Forms.Button button14;
  private System.Windows.Forms.Button button14;
 private System.Windows.Forms.Button button15;
  private System.Windows.Forms.Button button15;
 private System.Windows.Forms.TabPage tabPage4;
  private System.Windows.Forms.TabPage tabPage4;
 private System.Windows.Forms.TextBox textBox4;
  private System.Windows.Forms.TextBox textBox4;
 private System.Windows.Forms.Button button16;
  private System.Windows.Forms.Button button16;
 private System.Windows.Forms.TextBox textBox5;
  private System.Windows.Forms.TextBox textBox5;
 private System.Windows.Forms.Button button17;
  private System.Windows.Forms.Button button17;
 private System.Windows.Forms.Button button18;
  private System.Windows.Forms.Button button18;
 private System.Windows.Forms.Button button19;
  private System.Windows.Forms.Button button19;
 private System.Windows.Forms.Button button20;
  private System.Windows.Forms.Button button20;
 private System.Windows.Forms.Button button21;
  private System.Windows.Forms.Button button21;
 private System.Windows.Forms.RichTextBox richtext5;
  private System.Windows.Forms.RichTextBox richtext5;

 /**//// <summary>
  /**//// <summary>
 /// 必需的设计器变量。
  /// 必需的设计器变量。
 private static RSACryptoServiceProvider crypt;
  private static RSACryptoServiceProvider crypt;
 private static string  privatekey;
  private static string  privatekey;
 private static string  publickey;
  private static string  publickey;
 private static byte [] bytes;
  private static byte [] bytes;
 private static string  publicinfo;
  private static string  publicinfo;
 private static string  privateinfo;
  private static string  privateinfo;
 private static string  readpublickey;
  private static string  readpublickey;
 private static string  readprivatekey;
  private static string  readprivatekey;
 //文件打开和是否读取钥匙的标记
  //文件打开和是否读取钥匙的标记
 private bool openla = false;
  private bool openla = false;
 private bool savela = false;
  private bool savela = false;
 private bool readpubkey=false;
  private bool readpubkey=false;
 private bool readprikey=false;
  private bool readprikey=false;
 private Thread th;
  private Thread th; 

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

 public Form1()
public Form1()

 
   {
{
 //
   //
 // Windows 窗体设计器支持所必需的
   // Windows 窗体设计器支持所必需的
 //
   //
 InitializeComponent();
   InitializeComponent();

 //
//
 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 //
   //
 }
  }

 /**//// <summary>
  /**//// <summary>
 /// 清理所有正在使用的资源。
  /// 清理所有正在使用的资源。
 /// </summary>
  /// </summary>
 protected override void Dispose( bool disposing )
  protected override void Dispose( bool disposing )

 
   {
{
 if( disposing )
   if( disposing )

 
    {
{
 if (components != null)
    if (components != null)

 
     {
{
 components.Dispose();
     components.Dispose();
 }
    }
 }
   }
 base.Dispose( disposing );
   base.Dispose( disposing );
 }
  }


 Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码

 /**//// <summary>
  /**//// <summary>
 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 /// 此方法的内容。
  /// 此方法的内容。
 /// </summary>
  /// </summary>
 private void InitializeComponent()
  private void InitializeComponent()

 
   {
{
 this.tabControl1 = new System.Windows.Forms.TabControl();
   this.tabControl1 = new System.Windows.Forms.TabControl();
 this.tabPage1 = new System.Windows.Forms.TabPage();
   this.tabPage1 = new System.Windows.Forms.TabPage();
 this.button6 = new System.Windows.Forms.Button();
   this.button6 = new System.Windows.Forms.Button();
 this.button3 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
 this.button2 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
 this.richtext = new System.Windows.Forms.RichTextBox();
   this.richtext = new System.Windows.Forms.RichTextBox();
 this.button1 = new System.Windows.Forms.Button();
   this.button1 = new System.Windows.Forms.Button();
 this.tabPage2 = new System.Windows.Forms.TabPage();
   this.tabPage2 = new System.Windows.Forms.TabPage();
 this.button8 = new System.Windows.Forms.Button();
   this.button8 = new System.Windows.Forms.Button();
 this.button7 = new System.Windows.Forms.Button();
   this.button7 = new System.Windows.Forms.Button();
 this.label3 = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
 this.richtext3 = new System.Windows.Forms.RichTextBox();
   this.richtext3 = new System.Windows.Forms.RichTextBox();
 this.label2 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
 this.richtext2 = new System.Windows.Forms.RichTextBox();
   this.richtext2 = new System.Windows.Forms.RichTextBox();
 this.button5 = new System.Windows.Forms.Button();
   this.button5 = new System.Windows.Forms.Button();
 this.button4 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
 this.textBox1 = new System.Windows.Forms.TextBox();
   this.textBox1 = new System.Windows.Forms.TextBox();
 this.label1 = new System.Windows.Forms.Label();
   this.label1 = new System.Windows.Forms.Label();
 this.tabPage3 = new System.Windows.Forms.TabPage();
   this.tabPage3 = new System.Windows.Forms.TabPage();
 this.button15 = new System.Windows.Forms.Button();
   this.button15 = new System.Windows.Forms.Button();
 this.button14 = new System.Windows.Forms.Button();
   this.button14 = new System.Windows.Forms.Button();
 this.button13 = new System.Windows.Forms.Button();
   this.button13 = new System.Windows.Forms.Button();
 this.button12 = new System.Windows.Forms.Button();
   this.button12 = new System.Windows.Forms.Button();
 this.button11 = new System.Windows.Forms.Button();
   this.button11 = new System.Windows.Forms.Button();
 this.richtext4 = new System.Windows.Forms.RichTextBox();
   this.richtext4 = new System.Windows.Forms.RichTextBox();
 this.label6 = new System.Windows.Forms.Label();
   this.label6 = new System.Windows.Forms.Label();
 this.button10 = new System.Windows.Forms.Button();
   this.button10 = new System.Windows.Forms.Button();
 this.label5 = new System.Windows.Forms.Label();
   this.label5 = new System.Windows.Forms.Label();
 this.textBox3 = new System.Windows.Forms.TextBox();
   this.textBox3 = new System.Windows.Forms.TextBox();
 this.button9 = new System.Windows.Forms.Button();
   this.button9 = new System.Windows.Forms.Button();
 this.label4 = new System.Windows.Forms.Label();
   this.label4 = new System.Windows.Forms.Label();
 this.textBox2 = new System.Windows.Forms.TextBox();
   this.textBox2 = new System.Windows.Forms.TextBox();
 this.tabPage4 = new System.Windows.Forms.TabPage();
   this.tabPage4 = new System.Windows.Forms.TabPage();
 this.richtext5 = new System.Windows.Forms.RichTextBox();
   this.richtext5 = new System.Windows.Forms.RichTextBox();
 this.button21 = new System.Windows.Forms.Button();
   this.button21 = new System.Windows.Forms.Button();
 this.button20 = new System.Windows.Forms.Button();
   this.button20 = new System.Windows.Forms.Button();
 this.button19 = new System.Windows.Forms.Button();
   this.button19 = new System.Windows.Forms.Button();
 this.button18 = new System.Windows.Forms.Button();
   this.button18 = new System.Windows.Forms.Button();
 this.button17 = new System.Windows.Forms.Button();
   this.button17 = new System.Windows.Forms.Button();
 this.textBox5 = new System.Windows.Forms.TextBox();
   this.textBox5 = new System.Windows.Forms.TextBox();
 this.button16 = new System.Windows.Forms.Button();
   this.button16 = new System.Windows.Forms.Button();
 this.textBox4 = new System.Windows.Forms.TextBox();
   this.textBox4 = new System.Windows.Forms.TextBox();
 this.save = new System.Windows.Forms.SaveFileDialog();
   this.save = new System.Windows.Forms.SaveFileDialog();
 this.open = new System.Windows.Forms.OpenFileDialog();
   this.open = new System.Windows.Forms.OpenFileDialog();
 this.tabControl1.SuspendLayout();
   this.tabControl1.SuspendLayout();
 this.tabPage1.SuspendLayout();
   this.tabPage1.SuspendLayout();
 this.tabPage2.SuspendLayout();
   this.tabPage2.SuspendLayout();
 this.tabPage3.SuspendLayout();
   this.tabPage3.SuspendLayout();
 this.tabPage4.SuspendLayout();
   this.tabPage4.SuspendLayout();
 this.SuspendLayout();
   this.SuspendLayout();
 //
   //
 // tabControl1
   // tabControl1
 //
   //
 this.tabControl1.Controls.Add(this.tabPage1);
   this.tabControl1.Controls.Add(this.tabPage1);
 this.tabControl1.Controls.Add(this.tabPage2);
   this.tabControl1.Controls.Add(this.tabPage2);
 this.tabControl1.Controls.Add(this.tabPage3);
   this.tabControl1.Controls.Add(this.tabPage3);
 this.tabControl1.Controls.Add(this.tabPage4);
   this.tabControl1.Controls.Add(this.tabPage4);
 this.tabControl1.Location = new System.Drawing.Point(7, 59);
   this.tabControl1.Location = new System.Drawing.Point(7, 59);
 this.tabControl1.Name = "tabControl1";
   this.tabControl1.Name = "tabControl1";
 this.tabControl1.SelectedIndex = 0;
   this.tabControl1.SelectedIndex = 0;
 this.tabControl1.Size = new System.Drawing.Size(473, 342);
   this.tabControl1.Size = new System.Drawing.Size(473, 342);
 this.tabControl1.TabIndex = 0;
   this.tabControl1.TabIndex = 0;
 //
   //
 // tabPage1
   // tabPage1
 //
   //
 this.tabPage1.Controls.Add(this.button6);
   this.tabPage1.Controls.Add(this.button6);
 this.tabPage1.Controls.Add(this.button3);
   this.tabPage1.Controls.Add(this.button3);
 this.tabPage1.Controls.Add(this.button2);
   this.tabPage1.Controls.Add(this.button2);
 this.tabPage1.Controls.Add(this.richtext);
   this.tabPage1.Controls.Add(this.richtext);
 this.tabPage1.Controls.Add(this.button1);
   this.tabPage1.Controls.Add(this.button1);
 this.tabPage1.Location = new System.Drawing.Point(4, 22);
   this.tabPage1.Location = new System.Drawing.Point(4, 22);
 this.tabPage1.Name = "tabPage1";
   this.tabPage1.Name = "tabPage1";
 this.tabPage1.Size = new System.Drawing.Size(465, 316);
   this.tabPage1.Size = new System.Drawing.Size(465, 316);
 this.tabPage1.TabIndex = 0;
   this.tabPage1.TabIndex = 0;
 this.tabPage1.Text = "得到钥匙";
   this.tabPage1.Text = "得到钥匙";
 //
   //
 // button6
   // button6
 //
   //
 this.button6.Location = new System.Drawing.Point(180, 267);
   this.button6.Location = new System.Drawing.Point(180, 267);
 this.button6.Name = "button6";
   this.button6.Name = "button6";
 this.button6.Size = new System.Drawing.Size(62, 22);
   this.button6.Size = new System.Drawing.Size(62, 22);
 this.button6.TabIndex = 4;
   this.button6.TabIndex = 4;
 this.button6.Text = "写入文件";
   this.button6.Text = "写入文件";
 this.button6.Click += new System.EventHandler(this.button6_Click);
   this.button6.Click += new System.EventHandler(this.button6_Click);
 //
   //
 // button3
   // button3
 //
   //
 this.button3.Location = new System.Drawing.Point(380, 15);
   this.button3.Location = new System.Drawing.Point(380, 15);
 this.button3.Name = "button3";
   this.button3.Name = "button3";
 this.button3.Size = new System.Drawing.Size(62, 21);
   this.button3.Size = new System.Drawing.Size(62, 21);
 this.button3.TabIndex = 3;
   this.button3.TabIndex = 3;
 this.button3.Text = "保存私匙";
   this.button3.Text = "保存私匙";
 this.button3.Click += new System.EventHandler(this.button3_Click);
   this.button3.Click += new System.EventHandler(this.button3_Click);
 //
   //
 // button2
   // button2
 //
   //
 this.button2.Location = new System.Drawing.Point(267, 15);
   this.button2.Location = new System.Drawing.Point(267, 15);
 this.button2.Name = "button2";
   this.button2.Name = "button2";
 this.button2.Size = new System.Drawing.Size(62, 21);
   this.button2.Size = new System.Drawing.Size(62, 21);
 this.button2.TabIndex = 2;
   this.button2.TabIndex = 2;
 this.button2.Text = "保存公钥";
   this.button2.Text = "保存公钥";
 this.button2.Click += new System.EventHandler(this.button2_Click);
   this.button2.Click += new System.EventHandler(this.button2_Click);
 //
   //
 // richtext
   // richtext
 //
   //
 this.richtext.Font = new System.Drawing.Font("Times New Roman", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
   this.richtext.Font = new System.Drawing.Font("Times New Roman", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
 this.richtext.Location = new System.Drawing.Point(13, 52);
   this.richtext.Location = new System.Drawing.Point(13, 52);
 this.richtext.Name = "richtext";
   this.richtext.Name = "richtext";
 this.richtext.Size = new System.Drawing.Size(434, 171);
   this.richtext.Size = new System.Drawing.Size(434, 171);
 this.richtext.TabIndex = 1;
   this.richtext.TabIndex = 1;
 this.richtext.Text = "richTextBox1";
   this.richtext.Text = "richTextBox1";
 //
   //
 // button1
   // button1
 //
   //
 this.button1.Location = new System.Drawing.Point(113, 15);
   this.button1.Location = new System.Drawing.Point(113, 15);
 this.button1.Name = "button1";
   this.button1.Name = "button1";
 this.button1.Size = new System.Drawing.Size(74, 21);
   this.button1.Size = new System.Drawing.Size(74, 21);
 this.button1.TabIndex = 0;
   this.button1.TabIndex = 0;
 this.button1.Text = "得到钥匙信息";
   this.button1.Text = "得到钥匙信息";
 this.button1.Click += new System.EventHandler(this.button1_Click);
   this.button1.Click += new System.EventHandler(this.button1_Click);
 //
   //
 // tabPage2
   // tabPage2
 //
   //
 this.tabPage2.Controls.Add(this.button8);
   this.tabPage2.Controls.Add(this.button8);
 this.tabPage2.Controls.Add(this.button7);
   this.tabPage2.Controls.Add(this.button7);
 this.tabPage2.Controls.Add(this.label3);
   this.tabPage2.Controls.Add(this.label3);
 this.tabPage2.Controls.Add(this.richtext3);
   this.tabPage2.Controls.Add(this.richtext3);
 this.tabPage2.Controls.Add(this.label2);
   this.tabPage2.Controls.Add(this.label2);
 this.tabPage2.Controls.Add(this.richtext2);
   this.tabPage2.Controls.Add(this.richtext2);
 this.tabPage2.Controls.Add(this.button5);
   this.tabPage2.Controls.Add(this.button5);
 this.tabPage2.Controls.Add(this.button4);
   this.tabPage2.Controls.Add(this.button4);
 this.tabPage2.Controls.Add(this.textBox1);
   this.tabPage2.Controls.Add(this.textBox1);
 this.tabPage2.Controls.Add(this.label1);
   this.tabPage2.Controls.Add(this.label1);
 this.tabPage2.Location = new System.Drawing.Point(4, 22);
   this.tabPage2.Location = new System.Drawing.Point(4, 22);
 this.tabPage2.Name = "tabPage2";
   this.tabPage2.Name = "tabPage2";
 this.tabPage2.Size = new System.Drawing.Size(465, 316);
   this.tabPage2.Size = new System.Drawing.Size(465, 316);
 this.tabPage2.TabIndex = 1;
   this.tabPage2.TabIndex = 1;
 this.tabPage2.Text = "加密解密文字";
   this.tabPage2.Text = "加密解密文字";
 //
   //
 // button8
   // button8
 //
   //
 this.button8.Location = new System.Drawing.Point(307, 67);
   this.button8.Location = new System.Drawing.Point(307, 67);
 this.button8.Name = "button8";
   this.button8.Name = "button8";
 this.button8.Size = new System.Drawing.Size(62, 21);
   this.button8.Size = new System.Drawing.Size(62, 21);
 this.button8.TabIndex = 9;
   this.button8.TabIndex = 9;
 this.button8.Text = "读取私匙";
   this.button8.Text = "读取私匙";
 this.button8.Click += new System.EventHandler(this.button8_Click);
   this.button8.Click += new System.EventHandler(this.button8_Click);
 //
   //
 // button7
   // button7
 //
   //
 this.button7.Location = new System.Drawing.Point(100, 67);
   this.button7.Location = new System.Drawing.Point(100, 67);
 this.button7.Name = "button7";
   this.button7.Name = "button7";
 this.button7.Size = new System.Drawing.Size(62, 21);
   this.button7.Size = new System.Drawing.Size(62, 21);
 this.button7.TabIndex = 8;
   this.button7.TabIndex = 8;
 this.button7.Text = "读取公匙";
   this.button7.Text = "读取公匙";
 this.button7.Click += new System.EventHandler(this.button7_Click);
   this.button7.Click += new System.EventHandler(this.button7_Click);
 //
   //
 // label3
   // label3
 //
   //
 this.label3.Location = new System.Drawing.Point(253, 119);
   this.label3.Location = new System.Drawing.Point(253, 119);
 this.label3.Name = "label3";
   this.label3.Name = "label3";
 this.label3.Size = new System.Drawing.Size(84, 15);
   this.label3.Size = new System.Drawing.Size(84, 15);
 this.label3.TabIndex = 7;
   this.label3.TabIndex = 7;
 this.label3.Text = "解密结果";
   this.label3.Text = "解密结果";
 //
   //
 // richtext3
   // richtext3
 //
   //
 this.richtext3.Location = new System.Drawing.Point(233, 141);
   this.richtext3.Location = new System.Drawing.Point(233, 141);
 this.richtext3.Name = "richtext3";
   this.richtext3.Name = "richtext3";
 this.richtext3.Size = new System.Drawing.Size(227, 149);
   this.richtext3.Size = new System.Drawing.Size(227, 149);
 this.richtext3.TabIndex = 6;
   this.richtext3.TabIndex = 6;
 this.richtext3.Text = "richTextBox2";
   this.richtext3.Text = "richTextBox2";
 //
   //
 // label2
   // label2
 //
   //
 this.label2.Location = new System.Drawing.Point(40, 119);
   this.label2.Location = new System.Drawing.Point(40, 119);
 this.label2.Name = "label2";
   this.label2.Name = "label2";
 this.label2.Size = new System.Drawing.Size(107, 15);
   this.label2.Size = new System.Drawing.Size(107, 15);
 this.label2.TabIndex = 5;
   this.label2.TabIndex = 5;
 this.label2.Text = "加密窗口:";
   this.label2.Text = "加密窗口:";
 // richtext2
// richtext2
 //
   //
 this.richtext2.Location = new System.Drawing.Point(7, 141);
   this.richtext2.Location = new System.Drawing.Point(7, 141);
 this.richtext2.Name = "richtext2";
   this.richtext2.Name = "richtext2";
 this.richtext2.Size = new System.Drawing.Size(213, 149);
   this.richtext2.Size = new System.Drawing.Size(213, 149);
 this.richtext2.TabIndex = 4;
   this.richtext2.TabIndex = 4;
 this.richtext2.Text = "richTextBox2";
   this.richtext2.Text = "richTextBox2";
 //
   //
 // button5
   // button5
 //
   //
 this.button5.Location = new System.Drawing.Point(293, 89);
   this.button5.Location = new System.Drawing.Point(293, 89);
 this.button5.Name = "button5";
   this.button5.Name = "button5";
 this.button5.Size = new System.Drawing.Size(94, 21);
   this.button5.Size = new System.Drawing.Size(94, 21);
 this.button5.TabIndex = 3;
   this.button5.TabIndex = 3;
 this.button5.Text = "使用私匙解密";
   this.button5.Text = "使用私匙解密";
 this.button5.Click += new System.EventHandler(this.button5_Click);
   this.button5.Click += new System.EventHandler(this.button5_Click);
 //
   //
 // button4
   // button4
 //
   //
 this.button4.Location = new System.Drawing.Point(93, 89);
   this.button4.Location = new System.Drawing.Point(93, 89);
 this.button4.Name = "button4";
   this.button4.Name = "button4";
 this.button4.Size = new System.Drawing.Size(80, 21);
   this.button4.Size = new System.Drawing.Size(80, 21);
 this.button4.TabIndex = 2;
   this.button4.TabIndex = 2;
 this.button4.Text = "使用公匙加密";
   this.button4.Text = "使用公匙加密";
 this.button4.Click += new System.EventHandler(this.button4_Click);
   this.button4.Click += new System.EventHandler(this.button4_Click);
 //
   //
 // textBox1
   // textBox1
 //
   //
 this.textBox1.Location = new System.Drawing.Point(87, 30);
   this.textBox1.Location = new System.Drawing.Point(87, 30);
 this.textBox1.Multiline = true;
   this.textBox1.Multiline = true;
 this.textBox1.Name = "textBox1";
   this.textBox1.Name = "textBox1";
 this.textBox1.Size = new System.Drawing.Size(220, 29);
   this.textBox1.Size = new System.Drawing.Size(220, 29);
 this.textBox1.TabIndex = 1;
   this.textBox1.TabIndex = 1;
 this.textBox1.Text = "textBox1";
   this.textBox1.Text = "textBox1";
 //
   //
 // label1
   // label1
 //
   //
 this.label1.Location = new System.Drawing.Point(107, 7);
   this.label1.Location = new System.Drawing.Point(107, 7);
 this.label1.Name = "label1";
   this.label1.Name = "label1";
 this.label1.Size = new System.Drawing.Size(100, 15);
   this.label1.Size = new System.Drawing.Size(100, 15);
 this.label1.TabIndex = 0;
   this.label1.TabIndex = 0;
 this.label1.Text = "请输入加密的文字";
   this.label1.Text = "请输入加密的文字";
 //
   //
 // tabPage3
   // tabPage3
 //
   //
 this.tabPage3.Controls.Add(this.button15);
   this.tabPage3.Controls.Add(this.button15);
 this.tabPage3.Controls.Add(this.button14);
   this.tabPage3.Controls.Add(this.button14);
 this.tabPage3.Controls.Add(this.button13);
   this.tabPage3.Controls.Add(this.button13);
 this.tabPage3.Controls.Add(this.button12);
   this.tabPage3.Controls.Add(this.button12);
 this.tabPage3.Controls.Add(this.button11);
   this.tabPage3.Controls.Add(this.button11);
 this.tabPage3.Controls.Add(this.richtext4);
   this.tabPage3.Controls.Add(this.richtext4);
 this.tabPage3.Controls.Add(this.label6);
   this.tabPage3.Controls.Add(this.label6);
 this.tabPage3.Controls.Add(this.button10);
   this.tabPage3.Controls.Add(this.button10);
 this.tabPage3.Controls.Add(this.label5);
   this.tabPage3.Controls.Add(this.label5);
 this.tabPage3.Controls.Add(this.textBox3);
   this.tabPage3.Controls.Add(this.textBox3);
 this.tabPage3.Controls.Add(this.button9);
   this.tabPage3.Controls.Add(this.button9);
 this.tabPage3.Controls.Add(this.label4);
   this.tabPage3.Controls.Add(this.label4);
 this.tabPage3.Controls.Add(this.textBox2);
   this.tabPage3.Controls.Add(this.textBox2);
 this.tabPage3.Location = new System.Drawing.Point(4, 22);
   this.tabPage3.Location = new System.Drawing.Point(4, 22);
 this.tabPage3.Name = "tabPage3";
   this.tabPage3.Name = "tabPage3";
 this.tabPage3.Size = new System.Drawing.Size(465, 316);
   this.tabPage3.Size = new System.Drawing.Size(465, 316);
 this.tabPage3.TabIndex = 2;
   this.tabPage3.TabIndex = 2;
 this.tabPage3.Text = "加密解密文件";
   this.tabPage3.Text = "加密解密文件";
 //
   //
 // button15
   // button15
 //
   //
 this.button15.Location = new System.Drawing.Point(173, 119);
   this.button15.Location = new System.Drawing.Point(173, 119);
 this.button15.Name = "button15";
   this.button15.Name = "button15";
 this.button15.Size = new System.Drawing.Size(63, 21);
   this.button15.Size = new System.Drawing.Size(63, 21);
 this.button15.TabIndex = 12;
   this.button15.TabIndex = 12;
 this.button15.Text = "清空文本";
   this.button15.Text = "清空文本";
 this.button15.Click += new System.EventHandler(this.button15_Click);
   this.button15.Click += new System.EventHandler(this.button15_Click);
 //
   //
 // button14
   // button14
 //
   //
 this.button14.Location = new System.Drawing.Point(360, 82);
   this.button14.Location = new System.Drawing.Point(360, 82);
 this.button14.Name = "button14";
   this.button14.Name = "button14";
 this.button14.Size = new System.Drawing.Size(62, 21);
   this.button14.Size = new System.Drawing.Size(62, 21);
 this.button14.TabIndex = 11;
   this.button14.TabIndex = 11;
 this.button14.Text = "读取私匙";
   this.button14.Text = "读取私匙";
 this.button14.Click += new System.EventHandler(this.button14_Click);
   this.button14.Click += new System.EventHandler(this.button14_Click);
 //
   //
 // button13
   // button13
 //
   //
 this.button13.Location = new System.Drawing.Point(360, 30);
   this.button13.Location = new System.Drawing.Point(360, 30);
 this.button13.Name = "button13";
   this.button13.Name = "button13";
 this.button13.Size = new System.Drawing.Size(62, 21);
   this.button13.Size = new System.Drawing.Size(62, 21);
 this.button13.TabIndex = 10;
   this.button13.TabIndex = 10;
 this.button13.Text = "读取公匙";
   this.button13.Text = "读取公匙";
 this.button13.Click += new System.EventHandler(this.button13_Click);
   this.button13.Click += new System.EventHandler(this.button13_Click);
 //
   //
 // button12
   // button12
 //
   //
 this.button12.Location = new System.Drawing.Point(267, 82);
   this.button12.Location = new System.Drawing.Point(267, 82);
 this.button12.Name = "button12";
   this.button12.Name = "button12";
 this.button12.Size = new System.Drawing.Size(62, 21);
   this.button12.Size = new System.Drawing.Size(62, 21);
 this.button12.TabIndex = 9;
   this.button12.TabIndex = 9;
 this.button12.Text = "开始解密";
   this.button12.Text = "开始解密";
 this.button12.Click += new System.EventHandler(this.button12_Click);
   this.button12.Click += new System.EventHandler(this.button12_Click);
 //
   //
 // button11
   // button11
 //
   //
 this.button11.Location = new System.Drawing.Point(267, 30);
   this.button11.Location = new System.Drawing.Point(267, 30);
 this.button11.Name = "button11";
   this.button11.Name = "button11";
 this.button11.Size = new System.Drawing.Size(62, 21);
   this.button11.Size = new System.Drawing.Size(62, 21);
 this.button11.TabIndex = 8;
   this.button11.TabIndex = 8;
 this.button11.Text = "开始加密";
   this.button11.Text = "开始加密";
 this.button11.Click += new System.EventHandler(this.button11_Click);
   this.button11.Click += new System.EventHandler(this.button11_Click);
 //
   //
 // richtext4
   // richtext4
 //
   //
 this.richtext4.Location = new System.Drawing.Point(7, 141);
   this.richtext4.Location = new System.Drawing.Point(7, 141);
 this.richtext4.Name = "richtext4";
   this.richtext4.Name = "richtext4";
 this.richtext4.Size = new System.Drawing.Size(413, 156);
   this.richtext4.Size = new System.Drawing.Size(413, 156);
 this.richtext4.TabIndex = 7;
   this.richtext4.TabIndex = 7;
 this.richtext4.Text = "";
   this.richtext4.Text = "";
 //
   //
 // label6
   // label6
 //
   //
 this.label6.Location = new System.Drawing.Point(13, 111);
   this.label6.Location = new System.Drawing.Point(13, 111);
 this.label6.Name = "label6";
   this.label6.Name = "label6";
 this.label6.Size = new System.Drawing.Size(140, 22);
   this.label6.Size = new System.Drawing.Size(140, 22);
 this.label6.TabIndex = 6;
   this.label6.TabIndex = 6;
 this.label6.Text = "加密与解密窗口:";
   this.label6.Text = "加密与解密窗口:";
 //
   //
 // button10
   // button10
 //
   //
 this.button10.Location = new System.Drawing.Point(187, 82);
   this.button10.Location = new System.Drawing.Point(187, 82);
 this.button10.Name = "button10";
   this.button10.Name = "button10";
 this.button10.Size = new System.Drawing.Size(62, 21);
   this.button10.Size = new System.Drawing.Size(62, 21);
 this.button10.TabIndex = 5;
   this.button10.TabIndex = 5;
 this.button10.Text = "Save
   this.button10.Text = "Save  ";
";
 this.button10.Click += new System.EventHandler(this.button10_Click);
   this.button10.Click += new System.EventHandler(this.button10_Click);
 //
   //
 // label5
   // label5
 //
   //
 this.label5.Location = new System.Drawing.Point(13, 59);
   this.label5.Location = new System.Drawing.Point(13, 59);
 this.label5.Name = "label5";
   this.label5.Name = "label5";
 this.label5.Size = new System.Drawing.Size(180, 15);
   this.label5.Size = new System.Drawing.Size(180, 15);
 this.label5.TabIndex = 4;
   this.label5.TabIndex = 4;
 this.label5.Text = "请保存加密好或者解密后的文件";
   this.label5.Text = "请保存加密好或者解密后的文件";
 //
   //
 // textBox3
   // textBox3
 //
   //
 this.textBox3.Location = new System.Drawing.Point(13, 82);
   this.textBox3.Location = new System.Drawing.Point(13, 82);
 this.textBox3.Name = "textBox3";
   this.textBox3.Name = "textBox3";
 this.textBox3.Size = new System.Drawing.Size(154, 20);
   this.textBox3.Size = new System.Drawing.Size(154, 20);
 this.textBox3.TabIndex = 3;
   this.textBox3.TabIndex = 3;
 this.textBox3.Text = "textBox3";
   this.textBox3.Text = "textBox3";
 //
   //
 // button9
   // button9
 //
   //
 this.button9.Location = new System.Drawing.Point(187, 30);
   this.button9.Location = new System.Drawing.Point(187, 30);
 this.button9.Name = "button9";
   this.button9.Name = "button9";
 this.button9.Size = new System.Drawing.Size(62, 21);
   this.button9.Size = new System.Drawing.Size(62, 21);
 this.button9.TabIndex = 2;
   this.button9.TabIndex = 2;
 this.button9.Text = "Open
   this.button9.Text = "Open  ";
";
 this.button9.Click += new System.EventHandler(this.button9_Click);
   this.button9.Click += new System.EventHandler(this.button9_Click);
 //
   //
 // label4
   // label4
 //
   //
 this.label4.Location = new System.Drawing.Point(13, 7);
   this.label4.Location = new System.Drawing.Point(13, 7);
 this.label4.Name = "label4";
   this.label4.Name = "label4";
 this.label4.Size = new System.Drawing.Size(180, 15);
   this.label4.Size = new System.Drawing.Size(180, 15);
 this.label4.TabIndex = 1;
   this.label4.TabIndex = 1;
 this.label4.Text = "请选择要加密或者解密的文本文件";
   this.label4.Text = "请选择要加密或者解密的文本文件";
 //
   //
 // textBox2
   // textBox2
 //
   //
 this.textBox2.Location = new System.Drawing.Point(13, 30);
   this.textBox2.Location = new System.Drawing.Point(13, 30);
 this.textBox2.Name = "textBox2";
   this.textBox2.Name = "textBox2";
 this.textBox2.Size = new System.Drawing.Size(154, 20);
   this.textBox2.Size = new System.Drawing.Size(154, 20);
 this.textBox2.TabIndex = 0;
   this.textBox2.TabIndex = 0;
 this.textBox2.Text = "textBox2";
   this.textBox2.Text = "textBox2";
 //
   //
 // tabPage4
   // tabPage4
 //
   //
 this.tabPage4.Controls.Add(this.richtext5);
   this.tabPage4.Controls.Add(this.richtext5);
 this.tabPage4.Controls.Add(this.button21);
   this.tabPage4.Controls.Add(this.button21);
 this.tabPage4.Controls.Add(this.button20);
   this.tabPage4.Controls.Add(this.button20);
 this.tabPage4.Controls.Add(this.button19);
   this.tabPage4.Controls.Add(this.button19);
 this.tabPage4.Controls.Add(this.button18);
   this.tabPage4.Controls.Add(this.button18);
 this.tabPage4.Controls.Add(this.button17);
   this.tabPage4.Controls.Add(this.button17);
 this.tabPage4.Controls.Add(this.textBox5);
   this.tabPage4.Controls.Add(this.textBox5);
 this.tabPage4.Controls.Add(this.button16);
   this.tabPage4.Controls.Add(this.button16);
 this.tabPage4.Controls.Add(this.textBox4);
   this.tabPage4.Controls.Add(this.textBox4);
 this.tabPage4.Location = new System.Drawing.Point(4, 22);
   this.tabPage4.Location = new System.Drawing.Point(4, 22);
 this.tabPage4.Name = "tabPage4";
   this.tabPage4.Name = "tabPage4";
 this.tabPage4.Size = new System.Drawing.Size(465, 316);
   this.tabPage4.Size = new System.Drawing.Size(465, 316);
 this.tabPage4.TabIndex = 3;
   this.tabPage4.TabIndex = 3;
 this.tabPage4.Text = "加密解密其它文件";
   this.tabPage4.Text = "加密解密其它文件";
 //
   //
 // richtext5
   // richtext5
 //
   //
 this.richtext5.Location = new System.Drawing.Point(73, 186);
   this.richtext5.Location = new System.Drawing.Point(73, 186);
 this.richtext5.Name = "richtext5";
   this.richtext5.Name = "richtext5";
 this.richtext5.Size = new System.Drawing.Size(254, 104);
   this.richtext5.Size = new System.Drawing.Size(254, 104);
 this.richtext5.TabIndex = 8;
   this.richtext5.TabIndex = 8;
 this.richtext5.Text = "";
   this.richtext5.Text = "";
 //
   //
 // button21
   // button21
 //
   //
 this.button21.Location = new System.Drawing.Point(193, 134);
   this.button21.Location = new System.Drawing.Point(193, 134);
 this.button21.Name = "button21";
   this.button21.Name = "button21";
 this.button21.Size = new System.Drawing.Size(63, 21);
   this.button21.Size = new System.Drawing.Size(63, 21);
 this.button21.TabIndex = 7;
   this.button21.TabIndex = 7;
 this.button21.Text = "解密文件";
   this.button21.Text = "解密文件";
 this.button21.Click += new System.EventHandler(this.button21_Click);
   this.button21.Click += new System.EventHandler(this.button21_Click);
 //
   //
 // button20
   // button20
 //
   //
 this.button20.Location = new System.Drawing.Point(93, 134);
   this.button20.Location = new System.Drawing.Point(93, 134);
 this.button20.Name = "button20";
   this.button20.Name = "button20";
 this.button20.Size = new System.Drawing.Size(63, 21);
   this.button20.Size = new System.Drawing.Size(63, 21);
 this.button20.TabIndex = 6;
   this.button20.TabIndex = 6;
 this.button20.Text = "加密文件";
   this.button20.Text = "加密文件";
 this.button20.Click += new System.EventHandler(this.button20_Click);
   this.button20.Click += new System.EventHandler(this.button20_Click);
 //
   //
 // button19
   // button19
 //
   //
 this.button19.Location = new System.Drawing.Point(133, 82);
   this.button19.Location = new System.Drawing.Point(133, 82);
 this.button19.Name = "button19";
   this.button19.Name = "button19";
 this.button19.Size = new System.Drawing.Size(63, 21);
   this.button19.Size = new System.Drawing.Size(63, 21);
 this.button19.TabIndex = 5;
   this.button19.TabIndex = 5;
 this.button19.Text = "读取私匙";
   this.button19.Text = "读取私匙";
 this.button19.Click += new System.EventHandler(this.button19_Click);
   this.button19.Click += new System.EventHandler(this.button19_Click);
 //
   //
 // button18
   // button18
 //
   //
 this.button18.Location = new System.Drawing.Point(40, 82);
   this.button18.Location = new System.Drawing.Point(40, 82);
 this.button18.Name = "button18";
   this.button18.Name = "button18";
 this.button18.Size = new System.Drawing.Size(62, 21);
   this.button18.Size = new System.Drawing.Size(62, 21);
 this.button18.TabIndex = 4;
   this.button18.TabIndex = 4;
 this.button18.Text = "读取公匙";
   this.button18.Text = "读取公匙";
 this.button18.Click += new System.EventHandler(this.button18_Click);
   this.button18.Click += new System.EventHandler(this.button18_Click);
 //
   //
 // button17
   // button17
 //
   //
 this.button17.Location = new System.Drawing.Point(253, 52);
   this.button17.Location = new System.Drawing.Point(253, 52);
 this.button17.Name = "button17";
   this.button17.Name = "button17";
 this.button17.Size = new System.Drawing.Size(63, 21);
   this.button17.Size = new System.Drawing.Size(63, 21);
 this.button17.TabIndex = 3;
   this.button17.TabIndex = 3;
 this.button17.Text = "保存";
   this.button17.Text = "保存";
 this.button17.Click += new System.EventHandler(this.button17_Click);
   this.button17.Click += new System.EventHandler(this.button17_Click);
 //
   //
 // textBox5
   // textBox5
 //
   //
 this.textBox5.Location = new System.Drawing.Point(20, 52);
   this.textBox5.Location = new System.Drawing.Point(20, 52);
 this.textBox5.Name = "textBox5";
   this.textBox5.Name = "textBox5";
 this.textBox5.Size = new System.Drawing.Size(200, 20);
   this.textBox5.Size = new System.Drawing.Size(200, 20);
 this.textBox5.TabIndex = 2;
   this.textBox5.TabIndex = 2;
 this.textBox5.Text = "textBox5";
   this.textBox5.Text = "textBox5";
 //
   //
 // button16
   // button16
 //
   //
 this.button16.Location = new System.Drawing.Point(253, 22);
   this.button16.Location = new System.Drawing.Point(253, 22);
 this.button16.Name = "button16";
   this.button16.Name = "button16";
 this.button16.Size = new System.Drawing.Size(63, 22);
   this.button16.Size = new System.Drawing.Size(63, 22);
 this.button16.TabIndex = 1;
   this.button16.TabIndex = 1;
 this.button16.Text = "打开";
   this.button16.Text = "打开";
 this.button16.Click += new System.EventHandler(this.button16_Click);
   this.button16.Click += new System.EventHandler(this.button16_Click);
 //
   //
 // textBox4
   // textBox4
 //
   //
 this.textBox4.Location = new System.Drawing.Point(20, 22);
   this.textBox4.Location = new System.Drawing.Point(20, 22);
 this.textBox4.Name = "textBox4";
   this.textBox4.Name = "textBox4";
 this.textBox4.Size = new System.Drawing.Size(200, 20);
   this.textBox4.Size = new System.Drawing.Size(200, 20);
 this.textBox4.TabIndex = 0;
   this.textBox4.TabIndex = 0;
 this.textBox4.Text = "textBox4";
   this.textBox4.Text = "textBox4";
 //
   //
 // Form1
   // Form1
 //
   //
 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
 this.ClientSize = new System.Drawing.Size(486, 420);
   this.ClientSize = new System.Drawing.Size(486, 420);
 this.Controls.Add(this.tabControl1);
   this.Controls.Add(this.tabControl1);
 this.Name = "Form1";
   this.Name = "Form1";
 this.Text = "RSA";
   this.Text = "RSA";
 this.tabControl1.ResumeLayout(false);
   this.tabControl1.ResumeLayout(false);
 this.tabPage1.ResumeLayout(false);
   this.tabPage1.ResumeLayout(false);
 this.tabPage2.ResumeLayout(false);
   this.tabPage2.ResumeLayout(false);
 this.tabPage3.ResumeLayout(false);
   this.tabPage3.ResumeLayout(false);
 this.tabPage4.ResumeLayout(false);
   this.tabPage4.ResumeLayout(false);
 this.ResumeLayout(false);
   this.ResumeLayout(false);

 }
}
 #endregion
#endregion


 /**//// <summary>
/**//// <summary>
 /// 应用程序的主入口点。
  /// 应用程序的主入口点。
 /// </summary>
  /// </summary>
 [STAThread]
  [STAThread]
 static void Main()
  static void Main()

 
   {
{
 Application.Run(new Form1());
   Application.Run(new Form1());
 }
  }
 //得到钥匙信息
  //得到钥匙信息  
 private void button1_Click(object sender, System.EventArgs e)
  private void button1_Click(object sender, System.EventArgs e)

 
   {
{
 crypt=new RSACryptoServiceProvider();
   crypt=new RSACryptoServiceProvider();
 publickey=crypt.ToXmlString(false);
   publickey=crypt.ToXmlString(false);
 richtext.Text="导出秘匙的情况下:\n"+publickey+"\n";
   richtext.Text="导出秘匙的情况下:\n"+publickey+"\n";
 privatekey=crypt.ToXmlString(true);
   privatekey=crypt.ToXmlString(true);
 string info="仅仅导出公匙的情况下:\n"+privatekey+"\n";
   string info="仅仅导出公匙的情况下:\n"+privatekey+"\n";
 richtext.AppendText(info);
   richtext.AppendText(info);
 crypt.Clear();
   crypt.Clear(); 
 }
  }
 //保存公匙信息
  //保存公匙信息
 private void button2_Click(object sender, System.EventArgs e)
  private void button2_Click(object sender, System.EventArgs e)

 
   {
{
 
              
 save=new SaveFileDialog();
   save=new SaveFileDialog();
 save.Filter="File Text (*.txt)|*.txt|All File (*.*)|*.*";
   save.Filter="File Text (*.txt)|*.txt|All File (*.*)|*.*";
 save.ShowDialog();
   save.ShowDialog();
 publicinfo=save.FileName;
   publicinfo=save.FileName;  
 }
  }
 //保存密匙信息
  //保存密匙信息
 private void button3_Click(object sender, System.EventArgs e)
  private void button3_Click(object sender, System.EventArgs e)

 
   {
{
 save=new SaveFileDialog();
   save=new SaveFileDialog();
 save.Filter="File Text (*.txt)|*.txt|All File (*.*)|*.*";
   save.Filter="File Text (*.txt)|*.txt|All File (*.*)|*.*";
 save.ShowDialog();
   save.ShowDialog();
 privateinfo=save.FileName;
   privateinfo=save.FileName;   
 }
  } 
 //用公匙加密
  //用公匙加密
 private void button4_Click(object sender, System.EventArgs e)
  private void button4_Click(object sender, System.EventArgs e)

 
   {
{
 //   if(textBox1.Text =="")
   //   if(textBox1.Text =="")
 //   {
   //   {   
 //    MessageBox.Show("加密文字信息不能为空!");
   //    MessageBox.Show("加密文字信息不能为空!");
 //    return;
   //    return;   
 //   }
   //   }
 //   if(readpubkey==false)
   //   if(readpubkey==false)
 //   {
   //   {
 //    MessageBox.Show("请打开公钥所在的文件!");
   //    MessageBox.Show("请打开公钥所在的文件!");
 //    return;
   //    return;
 //   }
   //   }
 try
   try

 
    {
{
 readpublickey = "";
    readpublickey = "";

 crypt=new RSACryptoServiceProvider();
crypt=new RSACryptoServiceProvider();
 UTF8Encoding enc=new UTF8Encoding();
    UTF8Encoding enc=new UTF8Encoding();
 bytes=enc.GetBytes(textBox1.Text);
    bytes=enc.GetBytes(textBox1.Text);
 crypt.FromXmlString(  readpublickey );
    crypt.FromXmlString(  readpublickey );
 bytes = crypt.Encrypt( bytes,false );
    bytes = crypt.Encrypt( bytes,false );
 string encryttext=enc.GetString(bytes);
    string encryttext=enc.GetString(bytes);
 richtext2.Text="加密结果:\n"+encryttext+"\n"+"加密结束!";
    richtext2.Text="加密结果:\n"+encryttext+"\n"+"加密结束!";
 }
   }
 catch
   catch

 
    {
{
 MessageBox.Show("请检查是否打开公匙或者公匙是否损坏!");
    MessageBox.Show("请检查是否打开公匙或者公匙是否损坏!");
 
   
 }
   }
 }
  }
 //使用私匙解密
  //使用私匙解密
 private void button5_Click(object sender, System.EventArgs e)
  private void button5_Click(object sender, System.EventArgs e)

 
   {
{
 //   if(readprikey==false)
   //   if(readprikey==false)
 //   {
   //   {
 //    MessageBox.Show("请打开密匙所在的文件!");
   //    MessageBox.Show("请打开密匙所在的文件!");
 //    return;
   //    return;
 //   }
   //   }
 readprivatekey = "<RSAKeyValue><Modulus>xTzjEF8KYJVsSXztud+EPO8iNlvgDjlhS5Kbm3zmfFJdRTUBz1y2o3ydBBF8nUvYWeujXxTAdDk3rHbXueF0J29WIi1BZusq3FdpJ803BeSROl2f+V7NLrfwV1FoDvqdPXpTfmVzdxykTF26AIu3HTUYI0PaAxY1Hm0KCIar6Ks=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
   readprivatekey = "<RSAKeyValue><Modulus>xTzjEF8KYJVsSXztud+EPO8iNlvgDjlhS5Kbm3zmfFJdRTUBz1y2o3ydBBF8nUvYWeujXxTAdDk3rHbXueF0J29WIi1BZusq3FdpJ803BeSROl2f+V7NLrfwV1FoDvqdPXpTfmVzdxykTF26AIu3HTUYI0PaAxY1Hm0KCIar6Ks=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

 try
try

 
    {
{
 UTF8Encoding enc=new UTF8Encoding();
    UTF8Encoding enc=new UTF8Encoding();
 byte [] decryptbyte;
    byte [] decryptbyte;
 crypt.FromXmlString ( readprivatekey ) ;
    crypt.FromXmlString ( readprivatekey ) ;
 decryptbyte = crypt.Decrypt( bytes,false );
    decryptbyte = crypt.Decrypt( bytes,false );
 string decrypttext=enc.GetString( decryptbyte );
    string decrypttext=enc.GetString( decryptbyte );
 richtext3.Text = "解密结果:\n" + decrypttext + "\n" + "解密结束!" ;
    richtext3.Text = "解密结果:\n" + decrypttext + "\n" + "解密结束!" ;
 }
   }
 catch
   catch

 
    {
{
 MessageBox.Show("请检查是否打开私匙或者私匙是否损坏!");
    MessageBox.Show("请检查是否打开私匙或者私匙是否损坏!");
 
   
 }
   }
 }
  }
 //把钥匙信息写入文件
  //把钥匙信息写入文件
 private void button6_Click(object sender, System.EventArgs e)
  private void button6_Click(object sender, System.EventArgs e)

 
   {
{   
 StreamWriter one=new StreamWriter(publicinfo,true,UTF8Encoding.UTF8);
   StreamWriter one=new StreamWriter(publicinfo,true,UTF8Encoding.UTF8);
 one.Write(publickey);
   one.Write(publickey);
 StreamWriter two=new StreamWriter(privateinfo,true,UTF8Encoding.UTF8);
   StreamWriter two=new StreamWriter(privateinfo,true,UTF8Encoding.UTF8);
 two.Write(privatekey);
   two.Write(privatekey);
 one.Flush();
   one.Flush();
 two.Flush();
   two.Flush();
 one.Close();
   one.Close();
 two.Close();
   two.Close();
 MessageBox.Show("成功保存公匙和密匙!");
   MessageBox.Show("成功保存公匙和密匙!"); 
 }
  }
 //从文件中读取公匙信息
  //从文件中读取公匙信息
 private void button7_Click(object sender, System.EventArgs e)
  private void button7_Click(object sender, System.EventArgs e)

 
   {
{   
 readpublickey = ReadPublicKey();
   readpublickey = ReadPublicKey();
 readpubkey=true;
   readpubkey=true;
 }
  }
 
 
 //从文件中读取私匙信息
  //从文件中读取私匙信息
 private void button8_Click(object sender, System.EventArgs e)
  private void button8_Click(object sender, System.EventArgs e)

 
   {
{
 
   
 readprivatekey  = ReadPrivateKey();
   readprivatekey  = ReadPrivateKey();
 readprikey = true;
   readprikey = true;
 }
  }
 
 
 //打开加密或者解密的文件
  //打开加密或者解密的文件
 private void button9_Click(object sender, System.EventArgs e)
  private void button9_Click(object sender, System.EventArgs e)

 
   {
{
 open = new OpenFileDialog( );
   open = new OpenFileDialog( );
 open.Filter="Text File (*.txt)|*.txt|All Files (*.*)|*.*";
   open.Filter="Text File (*.txt)|*.txt|All Files (*.*)|*.*";
 open.ShowDialog();
   open.ShowDialog();
 textBox2.Text=open.FileName;
   textBox2.Text=open.FileName;
 openla = true;
   openla = true;
 }
  }
 //保存加密或者解密的文件
  //保存加密或者解密的文件
 private void button10_Click(object sender, System.EventArgs e)
  private void button10_Click(object sender, System.EventArgs e)

 
   {
{
 try
   try

 
    {
{
 save=new SaveFileDialog();
    save=new SaveFileDialog();
 save.Filter="File Text (*.txt)|*.txt|All File (*.*)|*.*";
    save.Filter="File Text (*.txt)|*.txt|All File (*.*)|*.*";
 save.ShowDialog();
    save.ShowDialog();
 textBox3.Text=save.FileName;
    textBox3.Text=save.FileName;
 savela = true;
    savela = true;
 }
   }
 catch
   catch

 
    {
{
 MessageBox.Show("请输入文件名字!");
    MessageBox.Show("请输入文件名字!");
 return;
    return;
 }
   }

 }
}
 
  
 //加密文本文件
  //加密文本文件
 private void button11_Click(object sender, System.EventArgs e)
  private void button11_Click(object sender, System.EventArgs e)

 
   {
{
 if(readpubkey != true)
   if(readpubkey != true)

 
    {
{
 MessageBox.Show("请打开公钥所在的文件!");
    MessageBox.Show("请打开公钥所在的文件!");
 return;
    return;
 }
   }
 if(openla==true&&savela==true)
   if(openla==true&&savela==true)

 
    {
{
 crypt=new RSACryptoServiceProvider();
    crypt=new RSACryptoServiceProvider();
 crypt.FromXmlString( readpublickey );
    crypt.FromXmlString( readpublickey );
 UTF8Encoding enc=new UTF8Encoding();
    UTF8Encoding enc=new UTF8Encoding();
 //读取原文件到一个string 去啊
    //读取原文件到一个string 去啊
 StreamReader sr=new StreamReader(textBox2.Text,Encoding.Default);
    StreamReader sr=new StreamReader(textBox2.Text,Encoding.Default);
 string textinfo=sr.ReadToEnd();
    string textinfo=sr.ReadToEnd();
 sr.Close();
    sr.Close();
 //  richtext4.AppendText( "\n原文件内容:\n" + textinfo + "\n" );
    //  richtext4.AppendText( "\n原文件内容:\n" + textinfo + "\n" );
 //开始加密
    //开始加密
 string readinfo=EncryptFile(textinfo,textBox3.Text);
    string readinfo=EncryptFile(textinfo,textBox3.Text);
 richtext4.AppendText( "加密文件已经保存到:" + textBox3.Text + "\n"  );
    richtext4.AppendText( "加密文件已经保存到:" + textBox3.Text + "\n"  );
 richtext4.AppendText( "加密结果:\n" + readinfo + "\n加密结束!" );
    richtext4.AppendText( "加密结果:\n" + readinfo + "\n加密结束!" ); 
 }
   }
 else
   else   
 MessageBox.Show("请选择你要加密的文件或者要保存的文件!");
    MessageBox.Show("请选择你要加密的文件或者要保存的文件!");   
 }
  }
 //解密文件
  //解密文件
 private void button12_Click(object sender, System.EventArgs e)
  private void button12_Click(object sender, System.EventArgs e)

 
   {
{
 if(readprikey==false)
   if(readprikey==false)

 
    {
{
 MessageBox.Show("请打开密匙所在的文件!");
    MessageBox.Show("请打开密匙所在的文件!");
 return;
    return;
 }
   }
 if(openla==true&&savela==true)
   if(openla==true&&savela==true)

 
    {
{
 try
    try

 
     {
{
 crypt=new RSACryptoServiceProvider();
     crypt=new RSACryptoServiceProvider();
 crypt.FromXmlString( readprivatekey );
     crypt.FromXmlString( readprivatekey );
 string decryptinfo = DecryptFile(textBox2.Text,textBox3.Text);
     string decryptinfo = DecryptFile(textBox2.Text,textBox3.Text);
 richtext4.AppendText( "解密文件已经保存到:" + textBox3.Text+ "\n" );
     richtext4.AppendText( "解密文件已经保存到:" + textBox3.Text+ "\n" );
 richtext4.AppendText("解密结果:\n" + decryptinfo + "\n" + "解密结束!\n");
     richtext4.AppendText("解密结果:\n" + decryptinfo + "\n" + "解密结束!\n");
 }
    }
 catch
    catch

 
     {
{
 MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
     MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
 return;
     return;
 }
    }
 }
   }
 else
   else
 MessageBox.Show("请选择你要解密的文件或者要保存的文件!");
    MessageBox.Show("请选择你要解密的文件或者要保存的文件!");
 }
  }
 
 
 //加密文件时候,同样要打开公钥匙
  //加密文件时候,同样要打开公钥匙
 private void button13_Click(object sender, System.EventArgs e)
  private void button13_Click(object sender, System.EventArgs e)

 
   {
{   
 readpublickey = ReadPublicKey();
   readpublickey = ReadPublicKey();
 readpubkey=true;
   readpubkey=true;
 
 
 }
  }
 //解密文件时候,同样要打开私钥匙
  //解密文件时候,同样要打开私钥匙
 private void button14_Click(object sender, System.EventArgs e)
  private void button14_Click(object sender, System.EventArgs e)

 
   {
{   
 readprivatekey  =ReadPrivateKey();
   readprivatekey  =ReadPrivateKey();
 readprikey=true;
   readprikey=true; 
 }
  }
 private void button15_Click(object sender, System.EventArgs e)
  private void button15_Click(object sender, System.EventArgs e)

 
   {
{
 richtext4.Clear();
   richtext4.Clear();  
 }
  }
 
 
 //打开文件
  //打开文件 
 private void button16_Click(object sender, System.EventArgs e)
  private void button16_Click(object sender, System.EventArgs e)

 
   {
{
 open = new OpenFileDialog( );
   open = new OpenFileDialog( );
 open.Filter="All Files (*.*)|*.*";
   open.Filter="All Files (*.*)|*.*";
 open.ShowDialog();
   open.ShowDialog();
 textBox4.Text=open.FileName;
   textBox4.Text=open.FileName;
 openla = true;
   openla = true; 
 }
  }
 //保存文件
  //保存文件
 private void button17_Click(object sender, System.EventArgs e)
  private void button17_Click(object sender, System.EventArgs e)

 
   {
{
 save=new SaveFileDialog();
   save=new SaveFileDialog();
 save.Filter="All File (*.*)|*.*";
   save.Filter="All File (*.*)|*.*";
 save.ShowDialog();
   save.ShowDialog();
 textBox5.Text=save.FileName;
   textBox5.Text=save.FileName;
 savela = true;
   savela = true;
 }
  }


 //读取公钥匙
//读取公钥匙
 private void button18_Click(object sender, System.EventArgs e)
  private void button18_Click(object sender, System.EventArgs e)

 
   {
{
 readpublickey = ReadPublicKey();
   readpublickey = ReadPublicKey();
 readpubkey=true;
   readpubkey=true; 
 }
  }  
 //读取私钥匙
  //读取私钥匙  
 private void button19_Click(object sender, System.EventArgs e)
  private void button19_Click(object sender, System.EventArgs e)

 
   {
{
 readprivatekey = ReadPrivateKey();
   readprivatekey = ReadPrivateKey();
 readprikey=true;
   readprikey=true; 
 }
  }
 // 加密其它非文本文件啦
  // 加密其它非文本文件啦
 private void button20_Click(object sender, System.EventArgs e)
  private void button20_Click(object sender, System.EventArgs e)

 
   {
{
 if(readpubkey==false)
   if(readpubkey==false)

 
    {
{
 MessageBox.Show("请打开公匙所在的文件!");
    MessageBox.Show("请打开公匙所在的文件!");
 return;
    return;
 }
   }
 if(openla==true&&savela==true)
   if(openla==true&&savela==true)

 
    {
{
 try
    try

 
     {
{
 crypt=new RSACryptoServiceProvider();
     crypt=new RSACryptoServiceProvider();
 crypt.FromXmlString( readpublickey );
     crypt.FromXmlString( readpublickey );
 EncryptOther( textBox4.Text,textBox5.Text );
     EncryptOther( textBox4.Text,textBox5.Text );
 richtext5.AppendText("加密过程结束!:\n" + "加密文件已经保存到:\n" + textBox5.Text + "\n");
     richtext5.AppendText("加密过程结束!:\n" + "加密文件已经保存到:\n" + textBox5.Text + "\n");
 }
    }
 catch
    catch

 
     {
{
 MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
     MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
 return;
     return;
 }
    }
 }
   }
 else
   else
 MessageBox.Show("请选择你要解密的文件或者要保存的文件!");
    MessageBox.Show("请选择你要解密的文件或者要保存的文件!");
 
 
 }
  }
 //解密其它文件啦
  //解密其它文件啦
 private void button21_Click(object sender, System.EventArgs e)
  private void button21_Click(object sender, System.EventArgs e)

 
   {
{
 if(readprikey==false)
   if(readprikey==false)

 
    {
{
 MessageBox.Show("请打开密匙所在的文件!");
    MessageBox.Show("请打开密匙所在的文件!");
 return;
    return;
 }
   }
 if(openla==true&&savela==true)
   if(openla==true&&savela==true)

 
    {
{
 try
    try

 
     {
{
 crypt=new RSACryptoServiceProvider();
     crypt=new RSACryptoServiceProvider();
 crypt.FromXmlString( readprivatekey );
     crypt.FromXmlString( readprivatekey );
 //使用线程后,反而达不到效果             //原因?暂时不清晰
     //使用线程后,反而达不到效果             //原因?暂时不清晰
 //可以转换,但是转换要经过几分钟,才可以真正得到想要的转换的东西。
     //可以转换,但是转换要经过几分钟,才可以真正得到想要的转换的东西。
 th = new Thread( new ThreadStart(  DecryptUsingThread ) );
     th = new Thread( new ThreadStart(  DecryptUsingThread ) );
 th.Start();
     th.Start();
 // DecryptOther(textBox4.Text,textBox5.Text);
     // DecryptOther(textBox4.Text,textBox5.Text);    
 richtext5.AppendText( "解密结束!\n" + "解密文件已经保存到:" + textBox5.Text+ "\n" );
     richtext5.AppendText( "解密结束!\n" + "解密文件已经保存到:" + textBox5.Text+ "\n" );    
 }
    }
 catch
    catch

 
     {
{
 MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
     MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
 return;
     return;
 }
    }
 }
   }
 else
   else
 MessageBox.Show("请选择你要解密的文件或者要保存的文件!");
    MessageBox.Show("请选择你要解密的文件或者要保存的文件!"); 
 }
  }

 //下面为自己定义的函数啦
//下面为自己定义的函数啦
 //单独一个读取公匙的函数
  //单独一个读取公匙的函数
 private string ReadPublicKey()
  private string ReadPublicKey()

 
   {
{
 
   
 string publickey;
   string publickey;
 open = new OpenFileDialog( );
   open = new OpenFileDialog( );
 open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";
   open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";
 open.ShowDialog();
   open.ShowDialog();  
 StreamReader sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);
   StreamReader sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);  
 publickey = sr.ReadToEnd();
   publickey = sr.ReadToEnd();
 sr.Close();
   sr.Close();
 return publickey;
   return publickey;  
 }
  }
 //单独一个读取私匙的函数
  //单独一个读取私匙的函数
 private string ReadPrivateKey()
  private string ReadPrivateKey()

 
   {
{
 string privatekey;
   string privatekey;   
 open = new OpenFileDialog( );
   open = new OpenFileDialog( );
 open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";
   open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";
 open.ShowDialog();
   open.ShowDialog();
 StreamReader sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);
   StreamReader sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);
 privatekey  = sr.ReadToEnd();
   privatekey  = sr.ReadToEnd();
 sr.Close();
   sr.Close();  
 return privatekey;
   return privatekey;
 }
  }
 //加密文本文件
  //加密文本文件 
 private string EncryptFile( string textinfo, string filename )
  private string EncryptFile( string textinfo, string filename )

 
   {
{    
 UTF8Encoding enc=new UTF8Encoding();
   UTF8Encoding enc=new UTF8Encoding();  
 byte [] bytes=enc.GetBytes( textinfo );
   byte [] bytes=enc.GetBytes( textinfo );
 int blockSize=0;
   int blockSize=0;
 if(crypt.KeySize==1024)
   if(crypt.KeySize==1024)
 blockSize=16;
    blockSize=16;
 else
   else
 blockSize=8;
    blockSize=8;
 //同时写入到内存中去
   //同时写入到内存中去
 MemoryStream ms=new MemoryStream();
   MemoryStream ms=new MemoryStream();
 //写入文件中去啊。
   //写入文件中去啊。
 FileStream fs=new FileStream(@filename,FileMode.OpenOrCreate);
   FileStream fs=new FileStream(@filename,FileMode.OpenOrCreate);
 byte[]rawblock,encryblock;
   byte[]rawblock,encryblock;
 for(int i=0;i<bytes.Length;i+=blockSize)
   for(int i=0;i<bytes.Length;i+=blockSize)

 
    {
{
 if((bytes.Length-i)>blockSize)
    if((bytes.Length-i)>blockSize)
 rawblock=new byte[blockSize];
     rawblock=new byte[blockSize];
 else
    else
 rawblock =new byte[bytes.Length-i];
     rawblock =new byte[bytes.Length-i];
 Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
    Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
 encryblock=crypt.Encrypt(rawblock,false);
    encryblock=crypt.Encrypt(rawblock,false);   
 fs.Write(encryblock,0,encryblock.Length);
    fs.Write(encryblock,0,encryblock.Length);
 //写入到内存中去啊。
    //写入到内存中去啊。
 ms.Write(encryblock,0,encryblock.Length);
    ms.Write(encryblock,0,encryblock.Length);
 }
   }
 //写入到内存中
   //写入到内存中
 ms.Position = 0;
   ms.Position = 0;
 byte [] decode = new byte[ms.Length];
   byte [] decode = new byte[ms.Length];
 ms.Read(decode,0,(int)ms.Length);
   ms.Read(decode,0,(int)ms.Length);
 string decodeinfo = enc.GetString(decode,0,decode.Length);
   string decodeinfo = enc.GetString(decode,0,decode.Length);  
 fs.Flush();
   fs.Flush();
 fs.Close();
   fs.Close();
 ms.Close();
   ms.Close();  
 return decodeinfo;
   return decodeinfo;
 }
  }

 //解密文本文件
//解密文本文件
 private string DecryptFile(string encryptfile,string decryptfile)
  private string DecryptFile(string encryptfile,string decryptfile)

 
   {
{
 //打开加密后的文件,读取文件内容
   //打开加密后的文件,读取文件内容
 FileStream fs=new FileStream(@encryptfile,FileMode.Open);
   FileStream fs=new FileStream(@encryptfile,FileMode.Open);
 byte [] bytes=new byte[fs.Length];
   byte [] bytes=new byte[fs.Length];
 fs.Read(bytes,0,(int)fs.Length);
   fs.Read(bytes,0,(int)fs.Length);
 fs.Close();
   fs.Close();
 //先写入到内存
   //先写入到内存
 MemoryStream ms=new MemoryStream();
   MemoryStream ms=new MemoryStream();
 //最好写入到解密后的一个文件
   //最好写入到解密后的一个文件
 StreamWriter sw=new StreamWriter(decryptfile);
   StreamWriter sw=new StreamWriter(decryptfile);
 int keySize=crypt.KeySize/8;
   int keySize=crypt.KeySize/8;
 byte[]rawblock,decryptblock;
   byte[]rawblock,decryptblock;
 for(int i=0;i<bytes.Length;i+=keySize)
   for(int i=0;i<bytes.Length;i+=keySize)

 
    {
{
 if( ( bytes.Length-i ) > keySize )
    if( ( bytes.Length-i ) > keySize )

 
     {
{
 rawblock=new byte[keySize];
     rawblock=new byte[keySize];
 }
    }
 else
    else

 
     {rawblock =new byte[bytes.Length-i];}
{rawblock =new byte[bytes.Length-i];}

 Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
 decryptblock = crypt.Decrypt( rawblock,false );
    decryptblock = crypt.Decrypt( rawblock,false );
 ms.Write(decryptblock,0,decryptblock.Length);
    ms.Write(decryptblock,0,decryptblock.Length);
 }
   }
 ms.Position=0;
   ms.Position=0;
 byte [] decode=new byte[ms.Length];
   byte [] decode=new byte[ms.Length];
 ms.Read(decode,0,(int)ms.Length);
   ms.Read(decode,0,(int)ms.Length);
 UTF8Encoding enc=new UTF8Encoding();
   UTF8Encoding enc=new UTF8Encoding();
 string text=enc.GetString(decode);
   string text=enc.GetString(decode);
 //写入到内存中去啊
   //写入到内存中去啊
 sw.Write(text);
   sw.Write(text);
 sw.Flush();
   sw.Flush(); 
 ms.Close();
   ms.Close();
 sw.Close();
   sw.Close();
 return text;
   return text;
 }
  }

 //加密其它非文本文件,当然可以包括文本文件
//加密其它非文本文件,当然可以包括文本文件
 private void EncryptOther(string file1,string file2)
  private void EncryptOther(string file1,string file2)

 
   {
{
 FileStream picfs=new FileStream(@file1,FileMode.Open);
   FileStream picfs=new FileStream(@file1,FileMode.Open);
 FileStream fs=new FileStream(@file2,FileMode.OpenOrCreate);
   FileStream fs=new FileStream(@file2,FileMode.OpenOrCreate);
 byte [] bytes=new byte[picfs.Length];
   byte [] bytes=new byte[picfs.Length];
 picfs.Read(bytes,0,(int)picfs.Length);
   picfs.Read(bytes,0,(int)picfs.Length);
 picfs.Close();
   picfs.Close();
 int blockSize=0;
   int blockSize=0;
 if(crypt.KeySize==1024)
   if(crypt.KeySize==1024)
 blockSize=16;
    blockSize=16;
 else blockSize=8;
   else blockSize=8;  
 byte[]rawblock,encryblock;
   byte[]rawblock,encryblock;
 for(int i=0;i<bytes.Length;i+=blockSize)
   for(int i=0;i<bytes.Length;i+=blockSize)

 
    {
{
 if((bytes.Length-i)>blockSize)
    if((bytes.Length-i)>blockSize)
 rawblock=new byte[blockSize];
     rawblock=new byte[blockSize];
 else rawblock =new byte[bytes.Length-i];
    else rawblock =new byte[bytes.Length-i];
 Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
    Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
 encryblock=crypt.Encrypt(rawblock,false);
    encryblock=crypt.Encrypt(rawblock,false);
 fs.Write(encryblock,0,encryblock.Length);
    fs.Write(encryblock,0,encryblock.Length);
 }
   }
 fs.Flush();
   fs.Flush();
 fs.Close();
   fs.Close();
 }
  }
 //解密其它非文本文件,当然可以包括文本文件
  //解密其它非文本文件,当然可以包括文本文件
 private void DecryptOther(string fileone,string filetwo)
  private void DecryptOther(string fileone,string filetwo)

 
   {
{
 FileStream fs=new FileStream(@fileone,FileMode.Open);
   FileStream fs=new FileStream(@fileone,FileMode.Open);
 FileStream writopic=new FileStream(@filetwo,FileMode.Create);
   FileStream writopic=new FileStream(@filetwo,FileMode.Create);
 byte [] bytes=new byte[fs.Length];
   byte [] bytes=new byte[fs.Length];
 fs.Read(bytes,0,(int)fs.Length);
   fs.Read(bytes,0,(int)fs.Length);
 fs.Close();
   fs.Close();
 MemoryStream ms=new MemoryStream();
   MemoryStream ms=new MemoryStream();
 int keySize=crypt.KeySize/8;
   int keySize=crypt.KeySize/8;
 byte[]rawblock,decryptblock;
   byte[]rawblock,decryptblock;
 for(int i=0;i<bytes.Length;i+=keySize)
   for(int i=0;i<bytes.Length;i+=keySize)

 
    {
{
 if( ( bytes.Length-i ) > keySize )
    if( ( bytes.Length-i ) > keySize )

 
     {
{
 rawblock=new byte[keySize];
     rawblock=new byte[keySize];
 }
    }
 else
    else

 
     {rawblock =new byte[bytes.Length-i];}
{rawblock =new byte[bytes.Length-i];}

 Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
 decryptblock = crypt.Decrypt( rawblock,false );
    decryptblock = crypt.Decrypt( rawblock,false );
 ms.Write(decryptblock,0,decryptblock.Length);
    ms.Write(decryptblock,0,decryptblock.Length);
 }
   }
 ms.Position=0;
   ms.Position=0;
 byte [] decode=new byte[ms.Length];
   byte [] decode=new byte[ms.Length];
 ms.Read(decode,0,(int)ms.Length);
   ms.Read(decode,0,(int)ms.Length);
 ms.Close();
   ms.Close();          
 writopic.Write(decode,0,decode.Length);
   writopic.Write(decode,0,decode.Length);
 writopic.Close();
   writopic.Close();

 }
}
 //创建一个函数,可以在线程中使用,经实验,效果不明显
  //创建一个函数,可以在线程中使用,经实验,效果不明显
 private void DecryptUsingThread()
  private void DecryptUsingThread()

 
   {
{
 DecryptOther(textBox4.Text,textBox5.Text);
   DecryptOther(textBox4.Text,textBox5.Text);
 }
  }
 
     
 }
 }
 }
}

 这个例子会有一点小错误,但是不影响学习.可以自己修改一下.
这个例子会有一点小错误,但是不影响学习.可以自己修改一下.

  
 
                    
                 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号