C#—非对称加密:加密文件 RSA

80酷酷网    80kuku.com

  加密

C#—非对称加密:加密文件
★★★★★★ ☆☆☆ ★★★★★★
●●○○     聂永     ○○●●
§§§§nie_yong163.com§§§§
◆◆□□nie.yong126.com□□◆◆
№№№№№№ ※※※ №№№№№№   
说明:
1.你要注意的是读取文件的两种不同方式;
2.从已经保存的钥匙文件中读取其内容;
3.这个程序做的很粗糙(要准备考外语六级,要准备期末考试,实在很忙啊!),希望诸位能够完善,然后也发表出来,在下表示感谢;
4.参考书目: 《C#数据安全手册》;
5.有做这方面程序的朋友,愿意相交。
代码部分:

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;
using System.Threading;

namespace 非对称加密
{
 /// <summary>
 /// 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.TabPage tabPage3;
  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;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.Button button9;
  private System.Windows.Forms.TextBox textBox3;
  private System.Windows.Forms.Label label5;
  private System.Windows.Forms.Button button10;
  private System.Windows.Forms.Label label6;
  private System.Windows.Forms.Button button11;
  private System.Windows.Forms.Button button12;
  private System.Windows.Forms.RichTextBox richtext4;
  private System.Windows.Forms.Button button13;
  private System.Windows.Forms.Button button14;
  private System.Windows.Forms.Button button15;
  private System.Windows.Forms.TabPage tabPage4;
  private System.Windows.Forms.TextBox textBox4;
  private System.Windows.Forms.Button button16;
  private System.Windows.Forms.TextBox textBox5;
  private System.Windows.Forms.Button button17;
  private System.Windows.Forms.Button button18;
  private System.Windows.Forms.Button button19;
  private System.Windows.Forms.Button button20;
  private System.Windows.Forms.Button button21;
  private System.Windows.Forms.RichTextBox richtext5;
  /// <summary>
  /// 必需的设计器变量。
  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 bool openla = false;
  private bool savela = false;
  private bool readpubkey=false;
  private bool readprikey=false;
  private Thread th; 
  /// </summary>
  private System.ComponentModel.Container components = null;

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

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }
  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </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.tabPage3 = new System.Windows.Forms.TabPage();
   this.button15 = new System.Windows.Forms.Button();
   this.button14 = new System.Windows.Forms.Button();
   this.button13 = new System.Windows.Forms.Button();
   this.button12 = new System.Windows.Forms.Button();
   this.button11 = new System.Windows.Forms.Button();
   this.richtext4 = new System.Windows.Forms.RichTextBox();
   this.label6 = new System.Windows.Forms.Label();
   this.button10 = new System.Windows.Forms.Button();
   this.label5 = new System.Windows.Forms.Label();
   this.textBox3 = new System.Windows.Forms.TextBox();
   this.button9 = new System.Windows.Forms.Button();
   this.label4 = new System.Windows.Forms.Label();
   this.textBox2 = new System.Windows.Forms.TextBox();
   this.tabPage4 = new System.Windows.Forms.TabPage();
   this.save = new System.Windows.Forms.SaveFileDialog();
   this.open = new System.Windows.Forms.OpenFileDialog();
   this.textBox4 = new System.Windows.Forms.TextBox();
   this.button16 = new System.Windows.Forms.Button();
   this.textBox5 = new System.Windows.Forms.TextBox();
   this.button17 = new System.Windows.Forms.Button();
   this.button18 = new System.Windows.Forms.Button();
   this.button19 = new System.Windows.Forms.Button();
   this.button20 = new System.Windows.Forms.Button();
   this.button21 = new System.Windows.Forms.Button();
   this.richtext5 = new System.Windows.Forms.RichTextBox();
   this.tabControl1.SuspendLayout();
   this.tabPage1.SuspendLayout();
   this.tabPage2.SuspendLayout();
   this.tabPage3.SuspendLayout();
   this.tabPage4.SuspendLayout();
   this.SuspendLayout();
   //
   // tabControl1
   //
   this.tabControl1.Controls.Add(this.tabPage1);
   this.tabControl1.Controls.Add(this.tabPage2);
   this.tabControl1.Controls.Add(this.tabPage3);
   this.tabControl1.Controls.Add(this.tabPage4);
   this.tabControl1.Location = new System.Drawing.Point(8, 64);
   this.tabControl1.Name = "tabControl1";
   this.tabControl1.SelectedIndex = 0;
   this.tabControl1.Size = new System.Drawing.Size(568, 368);
   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(560, 343);
   this.tabPage1.TabIndex = 0;
   this.tabPage1.Text = "得到钥匙";
   //
   // button6
   //
   this.button6.Location = new System.Drawing.Point(216, 288);
   this.button6.Name = "button6";
   this.button6.TabIndex = 4;
   this.button6.Text = "写入文件";
   this.button6.Click += new System.EventHandler(this.button6_Click);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(456, 16);
   this.button3.Name = "button3";
   this.button3.TabIndex = 3;
   this.button3.Text = "保存私匙";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(320, 16);
   this.button2.Name = "button2";
   this.button2.TabIndex = 2;
   this.button2.Text = "保存公钥";
   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(520, 184);
   this.richtext.TabIndex = 1;
   this.richtext.Text = "richTextBox1";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(136, 16);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(88, 23);
   this.button1.TabIndex = 0;
   this.button1.Text = "得到钥匙信息";
   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(560, 343);
   this.tabPage2.TabIndex = 1;
   this.tabPage2.Text = "加密解密文字";
   //
   // button8
   //
   this.button8.Location = new System.Drawing.Point(368, 72);
   this.button8.Name = "button8";
   this.button8.TabIndex = 9;
   this.button8.Text = "读取私匙";
   this.button8.Click += new System.EventHandler(this.button8_Click);
   //
   // button7
   //
   this.button7.Location = new System.Drawing.Point(120, 72);
   this.button7.Name = "button7";
   this.button7.TabIndex = 8;
   this.button7.Text = "读取公匙";
   this.button7.Click += new System.EventHandler(this.button7_Click);
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(304, 128);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(100, 16);
   this.label3.TabIndex = 7;
   this.label3.Text = "解密结果";
   //
   // richtext3
   //
   this.richtext3.Location = new System.Drawing.Point(280, 152);
   this.richtext3.Name = "richtext3";
   this.richtext3.Size = new System.Drawing.Size(272, 160);
   this.richtext3.TabIndex = 6;
   this.richtext3.Text = "richTextBox2";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(48, 128);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(128, 16);
   this.label2.TabIndex = 5;
   this.label2.Text = "加密窗口:";
   //
   // richtext2
   //
   this.richtext2.Location = new System.Drawing.Point(8, 152);
   this.richtext2.Name = "richtext2";
   this.richtext2.Size = new System.Drawing.Size(256, 160);
   this.richtext2.TabIndex = 4;
   this.richtext2.Text = "richTextBox2";
   //
   // button5
   //
   this.button5.Location = new System.Drawing.Point(352, 96);
   this.button5.Name = "button5";
   this.button5.Size = new System.Drawing.Size(112, 23);
   this.button5.TabIndex = 3;
   this.button5.Text = "使用私匙解密";
   this.button5.Click += new System.EventHandler(this.button5_Click);
   //
   // button4
   //
   this.button4.Location = new System.Drawing.Point(112, 96);
   this.button4.Name = "button4";
   this.button4.Size = new System.Drawing.Size(96, 23);
   this.button4.TabIndex = 2;
   this.button4.Text = "使用公匙加密";
   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(264, 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 = "请输入加密的文字";
   //
   // tabPage3
   //
   this.tabPage3.Controls.Add(this.button15);
   this.tabPage3.Controls.Add(this.button14);
   this.tabPage3.Controls.Add(this.button13);
   this.tabPage3.Controls.Add(this.button12);
   this.tabPage3.Controls.Add(this.button11);
   this.tabPage3.Controls.Add(this.richtext4);
   this.tabPage3.Controls.Add(this.label6);
   this.tabPage3.Controls.Add(this.button10);
   this.tabPage3.Controls.Add(this.label5);
   this.tabPage3.Controls.Add(this.textBox3);
   this.tabPage3.Controls.Add(this.button9);
   this.tabPage3.Controls.Add(this.label4);
   this.tabPage3.Controls.Add(this.textBox2);
   this.tabPage3.Location = new System.Drawing.Point(4, 21);
   this.tabPage3.Name = "tabPage3";
   this.tabPage3.Size = new System.Drawing.Size(560, 343);
   this.tabPage3.TabIndex = 2;
   this.tabPage3.Text = "加密解密文件";
   //
   // button15
   //
   this.button15.Location = new System.Drawing.Point(208, 128);
   this.button15.Name = "button15";
   this.button15.TabIndex = 12;
   this.button15.Text = "清空文本";
   this.button15.Click += new System.EventHandler(this.button15_Click);
   //
   // button14
   //
   this.button14.Location = new System.Drawing.Point(432, 88);
   this.button14.Name = "button14";
   this.button14.TabIndex = 11;
   this.button14.Text = "读取私匙";
   this.button14.Click += new System.EventHandler(this.button14_Click);
   //
   // button13
   //
   this.button13.Location = new System.Drawing.Point(432, 32);
   this.button13.Name = "button13";
   this.button13.TabIndex = 10;
   this.button13.Text = "读取公匙";
   this.button13.Click += new System.EventHandler(this.button13_Click);
   //
   // button12
   //
   this.button12.Location = new System.Drawing.Point(320, 88);
   this.button12.Name = "button12";
   this.button12.TabIndex = 9;
   this.button12.Text = "开始解密";
   this.button12.Click += new System.EventHandler(this.button12_Click);
   //
   // button11
   //
   this.button11.Location = new System.Drawing.Point(320, 32);
   this.button11.Name = "button11";
   this.button11.TabIndex = 8;
   this.button11.Text = "开始加密";
   this.button11.Click += new System.EventHandler(this.button11_Click);
   //
   // richtext4
   //
   this.richtext4.Location = new System.Drawing.Point(8, 152);
   this.richtext4.Name = "richtext4";
   this.richtext4.Size = new System.Drawing.Size(496, 168);
   this.richtext4.TabIndex = 7;
   this.richtext4.Text = "";
   //
   // label6
   //
   this.label6.Location = new System.Drawing.Point(16, 120);
   this.label6.Name = "label6";
   this.label6.Size = new System.Drawing.Size(168, 23);
   this.label6.TabIndex = 6;
   this.label6.Text = "加密与解密窗口:";
   //
   // button10
   //
   this.button10.Location = new System.Drawing.Point(224, 88);
   this.button10.Name = "button10";
   this.button10.TabIndex = 5;
   this.button10.Text = "Save ...";
   this.button10.Click += new System.EventHandler(this.button10_Click);
   //
   // label5
   //
   this.label5.Location = new System.Drawing.Point(16, 64);
   this.label5.Name = "label5";
   this.label5.Size = new System.Drawing.Size(216, 16);
   this.label5.TabIndex = 4;
   this.label5.Text = "请保存加密好或者解密后的文件";
   //
   // textBox3
   //
   this.textBox3.Location = new System.Drawing.Point(16, 88);
   this.textBox3.Name = "textBox3";
   this.textBox3.Size = new System.Drawing.Size(184, 21);
   this.textBox3.TabIndex = 3;
   this.textBox3.Text = "textBox3";
   //
   // button9
   //
   this.button9.Location = new System.Drawing.Point(224, 32);
   this.button9.Name = "button9";
   this.button9.TabIndex = 2;
   this.button9.Text = "Open ...";
   this.button9.Click += new System.EventHandler(this.button9_Click);
   //
   // label4
   //
   this.label4.Location = new System.Drawing.Point(16, 8);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(216, 16);
   this.label4.TabIndex = 1;
   this.label4.Text = "请选择要加密或者解密的文本文件";
   //
   // textBox2
   //
   this.textBox2.Location = new System.Drawing.Point(16, 32);
   this.textBox2.Name = "textBox2";
   this.textBox2.Size = new System.Drawing.Size(184, 21);
   this.textBox2.TabIndex = 0;
   this.textBox2.Text = "textBox2";
   //
   // tabPage4
   //
   this.tabPage4.Controls.Add(this.richtext5);
   this.tabPage4.Controls.Add(this.button21);
   this.tabPage4.Controls.Add(this.button20);
   this.tabPage4.Controls.Add(this.button19);
   this.tabPage4.Controls.Add(this.button18);
   this.tabPage4.Controls.Add(this.button17);
   this.tabPage4.Controls.Add(this.textBox5);
   this.tabPage4.Controls.Add(this.button16);
   this.tabPage4.Controls.Add(this.textBox4);
   this.tabPage4.Location = new System.Drawing.Point(4, 21);
   this.tabPage4.Name = "tabPage4";
   this.tabPage4.Size = new System.Drawing.Size(560, 343);
   this.tabPage4.TabIndex = 3;
   this.tabPage4.Text = "加密解密其它文件";
   //
   // textBox4
   //
   this.textBox4.Location = new System.Drawing.Point(24, 24);
   this.textBox4.Name = "textBox4";
   this.textBox4.Size = new System.Drawing.Size(240, 21);
   this.textBox4.TabIndex = 0;
   this.textBox4.Text = "textBox4";
   //
   // button16
   //
   this.button16.Location = new System.Drawing.Point(304, 24);
   this.button16.Name = "button16";
   this.button16.TabIndex = 1;
   this.button16.Text = "打开";
   this.button16.Click += new System.EventHandler(this.button16_Click);
   //
   // textBox5
   //
   this.textBox5.Location = new System.Drawing.Point(24, 56);
   this.textBox5.Name = "textBox5";
   this.textBox5.Size = new System.Drawing.Size(240, 21);
   this.textBox5.TabIndex = 2;
   this.textBox5.Text = "textBox5";
   //
   // button17
   //
   this.button17.Location = new System.Drawing.Point(304, 56);
   this.button17.Name = "button17";
   this.button17.TabIndex = 3;
   this.button17.Text = "保存";
   this.button17.Click += new System.EventHandler(this.button17_Click);
   //
   // button18
   //
   this.button18.Location = new System.Drawing.Point(48, 88);
   this.button18.Name = "button18";
   this.button18.TabIndex = 4;
   this.button18.Text = "读取公匙";
   this.button18.Click += new System.EventHandler(this.button18_Click);
   //
   // button19
   //
   this.button19.Location = new System.Drawing.Point(160, 88);
   this.button19.Name = "button19";
   this.button19.TabIndex = 5;
   this.button19.Text = "读取私匙";
   this.button19.Click += new System.EventHandler(this.button19_Click);
   //
   // button20
   //
   this.button20.Location = new System.Drawing.Point(112, 144);
   this.button20.Name = "button20";
   this.button20.TabIndex = 6;
   this.button20.Text = "加密文件";
   this.button20.Click += new System.EventHandler(this.button20_Click);
   //
   // button21
   //
   this.button21.Location = new System.Drawing.Point(232, 144);
   this.button21.Name = "button21";
   this.button21.TabIndex = 7;
   this.button21.Text = "解密文件";
   this.button21.Click += new System.EventHandler(this.button21_Click);
   //
   // richtext5
   //
   this.richtext5.Location = new System.Drawing.Point(88, 200);
   this.richtext5.Name = "richtext5";
   this.richtext5.Size = new System.Drawing.Size(304, 112);
   this.richtext5.TabIndex = 8;
   this.richtext5.Text = "";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(584, 453);
   this.Controls.Add(this.tabControl1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.tabControl1.ResumeLayout(false);
   this.tabPage1.ResumeLayout(false);
   this.tabPage2.ResumeLayout(false);
   this.tabPage3.ResumeLayout(false);
   this.tabPage4.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
        //得到钥匙信息  
  private void button1_Click(object sender, System.EventArgs e)
  {
   crypt=new RSACryptoServiceProvider();
   publickey=crypt.ToXmlString(false);
   richtext.Text="导出秘匙的情况下:\n"+publickey+"\n";
   privatekey=crypt.ToXmlString(true);
   string info="仅仅导出公匙的情况下:\n"+privatekey+"\n";
   richtext.AppendText(info);
   crypt.Clear(); 
  }
        //保存公匙信息
  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;  
  }
        //保存密匙信息
  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;   
  } 
        //用公匙加密
  private void button4_Click(object sender, System.EventArgs e)
  {
   if(textBox1.Text =="")
   {   
    MessageBox.Show("加密文字信息不能为空!");
    return;   
   }
   if(readpubkey==false)
   {
    MessageBox.Show("请打开公钥所在的文件!");
    return;
   }
   try
   {
    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);
    richtext2.Text="加密结果:\n"+encryttext+"\n"+"加密结束!";
   }
   catch
   {
    MessageBox.Show("请检查是否打开公匙或者公匙是否损坏!");
   
   }
  }
        //使用私匙解密
  private void button5_Click(object sender, System.EventArgs e)
  {
   if(readprikey==false)
   {
    MessageBox.Show("请打开密匙所在的文件!");
    return;
   }
   try
   {
    UTF8Encoding enc=new UTF8Encoding();
    byte [] decryptbyte;
    crypt.FromXmlString ( readprivatekey ) ;
    decryptbyte = crypt.Decrypt( bytes,false );
    string decrypttext=enc.GetString( decryptbyte );
    richtext3.Text = "解密结果:\n" + decrypttext + "\n" + "解密结束!" ;
   }
   catch
   {
    MessageBox.Show("请检查是否打开私匙或者私匙是否损坏!");
   
   }
  }
  //把钥匙信息写入文件
  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("成功保存公匙和密匙!"); 
  }
        //从文件中读取公匙信息
  private void button7_Click(object sender, System.EventArgs e)
  {   
      readpublickey = ReadPublicKey();
   readpubkey=true;
  }
 
        //从文件中读取私匙信息
  private void button8_Click(object sender, System.EventArgs e)
  {
   
      readprivatekey  = ReadPrivateKey();
   readprikey = true;
  }
 
        //打开加密或者解密的文件
  private void button9_Click(object sender, System.EventArgs e)
  {
      open = new OpenFileDialog( );
   open.Filter="Text File (*.txt)|*.txt|All Files (*.*)|*.*";
   open.ShowDialog();
   textBox2.Text=open.FileName;
   openla = true;
  }
        //保存加密或者解密的文件
  private void button10_Click(object sender, System.EventArgs e)
  {
   try
   {
    save=new SaveFileDialog();
    save.Filter="File Text (*.txt)|*.txt|All File (*.*)|*.*";
    save.ShowDialog();
    textBox3.Text=save.FileName;
    savela = true;
   }
   catch
   {
    MessageBox.Show("请输入文件名字!");
    return;
   }

  }
  
        //加密文本文件
  private void button11_Click(object sender, System.EventArgs e)
  {
   if(readpubkey != true)
   {
    MessageBox.Show("请打开公钥所在的文件!");
    return;
   }
   if(openla==true&&savela==true)
   {
    crypt=new RSACryptoServiceProvider();
    crypt.FromXmlString( readpublickey );
    UTF8Encoding enc=new UTF8Encoding();
    //读取原文件到一个string 去啊
    StreamReader sr=new StreamReader(textBox2.Text,Encoding.Default);
    string textinfo=sr.ReadToEnd();
    sr.Close();
  //  richtext4.AppendText( "\n原文件内容:\n" + textinfo + "\n" );
    //开始加密
    string readinfo=EncryptFile(textinfo,textBox3.Text);
    richtext4.AppendText( "加密文件已经保存到:" + textBox3.Text + "\n"  );
    richtext4.AppendText( "加密结果:\n" + readinfo + "\n加密结束!" ); 
   }
   else   
    MessageBox.Show("请选择你要加密的文件或者要保存的文件!");   
  }
        //解密文件
  private void button12_Click(object sender, System.EventArgs e)
  {
   if(readprikey==false)
   {
    MessageBox.Show("请打开密匙所在的文件!");
    return;
   }
   if(openla==true&&savela==true)
   {
    try
    {
     crypt=new RSACryptoServiceProvider();
     crypt.FromXmlString( readprivatekey );
     string decryptinfo = DecryptFile(textBox2.Text,textBox3.Text);
     richtext4.AppendText( "解密文件已经保存到:" + textBox3.Text+ "\n" );
     richtext4.AppendText("解密结果:\n" + decryptinfo + "\n" + "解密结束!\n");
    }
    catch
    {
     MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
     return;
    }
   }
   else
    MessageBox.Show("请选择你要解密的文件或者要保存的文件!");
  }
 
        //加密文件时候,同样要打开公钥匙
  private void button13_Click(object sender, System.EventArgs e)
  {   
   readpublickey = ReadPublicKey();
   readpubkey=true;
 
  }
        //解密文件时候,同样要打开私钥匙
  private void button14_Click(object sender, System.EventArgs e)
  {   
   readprivatekey  =ReadPrivateKey();
   readprikey=true; 
  }
  private void button15_Click(object sender, System.EventArgs e)
  {
   richtext4.Clear();  
  }
 
       //打开文件 
  private void button16_Click(object sender, System.EventArgs e)
  {
   open = new OpenFileDialog( );
   open.Filter="All Files (*.*)|*.*";
   open.ShowDialog();
   textBox4.Text=open.FileName;
   openla = true; 
  }
  //保存文件
  private void button17_Click(object sender, System.EventArgs e)
  {
   save=new SaveFileDialog();
   save.Filter="All File (*.*)|*.*";
   save.ShowDialog();
   textBox5.Text=save.FileName;
   savela = true;
  }
 

         //读取公钥匙
  private void button18_Click(object sender, System.EventArgs e)
  {
   readpublickey = ReadPublicKey();
   readpubkey=true; 
  }  
        //读取私钥匙  
  private void button19_Click(object sender, System.EventArgs e)
  {
   readprivatekey = ReadPrivateKey();
   readprikey=true; 
  }
        // 加密其它非文本文件啦
  private void button20_Click(object sender, System.EventArgs e)
  {
   if(readpubkey==false)
   {
    MessageBox.Show("请打开公匙所在的文件!");
    return;
   }
   if(openla==true&&savela==true)
   {
    try
    {
     crypt=new RSACryptoServiceProvider();
     crypt.FromXmlString( readpublickey );
     EncryptOther( textBox4.Text,textBox5.Text );
     richtext5.AppendText("加密过程结束!:\n" + "加密文件已经保存到:\n" + textBox5.Text + "\n");
    }
    catch
    {
     MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
     return;
    }
   }
   else
    MessageBox.Show("请选择你要解密的文件或者要保存的文件!");
 
  }
        //解密其它文件啦
  private void button21_Click(object sender, System.EventArgs e)
  {
   if(readprikey==false)
   {
    MessageBox.Show("请打开密匙所在的文件!");
    return;
   }
   if(openla==true&&savela==true)
   {
    try
    {
     crypt=new RSACryptoServiceProvider();
     crypt.FromXmlString( readprivatekey );
     //使用线程后,反而达不到效果             //原因?暂时不清晰
     //可以转换,但是转换要经过几分钟,才可以真正得到想要的转换的东西。
     th = new Thread( new ThreadStart(  DecryptUsingThread ) );
     th.Start();
    // DecryptOther(textBox4.Text,textBox5.Text);    
     richtext5.AppendText( "解密结束!\n" + "解密文件已经保存到:" + textBox5.Text+ "\n" );    
    }
    catch
    {
     MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");
     return;
    }
   }
   else
    MessageBox.Show("请选择你要解密的文件或者要保存的文件!"); 
  }

  //下面为自己定义的函数啦
  //单独一个读取公匙的函数
  private string ReadPublicKey()
  {
   
   string publickey;
   open = new OpenFileDialog( );
   open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";
   open.ShowDialog();  
   StreamReader sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);  
   publickey = sr.ReadToEnd();
   sr.Close();
   return publickey;  
  }
  //单独一个读取私匙的函数
  private string ReadPrivateKey()
  {
   string privatekey;   
   open = new OpenFileDialog( );
   open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";
   open.ShowDialog();
   StreamReader sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);
   privatekey  = sr.ReadToEnd();
   sr.Close();  
   return privatekey;
  }
  //加密文本文件 
  private string EncryptFile( string textinfo, string filename )
  {    
   UTF8Encoding enc=new UTF8Encoding();  
   byte [] bytes=enc.GetBytes( textinfo );
   int blockSize=0;
   if(crypt.KeySize==1024)
    blockSize=16;
   else
    blockSize=8;
   //同时写入到内存中去
   MemoryStream ms=new MemoryStream();
   //写入文件中去啊。
   FileStream fs=new FileStream(filename,FileMode.OpenOrCreate);
   byte[]rawblock,encryblock;
   for(int i=0;i<bytes.Length;i+=blockSize)
   {
    if((bytes.Length-i)>blockSize)
     rawblock=new byte[blockSize];
    else
     rawblock =new byte[bytes.Length-i];
    Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
    encryblock=crypt.Encrypt(rawblock,false);   
    fs.Write(encryblock,0,encryblock.Length);
    //写入到内存中去啊。
    ms.Write(encryblock,0,encryblock.Length);
   }
   //写入到内存中
   ms.Position = 0;
   byte [] decode = new byte[ms.Length];
   ms.Read(decode,0,(int)ms.Length);
   string decodeinfo = enc.GetString(decode,0,decode.Length);  
   fs.Flush();
   fs.Close();
   ms.Close();  
   return decodeinfo;
  }

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

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

  //加密其它非文本文件,当然可以包括文本文件
  private void EncryptOther(string file1,string file2)
  {
   FileStream picfs=new FileStream(file1,FileMode.Open);
   FileStream fs=new FileStream(file2,FileMode.OpenOrCreate);
   byte [] bytes=new byte[picfs.Length];
   picfs.Read(bytes,0,(int)picfs.Length);
   picfs.Close();
   int blockSize=0;
   if(crypt.KeySize==1024)
    blockSize=16;
   else blockSize=8;  
   byte[]rawblock,encryblock;
   for(int i=0;i<bytes.Length;i+=blockSize)
   {
    if((bytes.Length-i)>blockSize)
     rawblock=new byte[blockSize];
    else rawblock =new byte[bytes.Length-i];
    Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);
    encryblock=crypt.Encrypt(rawblock,false);
    fs.Write(encryblock,0,encryblock.Length);
   }
   fs.Flush();
   fs.Close();
  }
  //解密其它非文本文件,当然可以包括文本文件
  private void DecryptOther(string fileone,string filetwo)
  {
   FileStream fs=new FileStream(fileone,FileMode.Open);
   FileStream writopic=new FileStream(filetwo,FileMode.Create);
   byte [] bytes=new byte[fs.Length];
   fs.Read(bytes,0,(int)fs.Length);
   fs.Close();
   MemoryStream ms=new MemoryStream();
   int keySize=crypt.KeySize/8;
   byte[]rawblock,decryptblock;
   for(int i=0;i<bytes.Length;i+=keySize)
   {
    if( ( bytes.Length-i ) > keySize )
    {
     rawblock=new byte[keySize];
    }
    else
    {rawblock =new byte[bytes.Length-i];}

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

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

 

 

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: