1. 程式人生 > >C#非對稱加密程式

C#非對稱加密程式

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();
}



}

 

相關推薦

C#對稱加密程式

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;

C#---對稱加密的一個程式

<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><script type="text/

對稱加密RSA的C#實現

1.對稱加密演算法對稱加密是最快速、最簡單的一種加密方式,加密(encryption)與解密(decryption)用的是同樣的金鑰(secret key)。對稱加密有很多種演算法,由於它效率很高,所以被廣泛使用在很多加密協議的核心當中。對稱加密通常使用的是相對較小的金鑰,一

c語言實現rsa nopadding對稱加密演算法(openssl api方式),匹配java後端 bouncycastle

公私鑰: —–BEGIN PUBLIC KEY—– MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC32V2IAfotG8bZhPF8/Bx8y65g EDycAMyTWmvpPCfagEKORO/WvdkTYim7yhG/+

對稱加密

數據加密 bytes codebase 私鑰 .get comm x509 公鑰 except   非對稱加密為數據的加密與解密提供了一個非常安全的方法,它使用了一對密鑰,公鑰(public key)和私鑰(private key)。私鑰只能由一方安全保管,不能外泄,而公鑰

java加密算法入門(三)-對稱加密詳解

共享數據 net clas 實例 查看 安全性 自己的 generator mir 1、簡單介紹 這幾天一直在看非對稱的加密,相比之前的兩篇內容,這次看了兩倍多的時間還雲裏霧裏的,所以這篇文章相對之前的兩篇,概念性的東西多了些,另外是代碼的每一步我都做了介紹,方便自己以後

對稱加密對稱加密

通過 lan pan 消息加密 ack 屬於 加密算法 效率 構建 1.對稱加密 對稱加密(也叫私鑰加密)指加密和解密使用相同密鑰的加密算法。有時又叫傳統密碼算法,就是加密密鑰能夠從解密密鑰中推算出來,同時解密密鑰也可以從加密密鑰中推算出來。而在大多數的對稱算法中,加密密

如何使用gpg工具實現公鑰加密對稱加密對稱加密)?

gpg工具 對稱加密 公鑰加密 非對稱加密 使用gpg實現公鑰加密【對稱加密】1、 對稱加密file文件gpg -c filels file.gpg------------------------對稱加密過程------------------------輸入口令,兩次,例如centos再查看

對稱加密 - SSH, HTTPS

none 知乎 ext question 使用 soft 對稱 table sim 你想用實際生活中的例子懂"非對稱加密"嗎, 請點擊知乎優秀解答"非對稱加密", 一看就懂!!!看完上面形象的解釋後, 再看這個總結性的概括https://www.zhihu.com/ques

Java加密與解密筆記(三) 對稱加密

arr 內容 phy 資料 密碼 load esp uid user 非對稱的特點是加密和解密時使用的是不同的鑰匙。密鑰分為公鑰和私鑰,用公鑰加密的數據只能用私鑰進行解密,反之亦然。 另外,密鑰還可以用於數字簽名。數字簽名跟上文說的消息摘要是一個道理,通過一定方法對數據內容

對稱加密技術- RSA算法數學原理分析

這樣的 另一個 href 使用 兩個 對稱 基礎 大於 深入淺出 非對稱加密技術,在現在網絡中,有非常廣泛應用。加密技術更是數字貨幣的基礎。 所謂非對稱,就是指該算法需要一對密鑰,使用其中一個(公鑰)加密,則需要用另一個(私鑰)才能解密。 但是對於其原理大部分同學應該都是一

[svc]對稱加密/對稱加密細枝末節-如何做到數據傳輸的authentication/data integrity/confidentiality(私密)

ecc 數據完整性 安全 post 數字簽名 .html 模式 數字證書 大量 對稱/非對稱/混合加密的冷知識 數據在互聯網上傳輸,要考慮安全性. 講到安全,要從三方面考慮: 1、authentication 每一個IP包的認證,確保合法源的數據 2、data integ

《區塊鏈100問》第60集:對稱加密算法是什麽?

有用 form link 區塊 log OS 保存 是什麽 png 對稱加密算法是指在加密和解密時使用的是同一個秘鑰。與對稱加密算法不同,非對稱加密算法需要公鑰和私鑰。公鑰和私鑰是一對,如果用公鑰對數據進行加密,只有用對應的私鑰才能解密。   非對稱加密與對稱加密相比,其

加密的類型及其相關算法--公鑰加密對稱加密

加密類型 公鑰加密 非對稱加密 前面講了幾節,對稱加密(解決:機密性),單向加密(解決:完整性),密鑰交換(解決:完整性)。但總是能被E在從中搗亂,有沒有一種辦法能解決驗證對方身份的方法呢? 這個時候公鑰加密算法出來了。眾所周知公鑰加密算法是密鑰對:公鑰(公開出去的,從私鑰中按照某種

RSA 對稱加密【轉】

omx javax 生成 lba 測試 data htm dog ash 演示代碼:https://pan.baidu.com/s/10rfSUUDEEHvCDEYH0oEVCw Base64工具類,可以讓rsa編碼的亂碼變成一串字符序列 1 pack

對稱加密對稱加密

.com 對稱加密 alt str mage nbsp com strong bubuko 對稱加密和非對稱加密

java 對稱加密RSA

init nco security stat deb 編碼 IT hal mex package com.aarony.test; import java.io.IOException; import java.security.KeyFactory; imp

python----RSA對稱加密算法使用

算法 *** utf 獲取 目的 怎麽加密 save 判斷 非對稱加密 最近在搞項目的接口持續性自動化測試,好久沒有更新博客了。 項目中接觸到很多加密相關的數據,很多項目都會用到非對稱加密算法來保證前端和服務器交互的數據安全。 下面介紹下python下怎麽使用RSA加密算法

php openssl_sign() 語法+RSA公私鑰加密解密,對稱加密演算法詳解

其實有時候覺得寫部落格好煩,就個函式就開篇部落格。很小的意見事情而已,知道的人看來多取一舉,或者說沒什麼必要,浪費時間,不知道的人就會很鬱悶。技術就是這樣的,懂的人覺得真的很簡單啊,不知道的人真的好難。。。 一般在跟第三方介面對接資料的時候,為了保證很多都使用的RSA簽名,沒性趣瞭解的同學只需要

應用加密一;對稱加密演算法揭祕

非對稱加密演算法   使用過程:   乙方生成兩把金鑰(公鑰和私鑰) 甲方獲取乙方的公鑰,然後用它對資訊加密。   乙方得到加密後的資訊,用私鑰解密,乙方也可用私鑰加密字串   甲方獲取乙方私鑰加密資料,用公鑰解密   優點:   更安全,金鑰越長,它就越難破解   缺點:   加密速度慢