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

首頁 > 開發 > 綜合 > 正文

C#---非對稱加密的一個程序

2024-07-21 02:19:53
字體:
來源:轉載
供稿:網友
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();
}



}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 都江堰市| 会昌县| 白玉县| 富民县| 文登市| 汉川市| 收藏| 嘉鱼县| 宜兰市| 礼泉县| 广平县| 云南省| 仁寿县| 望奎县| 通辽市| 湖州市| 游戏| 金塔县| 阳山县| 清徐县| 万州区| 巴里| 弥渡县| 马鞍山市| 玛曲县| 仁寿县| 邵东县| 晋宁县| 浦东新区| 名山县| 虞城县| 木兰县| 晋中市| 阳东县| 寿光市| 秦皇岛市| 丹寨县| 秦安县| 南丰县| 恩施市| 孙吴县|