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);
}
}
}
新闻热点
疑难解答