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

C#—非对称加密:加密文件
★★★★★★ ☆☆☆ ★★★★★★
●●○○ 聂永 ○○●●
§§§§nie_[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 非对称加密
{
///

1<summary>   
2/// Form1 的摘要说明。   
3/// </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;
///

 1<summary>   
 2/// 必需的设计器变量。   
 3private static RSACryptoServiceProvider crypt;   
 4private static string privatekey;   
 5private static string publickey;   
 6private static byte [] bytes;   
 7private static string publicinfo;   
 8private static string privateinfo;   
 9private static string readpublickey;   
10private static string readprivatekey;   
11//文件打开和是否读取钥匙的标记   
12private bool openla = false;   
13private bool savela = false;   
14private bool readpubkey=false;   
15private bool readprikey=false;   
16private Thread th;   
17/// </summary>

private System.ComponentModel.Container components = null;

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

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

1<summary>   
2/// 清理所有正在使用的资源。   
3/// </summary>

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

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

1<summary>   
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
3/// 此方法的内容。   
4/// </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

///

1<summary>   
2/// 应用程序的主入口点。   
3/// </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 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

  1<bytes.length;i+=blocksize) if((bytes.length-i)="" {="">blockSize)   
  2rawblock=new byte[blockSize];   
  3else rawblock =new byte[bytes.Length-i];   
  4Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);   
  5encryblock=crypt.Encrypt(rawblock,false);   
  6fs.Write(encryblock,0,encryblock.Length);   
  7}   
  8fs.Flush();   
  9fs.Close();   
 10}   
 11//解密其它非文本文件   
 12private void DecryptOther(string fileone,string filetwo)   
 13{   
 14FileStream fs=new FileStream(@fileone,FileMode.Open);   
 15FileStream writopic=new FileStream(@filetwo,FileMode.Create);   
 16byte [] bytes=new byte[fs.Length];   
 17fs.Read(bytes,0,(int)fs.Length);   
 18fs.Close();   
 19MemoryStream ms=new MemoryStream();   
 20int keySize=crypt.KeySize/8;   
 21byte[]rawblock,decryptblock;   
 22for(int i=0;i<bytes.length;i+=keysize) (="" )="" bytes.length-i="" if(="" {=""> keySize )   
 23{   
 24rawblock=new byte[keySize];   
 25}   
 26else   
 27{rawblock =new byte[bytes.Length-i];} 
 28
 29Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);   
 30decryptblock = crypt.Decrypt( rawblock,false );   
 31ms.Write(decryptblock,0,decryptblock.Length);   
 32}   
 33ms.Position=0;   
 34byte [] decode=new byte[ms.Length];   
 35ms.Read(decode,0,(int)ms.Length);   
 36ms.Close();   
 37writopic.Write(decode,0,decode.Length);   
 38writopic.Close(); 
 39
 40}   
 41//创建一个函数,可以在线程中使用   
 42private void DecryptUsingThread()   
 43{   
 44DecryptOther(textBox4.Text,textBox5.Text);   
 45}   
 46//读取公钥匙   
 47private void button18_Click(object sender, System.EventArgs e)   
 48{   
 49readpublickey = ReadPublicKey();   
 50readpubkey=true;   
 51}   
 52//读取私钥匙   
 53private void button19_Click(object sender, System.EventArgs e)   
 54{   
 55readprivatekey = ReadPrivateKey();   
 56readprikey=true;   
 57}   
 58// 加密其它非文本文件啦   
 59private void button20_Click(object sender, System.EventArgs e)   
 60{   
 61if(readpubkey==false)   
 62{   
 63MessageBox.Show("请打开公匙所在的文件!");   
 64return;   
 65}   
 66if(openla==true&amp;&amp;savela==true)   
 67{   
 68try   
 69{   
 70crypt=new RSACryptoServiceProvider();   
 71crypt.FromXmlString( readpublickey );   
 72EncryptOther( textBox4.Text,textBox5.Text );   
 73richtext5.AppendText("加密过程结束!:\n" + "加密文件已经保存到:\n" + textBox5.Text + "\n");   
 74}   
 75catch   
 76{   
 77MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");   
 78return;   
 79}   
 80}   
 81else   
 82MessageBox.Show("请选择你要解密的文件或者要保存的文件!");   
 83  
 84}   
 85//解密其它文件啦   
 86private void button21_Click(object sender, System.EventArgs e)   
 87{   
 88if(readprikey==false)   
 89{   
 90MessageBox.Show("请打开密匙所在的文件!");   
 91return;   
 92}   
 93if(openla==true&amp;&amp;savela==true)   
 94{   
 95try   
 96{   
 97crypt=new RSACryptoServiceProvider();   
 98crypt.FromXmlString( readprivatekey );   
 99//使用线程后,反而达不到效果 //原因?暂时不清晰   
100//可以转换,但是转换要经过几分钟,才可以真正得到想要的转换的东西。   
101th = new Thread( new ThreadStart( DecryptUsingThread ) );   
102th.Start();   
103// DecryptOther(textBox4.Text,textBox5.Text);   
104richtext5.AppendText( "解密结束!\n" + "解密文件已经保存到:" + textBox5.Text+ "\n" );   
105}   
106catch   
107{   
108MessageBox.Show("请检查密匙文件是否和公匙相对应或者密匙文件损坏!");   
109return;   
110}   
111}   
112else   
113MessageBox.Show("请选择你要解密的文件或者要保存的文件!");   
114} 
115
116//下面为自己定义的函数啦   
117//单独一个读取公匙的函数   
118private string ReadPublicKey()   
119{   
120  
121string publickey;   
122open = new OpenFileDialog( );   
123open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";   
124open.ShowDialog();   
125StreamReader sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);   
126publickey = sr.ReadToEnd();   
127sr.Close();   
128return publickey;   
129}   
130//单独一个读取私匙的函数   
131private string ReadPrivateKey()   
132{   
133string privatekey;   
134open = new OpenFileDialog( );   
135open.Filter="Text File (*.txt)|*.txt|All File (*.*)|*.* ";   
136open.ShowDialog();   
137StreamReader sr = new StreamReader(open.FileName,UTF8Encoding.UTF8);   
138privatekey = sr.ReadToEnd();   
139sr.Close();   
140return privatekey;   
141}   
142//加密文本文件   
143private string EncryptFile( string textinfo, string filename )   
144{   
145UTF8Encoding enc=new UTF8Encoding();   
146byte [] bytes=enc.GetBytes( textinfo );   
147int blockSize=0;   
148if(crypt.KeySize==1024)   
149blockSize=16;   
150else   
151blockSize=8;   
152//同时写入到内存中去   
153MemoryStream ms=new MemoryStream();   
154//写入文件中去啊。   
155FileStream fs=new FileStream(@filename,FileMode.OpenOrCreate);   
156byte[]rawblock,encryblock;   
157for(int i=0;i<bytes.length;i+=blocksize) if((bytes.length-i)="" {="">blockSize)   
158rawblock=new byte[blockSize];   
159else   
160rawblock =new byte[bytes.Length-i];   
161Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);   
162encryblock=crypt.Encrypt(rawblock,false);   
163fs.Write(encryblock,0,encryblock.Length);   
164//写入到内存中去啊。   
165ms.Write(encryblock,0,encryblock.Length);   
166}   
167//写入到内存中   
168ms.Position = 0;   
169byte [] decode = new byte[ms.Length];   
170ms.Read(decode,0,(int)ms.Length);   
171string decodeinfo = enc.GetString(decode,0,decode.Length);   
172fs.Flush();   
173fs.Close();   
174ms.Close();   
175return decodeinfo;   
176} 
177
178//解密文本文件   
179private string DecryptFile(string encryptfile,string decryptfile)   
180{   
181//打开加密后的文件,读取文件内容   
182FileStream fs=new FileStream(@encryptfile,FileMode.Open);   
183byte [] bytes=new byte[fs.Length];   
184fs.Read(bytes,0,(int)fs.Length);   
185fs.Close();   
186//先写入到内存   
187MemoryStream ms=new MemoryStream();   
188//最好写入到解密后的一个文件   
189StreamWriter sw=new StreamWriter(decryptfile);   
190int keySize=crypt.KeySize/8;   
191byte[]rawblock,decryptblock;   
192for(int i=0;i<bytes.length;i+=keysize) (="" )="" bytes.length-i="" if(="" {=""> keySize )   
193{   
194rawblock=new byte[keySize];   
195}   
196else   
197{rawblock =new byte[bytes.Length-i];} 
198
199Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);   
200decryptblock = crypt.Decrypt( rawblock,false );   
201ms.Write(decryptblock,0,decryptblock.Length);   
202}   
203ms.Position=0;   
204byte [] decode=new byte[ms.Length];   
205ms.Read(decode,0,(int)ms.Length);   
206UTF8Encoding enc=new UTF8Encoding();   
207string text=enc.GetString(decode);   
208//写入到内存中去啊   
209sw.Write(text);   
210sw.Flush();   
211ms.Close();   
212sw.Close();   
213return text;   
214} 
215
216//加密其它非文本文件,当然可以包括文本文件   
217private void EncryptOther(string file1,string file2)   
218{   
219FileStream picfs=new FileStream(@file1,FileMode.Open);   
220FileStream fs=new FileStream(@file2,FileMode.OpenOrCreate);   
221byte [] bytes=new byte[picfs.Length];   
222picfs.Read(bytes,0,(int)picfs.Length);   
223picfs.Close();   
224int blockSize=0;   
225if(crypt.KeySize==1024)   
226blockSize=16;   
227else blockSize=8;   
228byte[]rawblock,encryblock;   
229for(int i=0;i<bytes.length;i+=blocksize) if((bytes.length-i)="" {="">blockSize)   
230rawblock=new byte[blockSize];   
231else rawblock =new byte[bytes.Length-i];   
232Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);   
233encryblock=crypt.Encrypt(rawblock,false);   
234fs.Write(encryblock,0,encryblock.Length);   
235}   
236fs.Flush();   
237fs.Close();   
238}   
239//解密其它非文本文件,当然可以包括文本文件   
240private void DecryptOther(string fileone,string filetwo)   
241{   
242FileStream fs=new FileStream(@fileone,FileMode.Open);   
243FileStream writopic=new FileStream(@filetwo,FileMode.Create);   
244byte [] bytes=new byte[fs.Length];   
245fs.Read(bytes,0,(int)fs.Length);   
246fs.Close();   
247MemoryStream ms=new MemoryStream();   
248int keySize=crypt.KeySize/8;   
249byte[]rawblock,decryptblock;   
250for(int i=0;i<bytes.length;i+=keysize) (="" )="" bytes.length-i="" if(="" {=""> keySize )   
251{   
252rawblock=new byte[keySize];   
253}   
254else   
255{rawblock =new byte[bytes.Length-i];} 
256
257Buffer.BlockCopy(bytes,i,rawblock,0,rawblock.Length);   
258decryptblock = crypt.Decrypt( rawblock,false );   
259ms.Write(decryptblock,0,decryptblock.Length);   
260}   
261ms.Position=0;   
262byte [] decode=new byte[ms.Length];   
263ms.Read(decode,0,(int)ms.Length);   
264ms.Close();   
265writopic.Write(decode,0,decode.Length);   
266writopic.Close(); 
267
268}   
269//创建一个函数,可以在线程中使用,经实验,效果不明显   
270private void DecryptUsingThread()   
271{   
272DecryptOther(textBox4.Text,textBox5.Text);   
273}   
274  
275}   
276}</bytes.length;i+=keysize)></bytes.length;i+=blocksize)></bytes.length;i+=keysize)></bytes.length;i+=blocksize)></bytes.length;i+=keysize)></bytes.length;i+=blocksize)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus