?? rsa加密解密及rsa簽名和驗(yàn)證.txt
字號(hào):
}
}
#endregion
#region RSA 簽名驗(yàn)證
public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
{
try
{
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的時(shí)候HASH算法為MD5
RSADeformatter.SetHashAlgorithm("MD5");
if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw ex;
}
}
public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData)
{
try
{
byte[] HashbyteDeformatter;
HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的時(shí)候HASH算法為MD5
RSADeformatter.SetHashAlgorithm("MD5");
if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw ex;
}
}
public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData)
{
try
{
byte[] DeformatterData;
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的時(shí)候HASH算法為MD5
RSADeformatter.SetHashAlgorithm("MD5");
DeformatterData =Convert.FromBase64String(p_strDeformatterData);
if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw ex;
}
}
public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
{
try
{
byte[] DeformatterData;
byte[] HashbyteDeformatter;
HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的時(shí)候HASH算法為MD5
RSADeformatter.SetHashAlgorithm("MD5");
DeformatterData =Convert.FromBase64String(p_strDeformatterData);
if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw ex;
}
}
#endregion
#endregion
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//frmRSACryptionTest.cs
///////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace RSAApplication
{
/// <summary>
/// frmRSACryptionTest 的摘要說(shuō)明。
/// </summary>
public class frmRSACryptionTest : System.Windows.Forms.Form
{
#region 必需的設(shè)計(jì)器變量
/// <summary>
/// 必需的設(shè)計(jì)器變量
/// </summary>
private System.Windows.Forms.Button btnBuildKey;
private System.Windows.Forms.TextBox txtKeyPublic;
private System.Windows.Forms.TextBox txtKeyPrivate;
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnRSAEncrypt;
private System.Windows.Forms.TextBox txtRSADecrypt;
private System.Windows.Forms.Button btnRSADecrypt;
private System.Windows.Forms.TextBox txtSource;
private System.Windows.Forms.TextBox txtRSAEncrypt;
private System.Windows.Forms.Button btnSignature;
private System.Windows.Forms.Button btnDeformatter;
private System.Windows.Forms.Button btnGetHashSignature;
private System.Windows.Forms.Button btnGetHashDeformatter;
private System.Windows.Forms.TextBox txtSignature;
private System.Windows.Forms.TextBox txtGetHashSignature;
private System.Windows.Forms.TextBox txtGetHashDeformatter;
private string m_strKeyPrivate = "";
private string m_strKeyPublic = "";
private string m_strHashbyteSignature = "";
private string m_strHashbyteDeformatter = "";
private string m_strEncryptedSignatureData = "";
#endregion
#region 構(gòu)造函數(shù)
public frmRSACryptionTest()
{
//
// Windows 窗體設(shè)計(jì)器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.btnBuildKey = new System.Windows.Forms.Button();
this.txtKeyPublic = new System.Windows.Forms.TextBox();
this.txtKeyPrivate = new System.Windows.Forms.TextBox();
this.btnRSAEncrypt = new System.Windows.Forms.Button();
this.txtSource = new System.Windows.Forms.TextBox();
this.txtRSAEncrypt = new System.Windows.Forms.TextBox();
this.txtRSADecrypt = new System.Windows.Forms.TextBox();
this.btnRSADecrypt = new System.Windows.Forms.Button();
this.btnDeformatter = new System.Windows.Forms.Button();
this.btnSignature = new System.Windows.Forms.Button();
this.txtSignature = new System.Windows.Forms.TextBox();
this.btnGetHashSignature = new System.Windows.Forms.Button();
this.btnGetHashDeformatter = new System.Windows.Forms.Button();
this.txtGetHashSignature = new System.Windows.Forms.TextBox();
this.txtGetHashDeformatter = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnBuildKey
//
this.btnBuildKey.Location = new System.Drawing.Point(11, 17);
this.btnBuildKey.Name = "btnBuildKey";
this.btnBuildKey.Size = new System.Drawing.Size(77, 34);
this.btnBuildKey.TabIndex = 0;
this.btnBuildKey.Text = "產(chǎn)生密鑰";
this.btnBuildKey.Click += new System.EventHandler(this.btnBuildKey_Click);
//
// txtKeyPublic
//
this.txtKeyPublic.Location = new System.Drawing.Point(137, 11);
this.txtKeyPublic.Multiline = true;
this.txtKeyPublic.Name = "txtKeyPublic";
this.txtKeyPublic.Size = new System.Drawing.Size(602, 44);
this.txtKeyPublic.TabIndex = 1;
this.txtKeyPublic.Text = "";
//
// txtKeyPrivate
//
this.txtKeyPrivate.Location = new System.Drawing.Point(137, 58);
this.txtKeyPrivate.Multiline = true;
this.txtKeyPrivate.Name = "txtKeyPrivate";
this.txtKeyPrivate.Size = new System.Drawing.Size(602, 44);
this.txtKeyPrivate.TabIndex = 2;
this.txtKeyPrivate.Text = "";
//
// btnRSAEncrypt
//
this.btnRSAEncrypt.Location = new System.Drawing.Point(11, 157);
this.btnRSAEncrypt.Name = "btnRSAEncrypt";
this.btnRSAEncrypt.Size = new System.Drawing.Size(77, 34);
this.btnRSAEncrypt.TabIndex = 3;
this.btnRSAEncrypt.Text = "RSA加密";
this.btnRSAEncrypt.Click += new System.EventHandler(this.btnRSAEncrypt_Click);
//
// txtSource
//
this.txtSource.Location = new System.Drawing.Point(137, 108);
this.txtSource.Multiline = true;
this.txtSource.Name = "txtSource";
this.txtSource.Size = new System.Drawing.Size(602, 44);
this.txtSource.TabIndex = 4;
this.txtSource.Text = "字串不能太長(zhǎng)j——km,.ewm.m, .vkj中國(guó)福建";
//
// txtRSAEncrypt
//
this.txtRSAEncrypt.Location = new System.Drawing.Point(137, 155);
this.txtRSAEncrypt.Multiline = true;
this.txtRSAEncrypt.Name = "txtRSAEncrypt";
this.txtRSAEncrypt.Size = new System.Drawing.Size(602, 44);
this.txtRSAEncrypt.TabIndex = 5;
this.txtRSAEncrypt.Text = "";
//
// txtRSADecrypt
//
this.txtRSADecrypt.Location = new System.Drawing.Point(137, 203);
this.txtRSADecrypt.Multiline = true;
this.txtRSADecrypt.Name = "txtRSADecrypt";
this.txtRSADecrypt.Size = new System.Drawing.Size(602, 44);
this.txtRSADecrypt.TabIndex = 6;
this.txtRSADecrypt.Text = "";
//
// btnRSADecrypt
//
this.btnRSADecrypt.Location = new System.Drawing.Point(11, 202);
this.btnRSADecrypt.Name = "btnRSADecrypt";
this.btnRSADecrypt.Size = new System.Drawing.Size(77, 34);
this.btnRSADecrypt.TabIndex = 7;
this.btnRSADecrypt.Text = "RSA解密";
this.btnRSADecrypt.Click += new System.EventHandler(this.btnRSADecrypt_Click);
//
// btnDeformatter
//
this.btnDeformatter.Location = new System.Drawing.Point(11, 396);
this.btnDeformatter.Name = "btnDeformatter";
this.btnDeformatter.Size = new System.Drawing.Size(77, 34);
this.btnDeformatter.TabIndex = 10;
this.btnDeformatter.Text = "RSA驗(yàn)證";
this.btnDeformatter.Click += new System.EventHandler(this.btnDeformatter_Click);
//
// btnSignature
//
this.btnSignature.Location = new System.Drawing.Point(11, 297);
this.btnSignature.Name = "btnSignature";
this.btnSignature.Size = new System.Drawing.Size(77, 34);
this.btnSignature.TabIndex = 9;
this.btnSignature.Text = "RSA簽名";
this.btnSignature.Click += new System.EventHandler(this.btnSignature_Click);
//
// txtSignature
//
this.txtSignature.Location = new System.Drawing.Point(137, 298);
this.txtSignature.Multiline = true;
this.txtSignature.Name = "txtSignature";
this.txtSignature.Size = new System.Drawing.Size(602, 44);
this.txtSignature.TabIndex = 11;
this.txtSignature.Text = "";
//
// btnGetHas
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -