程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#—非對稱加密:加密文件 RSA

C#—非對稱加密:加密文件 RSA

編輯:關於C語言

C#—非對稱加密:加密文件
★★★★★★ ☆☆☆ ★★★★★★
●●○○     聶永     ○○●●
§§§§[email protected]§§§§
◆◆□□[email protected]□□◆◆
№№№№№№ ※※※ №№№№№№   
說明:
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);
  }
     
 }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved