欢迎光临
我们一直在努力

C#—非对称加密的一个程序-.NET教程,C#语言

建站超值云服务器,限时71元/月

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;

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.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;

/// <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 static byte [] onebytes ;

/// </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.save = new system.windows.forms.savefiledialog();

this.open = new system.windows.forms.openfiledialog();

this.tabcontrol1.suspendlayout();

this.tabpage1.suspendlayout();

this.tabpage2.suspendlayout();

this.suspendlayout();

//

// tabcontrol1

//

this.tabcontrol1.controls.add(this.tabpage1);

this.tabcontrol1.controls.add(this.tabpage2);

this.tabcontrol1.location = new system.drawing.point(32, 64);

this.tabcontrol1.name = "tabcontrol1";

this.tabcontrol1.selectedindex = 0;

this.tabcontrol1.size = new system.drawing.size(416, 280);

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(408, 255);

this.tabpage1.tabindex = 0;

this.tabpage1.text = "得到钥匙";

//

// button6

//

this.button6.location = new system.drawing.point(72, 168);

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(200, 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(128, 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(256, 96);

this.richtext.tabindex = 1;

this.richtext.text = "richtextbox1";

//

// button1

//

this.button1.location = new system.drawing.point(8, 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(408, 255);

this.tabpage2.tabindex = 1;

this.tabpage2.text = "加密解密文字";

//

// button8

//

this.button8.location = new system.drawing.point(216, 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(56, 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(200, 120);

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(200, 144);

this.richtext3.name = "richtext3";

this.richtext3.size = new system.drawing.size(120, 72);

this.richtext3.tabindex = 6;

this.richtext3.text = "richtextbox2";

//

// label2

//

this.label2.location = new system.drawing.point(48, 120);

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(40, 144);

this.richtext2.name = "richtext2";

this.richtext2.size = new system.drawing.size(120, 72);

this.richtext2.tabindex = 4;

this.richtext2.text = "richtextbox2";

//

// button5

//

this.button5.location = new system.drawing.point(200, 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(48, 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(168, 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 = "请输入加密的文字";

//

// form1

//

this.autoscalebasesize = new system.drawing.size(6, 14);

this.clientsize = new system.drawing.size(504, 357);

this.controls.add(this.tabcontrol1);

this.name = "form1";

this.text = "form1";

this.tabcontrol1.resumelayout(false);

this.tabpage1.resumelayout(false);

this.tabpage2.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 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 button4_click(object sender, system.eventargs e)

{

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);//encryptbyte);

richtext2.text="加密结果:\n"+encryttext+"\n"+"加密结束!";

}

private void button5_click(object sender, system.eventargs e)

{

utf8encoding enc=new utf8encoding();

byte [] decryptbyte;

crypt.fromxmlstring ( readprivatekey ) ;

decryptbyte = crypt.decrypt( bytes,false );

string decrypttext=enc.getstring( decryptbyte );

richtext3.text = "解密结果:\n" + decrypttext + "\n" + "解密结束!" ;

}

//从文件中读取公匙信息

private void button7_click(object sender, system.eventargs e)

{

streamreader sr ;

open = new openfiledialog( );

open.filter="text file (*.txt)|*.txt|all file (*.*)|*.* ";

// open.showdialog();

if(open.showdialog()==dialogresult.ok)

{

sr = new streamreader(open.filename,utf8encoding.utf8);

}

else

{

messagebox.show("发生错误!");

return;

}

readpublickey = sr.readtoend();

sr.close();

}

//从文件中读取私匙信息

private void button8_click(object sender, system.eventargs e)

{

open = new openfiledialog( );

open.filter="text file (*.txt)|*.txt|all file (*.*)|*.* ";

open.showdialog();

streamreader sr = new streamreader(open.filename,utf8encoding.utf8);

readprivatekey = sr.readtoend();

sr.close();

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C#—非对称加密的一个程序-.NET教程,C#语言
分享到: 更多 (0)