當前位置:成語大全網 - 漢語詞典 - 如何在中加密數據?網

如何在中加密數據?網

/China/TechNet/security/guidance/sec mod 39 . mspx(最佳理論數據)

建立壹個win窗體程序,然後把代碼復制到後臺,就可以實現C#非對稱加密程序。

使用系統;

使用系統。繪圖;

使用系統。收藏;

使用系統。組件模型;

使用系統。Windows . Forms

使用系統。數據;

使用系統。木衛壹;

使用系統。文本;

使用系統。安全。密碼術;

命名空間不對稱加密

{

///& lt;總結& gt

///form 1的匯總描述。

///& lt;/summary & gt;

公共類Form1 : System。窗口.窗體.窗體

{

私人系統。windows . forms . tab control tab control 1;

私人系統。windows . forms . tab page tab page 1;

私人系統。windows . forms . tab page tab page 2;

私人系統。windows . forms . button button 1;

私人系統。Windows . Forms.Button button2

私人系統。Windows . Forms.Button button3

私人系統。Windows.Forms.SaveFileDialog保存;

私人系統。windows . forms . label label 1;

私人系統。windows . forms . textbox textbox 1;

私人系統。Windows . Forms.Button button4

私人系統。Windows . Forms.Button button5

私人系統。Windows . Forms.Label label2

私人系統。windows . forms . richtextbox richtext 2;

私人系統。Windows.Forms.OpenFileDialog打開;

私人系統。Windows.Forms.Label標簽3;

私人系統。windows . forms . richtextbox richtext 3;

私人系統。windows . forms . richtextbox rich text;

私人系統。Windows . Forms.Button button6

私人系統。Windows . Forms.Button button7

私人系統。Windows . Forms.Button button8

///& lt;總結& gt

///必需的設計器變量。

私有靜態RSACryptoServiceProvider crypt

私有靜態字符串privatekey

私有靜態字符串publickey

私有靜態字節[]字節;

私有靜態字符串publicinfo

私有靜態字符串privateinfo

私有靜態字符串readpublickey

私有靜態字符串readprivatekey

/////私有靜態字節[]one bytes;

///& lt;/summary & gt;

私人系統。ComponentModel.Container組件= null

公共表單1()

{

//

// Windows窗體設計器支持必要的

//

initialize component();

//

// TODO:在InitializeComponent調用後添加任何構造函數代碼。

//

}

///& lt;總結& gt

///清理所有正在使用的資源。

///& lt;/summary & gt;

受保護的覆蓋void Dispose( bool disposing)

{

如果(處理)

{

如果(組件!=空)

{

組件。dispose();

}

}

基地。Dispose(處置);

}

由Windows窗體設計器生成的# regionCode

///& lt;總結& gt

///設計器支持所需的方法-不要使用代碼編輯器修改它。

///此方法的內容。

///& lt;/summary & gt;

私有void InitializeComponent()

{

this.tabControl1 =新系統。windows . forms . tab control();

this.tabPage1 =新系統。windows . forms . tab page();

this.button6 =新系統。windows . forms . button();

this.button3 =新系統。windows . forms . button();

this.button2 =新系統。windows . forms . button();

this.richtext =新系統。windows . forms . richtextbox();

this.button1 =新系統。windows . forms . button();

this.tabPage2 =新系統。windows . forms . tab page();

this.button8 =新系統。windows . forms . button();

this.button7 =新系統。windows . forms . button();

this.label3 =新系統。windows . forms . label();

this.richtext3 =新系統。windows . forms . richtextbox();

this.label2 =新系統。windows . forms . label();

this.richtext2 =新系統。windows . forms . richtextbox();

this.button5 =新系統。windows . forms . button();

this.button4 =新系統。windows . forms . button();

this.textBox1 =新系統。windows . forms . textbox();

this.label1 =新系統。windows . forms . label();

this.save =新系統。windows . forms . savefiledialog();

this.open =新系統。windows . forms . openfile dialog();

this.tabControl1。suspend layout();

this.tabPage1。suspend layout();

this . tab page 2 . suspend layout();

這個。suspend layout();

//

// tabControl1

//

this.tabControl1。controls . Add(this . tab page 1);

this.tabControl1。controls . Add(this . tab page 2);

this.tabControl1。位置=新系統。繪圖點(32,64);

this.tabControl1。name = " tab control 1 ";

this.tabControl1。SelectedIndex = 0;

this.tabControl1。大小=新系統。圖紙.尺寸(416,280);

this.tabControl1。TabIndex = 0;

//

// tabPage1

//

this.tabPage1。controls . Add(this . button 6);

this.tabPage1。controls . Add(this . button 3);

this.tabPage1。controls . Add(this . button 2);

this.tabPage1。controls . Add(this . rich text);

this.tabPage1。controls . Add(this . button 1);

this.tabPage1。位置=新系統。繪圖.點(4,21);

this.tabPage1。name = " tab page 1 ";

this.tabPage1。大小=新系統。圖紙。尺寸(408,255);

this.tabPage1。TabIndex = 0;

This.tabPage1。Text = "獲取密鑰";

//

//按鈕6

//

this.button6.Location =新系統。圖紙.點(72,168);

this . button 6 . name = " button 6 ";

this . button 6 . tabindex = 4;

This.button6.Text = "寫入文件";

this.button6.Click +=新系統。EventHandler(this . button 6 _ Click);

//

//按鈕3

//

this.button3.Location =新系統。繪圖.點(200,16);

this . button 3 . name = " button 3 ";

this . button 3 . tabindex = 3;

This.button3.Text = "保存私鑰";

this.button3.Click +=新系統。EventHandler(this . button 3 _ Click);

//

//按鈕2

//

this.button2.Location =新系統。繪圖.點(128,16);

this . button 2 . name = " button 2 ";

this . button 2 . tabindex = 2;

This.button2.Text = "保存公鑰";

this.button2.Click +=新系統。EventHandler(this . button 2 _ Click);

//

// richtext

//

this.richtext.Location =新系統。繪圖.點(16,56);

this . rich text . name = " rich text ";

this.richtext.Size =新系統。圖紙。尺寸(256,96);

this . rich text . tabindex = 1;

this . rich text . text = " rich textbox 1 ";

//

// button1

//

this.button1。位置=新系統。圖紙.點(8,16);

this.button1。name = " button 1 ";

this.button1。大小=新系統。圖紙。尺寸(88,23);

this.button1。TabIndex = 0;

This.button1。Text = "獲取關鍵信息";

this.button1。點擊+=新系統。EventHandler(this . button 1 _ Click);

//

// tabPage2

//

this . tab page 2 . controls . add(this . button 8);

this . tab page 2 . controls . add(this . button 7);

this . tab page 2 . controls . add(this . label 3);

this . tab page 2 . controls . add(this . rich text 3);

this . tab page 2 . controls . add(this . label 2);

this . tab page 2 . controls . add(this . rich text 2);

this . tab page 2 . controls . add(this . button 5);

this . tab page 2 . controls . add(this . button 4);

this . tab page 2 . controls . add(this . textbox 1);

this . tab page 2 . controls . add(this . label 1);

this.tabPage2.Location =新系統。繪圖.點(4,21);

this . tab page 2 . name = " tab page 2 ";

this.tabPage2.Size =新系統。圖紙。尺寸(408,255);

this . tab page 2 . tabindex = 1;

This.tabPage2.Text = "加密和解密文本";

//

//按鈕8

//

this.button8.Location =新系統。繪圖.點(216,72);

this . button 8 . name = " button 8 ";

this . button 8 . tabindex = 9;

This.button8.Text = "讀取私鑰";

this.button8.Click +=新系統。EventHandler(this . button 8 _ Click);

//

//按鈕7

//

this.button7.Location =新系統。繪圖點(56,72);

this . button 7 . name = " button 7 ";

this . button 7 . tabindex = 8;

This.button7.Text = "讀取公鑰";

this.button7.Click +=新系統。EventHandler(this . button 7 _ Click);

//

//標簽3

//

this.label3.Location =新系統。繪圖.點(200,120);

this . label 3 . name = " label 3 ";

this.label3.Size =新系統。圖紙.尺寸(100,16);

this . label 3 . tabindex = 7;

This.label3.Text = "解密結果";

//

// richtext3

//

this.richtext3.Location =新系統。繪圖.點(200,144);

this . rich text 3 . name = " rich text 3 ";

this.richtext3.Size =新系統。圖紙.尺寸(120,72);

this . rich text 3 . tabindex = 6;

this . rich text 3 . text = " rich textbox 2 ";

//

// label2

//

this.label2.Location =新系統。圖紙.點(48,120);

this . label 2 . name = " label 2 ";

this.label2.Size =新系統。圖紙.尺寸(128,16);

this . label 2 . tabindex = 5;

This.label2.Text = "加密窗口:";

//

// richtext2

//

this.richtext2.Location =新系統。繪圖點(40,144);

this . rich text 2 . name = " rich text 2 ";

this.richtext2.Size =新系統。圖紙.尺寸(120,72);

this . rich text 2 . tabindex = 4;

this . rich text 2 . text = " rich textbox 2 ";

//

//按鈕5

//

this.button5.Location =新系統。繪圖.點(200,96);

this . button 5 . name = " button 5 ";

this.button5.Size =新系統。圖紙.尺寸(112,23);

this . button 5 . tabindex = 3;

This.button5.Text = "用私鑰解密";

this.button5.Click +=新系統。EventHandler(this . button 5 _ Click);

//

//按鈕4

//

this.button4.Location =新系統。繪圖點(48,96);

this . button 4 . name = " button 4 ";

this.button4.Size =新系統。圖紙。尺寸(96,23);

this . button 4 . tabindex = 2;

This.button4.Text = "使用公鑰加密";

this.button4.Click +=新系統。EventHandler(this . button 4 _ Click);

//

// textBox1

//

this.textBox1。位置=新系統。繪圖.點(104,32);

this.textBox1。Multiline = true

this.textBox1。name = " textbox 1 ";

this.textBox1。大小=新系統。圖紙.尺寸(168,32);

this.textBox1。TabIndex = 1;

this.textBox1。text = " textbox 1 ";

//

// label1

//

this.label1。位置=新系統。繪圖.點(128,8);

this.label1。name = " label 1 ";

this.label1。大小=新系統。圖紙.尺寸(120,16);

this.label1。TabIndex = 0;

This.label1。Text = "請輸入加密文本";

//

// Form1

//

這個。AutoScaleBaseSize =新系統。圖紙尺寸(6,14);

這個。ClientSize =新系統。圖紙。尺寸(504,357);

這個。controls . Add(this . tab control 1);

這個。name = " form 1 ";

這個。text = " form 1 ";

this.tabControl1。ResumeLayout(false);

this.tabPage1。ResumeLayout(false);

this . tab page 2 . resume layout(false);

這個。ResumeLayout(false);

}

#結束區域

///& lt;總結& gt

///應用程序的主入口點。

///& lt;/summary & gt;

[靜態線程]

靜態空幹管()

{

申請。run(new form 1());

}

//獲取密鑰信息

私有void button1_Click(對象發送方,系統。EventArgs e)

{

crypt = new RSACryptoServiceProvider();

公共密鑰=crypt。ToXmlString(false);

RichtextText= "如果導出密鑰:\ n "+public key+" \ n ";

privatekey =地穴。ToXmlString(true);

String info= "僅在導出公鑰時:\ n "+private key+" \ n ";

richtext。AppendText(info);

地穴。clear();

}

//保存公鑰信息

私有void button2_Click(對象發送方,系統。EventArgs e)

{

save = new SaveFileDialog();

保存。Filter= "文件文本(*。txt)|*。txt |所有文件(*。*)|*.*";

保存。ShowDialog();

publicinfo =保存。文件名;

}

//保存密鑰信息

私有void button3_Click(對象發送方,系統。EventArgs e)

{

save = new SaveFileDialog();

保存。Filter= "文件文本(*。txt)|*。txt |所有文件(*。*)|*.*";

保存。ShowDialog();

privateinfo =保存。文件名;

}

//將密鑰信息寫入文件

私有void button6_Click(對象發送方,系統。EventArgs e)

{

StreamWriter one = new StreamWriter(public info,true,UTF8Encoding。UTF8);

壹個。write(public key);

StreamWriter two = new StreamWriter(private info,true,UTF8Encoding。UTF8);

兩個。write(private key);

壹個。flush();

兩個。flush();

壹個。close();

兩個。close();

MessageBox。Show("成功保存公鑰和密鑰!");

}

//用公鑰加密

私有void button4_Click(對象發送方,系統。EventArgs e)

{

crypt = new RSACryptoServiceProvider();

ut F8 encoding enc = new ut F8 encoding();

字節=enc。GetBytes(textBox1。正文);

地穴。from XML string(read public key);

字節= crypt。加密(字節,假);

string encryttext=enc。GetString(字節);//encrypt byte);

Richtext2。Text= "加密結果:\ n"+encrytext+"\ n"+"加密結束!";

}

私有void button5_Click(對象發送方,系統。EventArgs e)

{

ut F8 encoding enc = new ut F8 encoding();

byte []解密字節;

地穴。from XML string(readprivatekey);

decryptbyte = crypt。解密(字節,假);

string decrypttext=enc。GetString(decrypt byte);

Richtext3。Text = "解密結果:\n"+decrypttext+"\n"+"解密結束!";

}

//從文件中讀取公鑰信息

私有void button7_Click(對象發送方,系統。EventArgs e)

{

StreamReader sr

open = new openfile dialog();

打開。Filter= "文本文件(*。txt)|*。txt |所有文件(*。*)|*.* ";

//打開。ShowDialog();

如果(打開。ShowDialog()==DialogResult。好的)

{

sr =新StreamReader(打開。文件名,utf8編碼。UTF8);

}

其他

{

MessageBox。顯示("出現錯誤!");

返回;

}

read public key = Sr . ReadToEnd();

Sr . Close();

}

//從文件中讀取私鑰信息

私有void button8_Click(對象發送方,系統。EventArgs e)

{

open = new openfile dialog();

打開。Filter= "文本文件(*。txt)|*。txt |所有文件(*。*)|*.* ";

打開。ShowDialog();

StreamReader sr =新的StreamReader(打開。文件名,utf8編碼。UTF8);

readprivatekey = Sr . ReadToEnd();

Sr . Close();

}

}

}