国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

C#—非對(duì)稱加密:加密文件 RSA

2024-07-21 02:27:13
字體:
供稿:網(wǎng)友
  • 本文來源于網(wǎng)頁設(shè)計(jì)愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。
  • c#—非對(duì)稱加密:加密文件
    ★★★★★★ ☆☆☆ ★★★★★★
    ●●○○     聶永     ○○●●
    §§§§[email protected]§§§§
    ◆◆□□[email protected]□□◆◆
    №№№№№№ ※※※ №№№№№№   
    說明:
    1.你要注意的是讀取文件的兩種不同方式;
    2.從已經(jīng)保存的鑰匙文件中讀取其內(nèi)容;
    3.這個(gè)程序做的很粗糙(要準(zhǔn)備考外語六級(jí),要準(zhǔn)備期末考試,實(shí)在很忙啊!),希望諸位能夠完善,然后也發(fā)表出來,在下表示感謝;
    4.參考書目: 《c#數(shù)據(jù)安全手冊(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 非對(duì)稱加密
    {
     /// <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>
      /// 必需的設(shè)計(jì)器變量。
      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;
      //文件打開和是否讀取鑰匙的標(biāo)記
      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 窗體設(shè)計(jì)器支持所必需的
       //
       initializecomponent();

       //
       // todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
       //
      }
      /// <summary>
      /// 清理所有正在使用的資源。
      /// </summary>
      protected override void dispose( bool disposing )
      {
       if( disposing )
       {
        if (components != null)
        {
         components.dispose();
        }
       }
       base.dispose( disposing );
      }

      #region windows 窗體設(shè)計(jì)器生成的代碼
      /// <summary>
      /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
      /// 此方法的內(nèi)容。
      /// </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 = "解密結(jié)果";
       //
       // 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 = "請(qǐng)輸入加密的文字";
       //
       // 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 = "請(qǐng)保存加密好或者解密后的文件";
       //
       // 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 = "請(qǐng)選擇要加密或者解密的文本文件";
       //
       // 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>
      /// 應(yīng)用程序的主入口點(diǎn)。
      /// </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="導(dǎo)出秘匙的情況下:/n"+publickey+"/n";
       privatekey=crypt.toxmlstring(true);
       string info="僅僅導(dǎo)出公匙的情況下:/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("請(qǐng)打開公鑰所在的文件!");
        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="加密結(jié)果:/n"+encryttext+"/n"+"加密結(jié)束!";
       }
       catch
       {
        messagebox.show("請(qǐng)檢查是否打開公匙或者公匙是否損壞!");
       
       }
      }
            //使用私匙解密
      private void button5_click(object sender, system.eventargs e)
      {
       if(readprikey==false)
       {
        messagebox.show("請(qǐng)打開密匙所在的文件!");
        return;
       }
       try
       {
        utf8encoding enc=new utf8encoding();
        byte [] decryptbyte;
        crypt.fromxmlstring ( readprivatekey ) ;
        decryptbyte = crypt.decrypt( bytes,false );
        string decrypttext=enc.getstring( decryptbyte );
        richtext3.text = "解密結(jié)果:/n" + decrypttext + "/n" + "解密結(jié)束!" ;
       }
       catch
       {
        messagebox.show("請(qǐng)檢查是否打開私匙或者私匙是否損壞!");
       
       }
      }
      //把鑰匙信息寫入文件
      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("請(qǐng)輸入文件名字!");
        return;
       }

      }
      
            //加密文本文件
      private void button11_click(object sender, system.eventargs e)
      {
       if(readpubkey != true)
       {
        messagebox.show("請(qǐng)打開公鑰所在的文件!");
        return;
       }
       if(openla==true&&savela==true)
       {
        crypt=new rsacryptoserviceprovider();
        crypt.fromxmlstring( readpublickey );
        utf8encoding enc=new utf8encoding();
        //讀取原文件到一個(gè)string 去啊
        streamreader sr=new streamreader(textbox2.text,encoding.default);
        string textinfo=sr.readtoend();
        sr.close();
      //  richtext4.appendtext( "/n原文件內(nèi)容:/n" + textinfo + "/n" );
        //開始加密
        string readinfo=encryptfile(textinfo,textbox3.text);
        richtext4.appendtext( "加密文件已經(jīng)保存到:" + textbox3.text + "/n"  );
        richtext4.appendtext( "加密結(jié)果:/n" + readinfo + "/n加密結(jié)束!" ); 
       }
       else   
        messagebox.show("請(qǐng)選擇你要加密的文件或者要保存的文件!");   
      }
            //解密文件
      private void button12_click(object sender, system.eventargs e)
      {
       if(readprikey==false)
       {
        messagebox.show("請(qǐng)打開密匙所在的文件!");
        return;
       }
       if(openla==true&&savela==true)
       {
        try
        {
         crypt=new rsacryptoserviceprovider();
         crypt.fromxmlstring( readprivatekey );
         string decryptinfo = decryptfile(textbox2.text,textbox3.text);
         richtext4.appendtext( "解密文件已經(jīng)保存到:" + textbox3.text+ "/n" );
         richtext4.appendtext("解密結(jié)果:/n" + decryptinfo + "/n" + "解密結(jié)束!/n");
        }
        catch
        {
         messagebox.show("請(qǐng)檢查密匙文件是否和公匙相對(duì)應(yīng)或者密匙文件損壞!");
         return;
        }
       }
       else
        messagebox.show("請(qǐng)選擇你要解密的文件或者要保存的文件!");
      }
     
            //加密文件時(shí)候,同樣要打開公鑰匙
      private void button13_click(object sender, system.eventargs e)
      {   
       readpublickey = readpublickey();
       readpubkey=true;
     
      }
            //解密文件時(shí)候,同樣要打開私鑰匙
      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("請(qǐng)打開公匙所在的文件!");
        return;
       }
       if(openla==true&&savela==true)
       {
        try
        {
         crypt=new rsacryptoserviceprovider();
         crypt.fromxmlstring( readpublickey );
         encryptother( textbox4.text,textbox5.text );
         richtext5.appendtext("加密過程結(jié)束!:/n" + "加密文件已經(jīng)保存到:/n" + textbox5.text + "/n");
        }
        catch
        {
         messagebox.show("請(qǐng)檢查密匙文件是否和公匙相對(duì)應(yīng)或者密匙文件損壞!");
         return;
        }
       }
       else
        messagebox.show("請(qǐng)選擇你要解密的文件或者要保存的文件!");
     
      }
            //解密其它文件啦
      private void button21_click(object sender, system.eventargs e)
      {
       if(readprikey==false)
       {
        messagebox.show("請(qǐng)打開密匙所在的文件!");
        return;
       }
       if(openla==true&&savela==true)
       {
        try
        {
         crypt=new rsacryptoserviceprovider();
         crypt.fromxmlstring( readprivatekey );
         //使用線程后,反而達(dá)不到效果             //原因?暫時(shí)不清晰
         //可以轉(zhuǎn)換,但是轉(zhuǎn)換要經(jīng)過幾分鐘,才可以真正得到想要的轉(zhuǎn)換的東西。
         th = new thread( new threadstart(  decryptusingthread ) );
         th.start();
        // decryptother(textbox4.text,textbox5.text);    
         richtext5.appendtext( "解密結(jié)束!/n" + "解密文件已經(jīng)保存到:" + textbox5.text+ "/n" );    
        }
        catch
        {
         messagebox.show("請(qǐng)檢查密匙文件是否和公匙相對(duì)應(yīng)或者密匙文件損壞!");
         return;
        }
       }
       else
        messagebox.show("請(qǐng)選擇你要解密的文件或者要保存的文件!"); 
      }

      //下面為自己定義的函數(shù)啦
      //單獨(dú)一個(gè)讀取公匙的函數(shù)
      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;  
      }
      //單獨(dú)一個(gè)讀取私匙的函數(shù)
      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;
       //同時(shí)寫入到內(nèi)存中去
       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);
        //寫入到內(nèi)存中去啊。
        ms.write(encryblock,0,encryblock.length);
       }
       //寫入到內(nèi)存中
       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)
      {
       //打開加密后的文件,讀取文件內(nèi)容
       filestream fs=new filestream(@encryptfile,filemode.open);
       byte [] bytes=new byte[fs.length];
       fs.read(bytes,0,(int)fs.length);
       fs.close();
       //先寫入到內(nèi)存
       memorystream ms=new memorystream();
       //最好寫入到解密后的一個(gè)文件
       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);
       //寫入到內(nèi)存中去啊
       sw.write(text);
       sw.flush(); 
       ms.close();
       sw.close();
       return text;
      }

      //加密其它非文本文件,當(dāng)然可以包括文本文件
      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();
      }
      //解密其它非文本文件,當(dāng)然可以包括文本文件
      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();

      }
      //創(chuàng)建一個(gè)函數(shù),可以在線程中使用,經(jīng)實(shí)驗(yàn),效果不明顯
      private void decryptusingthread()
      {
       decryptother(textbox4.text,textbox5.text);
      }
         
     }
    }

     

     

    發(fā)表評(píng)論 共有條評(píng)論
    用戶名: 密碼:
    驗(yàn)證碼: 匿名發(fā)表
    主站蜘蛛池模板: 黄龙县| 青田县| 图木舒克市| 隆昌县| 斗六市| 湘潭市| 北宁市| 广州市| 五寨县| 锦州市| 承德县| 靖宇县| 衡东县| 石首市| 宿州市| 上饶市| 通许县| 枣阳市| 靖宇县| 革吉县| 龙山县| 静安区| 武强县| 安多县| 溧阳市| 遂溪县| 西和县| 蒙阴县| 乐至县| 杭锦旗| 新邵县| 绥德县| 德阳市| 晋宁县| 延川县| 西充县| 静海县| 鄂尔多斯市| 雅安市| 阿克苏市| 青川县|