RSA加密解密及RSA签名和验证

加密|解密

此Demo包含两个文件,建立一个解决方案,然后建立两个文件,一个为Form,一个为Class,把代码分别复制进去即可

RSA正确的执行过程:
加密解密:
1、获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥
2、加密
3、解密
签名和验证:
签名:
1、获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥
2、获取待签名的Hash码
3、签名
其中,1和2的步骤无所谓,在本例中,我们将对txtSource里的内容进行签名,也可以对文件进行签名
验证签名:
1、获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥
2、获取待验证签名的Hash码
3、获取签名的字串,这里签名的字串存储在m_strEncryptedSignatureData变量中,在DEMO中必须通过签名才能获得这个字串,因此需要先执行签名,当然也可以更改之后通过别的方式获得
4、验证
其中,1和2的步骤无所谓,在本例中,我们将对txtSource里的内容进行签名验证,也可以对文件进行签名验证
如果是文件,取得文件之后把文件的内容以byte[]的方式代入即可
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//RSACryption.cs
///////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Text;
using System.Security.Cryptography;

namespace RSAApplication
{
/// <summary>
/// RSACryption 的摘要说明。
/// </summary>
public class RSACryption
{
#region 构造函数

public RSACryption()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion

#region RSA 加密解密

#region RSA 的密钥产生
//RSA 的密钥产生
//产生私钥 和公钥
public void RSAKey(out string xmlKeys,out string xmlPublicKey)
{
try
{
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
xmlKeys=rsa.ToXmlString(true);
xmlPublicKey = rsa.ToXmlString(false);
}
catch(Exception ex)
{
throw ex;
}
}
#endregion
#region RSA的加密函数
//##############################################################################
//RSA 方式加密
//说明KEY必须是XML的行式,返回的是字符串
//在有一点需要说明!!该加密方式有 长度 限制的!!
//##############################################################################

//RSA的加密函数
public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString )
{
try
{
byte[] PlainTextBArray;
byte[] CypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
Result=Convert.ToBase64String(CypherTextBArray);
return Result;
}
catch(Exception ex)
{
throw ex;
}
}
//RSA的加密函数
public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString )
{
try
{
byte[] CypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
CypherTextBArray = rsa.Encrypt(EncryptString, false);
Result=Convert.ToBase64String(CypherTextBArray);
return Result;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion

#region RSA的解密函数
//RSA的解密函数
public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString )
{
try
{
byte[] PlainTextBArray;
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
PlainTextBArray =Convert.FromBase64String(m_strDecryptString);
DypherTextBArray=rsa.Decrypt(PlainTextBArray, false);
Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
catch(Exception ex)
{
throw ex;
}
}

//RSA的解密函数
public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString )
{
try
{
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
DypherTextBArray=rsa.Decrypt(DecryptString, false);
Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion

#endregion

#region RSA数字签名

#region 获取Hash描述表
//获取Hash描述表
public bool GetHash(string m_strSource, ref byte[] HashData)
{
try
{
//从字符串中取得Hash描述
byte[] Buffer;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
HashData = MD5.ComputeHash(Buffer);

return true;
}
catch(Exception ex)
{
throw ex;
}
}

//获取Hash描述表
public bool GetHash(string m_strSource, ref string strHashData)
{
try
{
//从字符串中取得Hash描述
byte[] Buffer;
byte[] HashData;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
HashData = MD5.ComputeHash(Buffer);

strHashData = Convert.ToBase64String(HashData);
return true;
}
catch(Exception ex)
{
throw ex;
}
}

//获取Hash描述表
public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)
{
try
{
//从文件中取得Hash描述
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
HashData = MD5.ComputeHash(objFile);
objFile.Close();

return true;
}
catch(Exception ex)
{
throw ex;
}
}

//获取Hash描述表
public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
{
try
{
//从文件中取得Hash描述
byte[] HashData;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
HashData = MD5.ComputeHash(objFile);
objFile.Close();

strHashData = Convert.ToBase64String(HashData);

return true;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion

#region RSA签名
//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
{
try
{
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

return true;
}
catch(Exception ex)
{
throw ex;
}
}

//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData)
{
try
{
byte[] EncryptedSignatureData;

System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);

return true;
}
catch(Exception ex)
{
throw ex;
}
}

//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData)
{
try
{
byte[] HashbyteSignature;

HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

return true;
}
catch(Exception ex)
{
throw ex;
}
}

//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData)
{
try
{
byte[] HashbyteSignature;
byte[] EncryptedSignatureData;

HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);

return true;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion

#region RSA 签名验证

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);
//指定解密的时候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);
//指定解密的时候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);
//指定解密的时候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);
//指定解密的时候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 的摘要说明。
/// </summary>
public class frmRSACryptionTest : System.Windows.Forms.Form
{
#region 必需的设计器变量
/// <summary>
/// 必需的设计器变量
/// </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 构造函数
public frmRSACryptionTest()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </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 = "产生密钥";
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 = "字串不能太长j——km,.ewm.m, .vkj中国福建";
//
// 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验证";
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 = "";
//
// btnGetHashSignature
//
this.btnGetHashSignature.Location = new System.Drawing.Point(11, 252);
this.btnGetHashSignature.Name = "btnGetHashSignature";
this.btnGetHashSignature.Size = new System.Drawing.Size(117, 36);
this.btnGetHashSignature.TabIndex = 13;
this.btnGetHashSignature.Text = "获取哈稀码(签名)";
this.btnGetHashSignature.Click += new System.EventHandler(this.btnGetHashSignature_Click);
//
// btnGetHashDeformatter
//
this.btnGetHashDeformatter.Location = new System.Drawing.Point(11, 348);
this.btnGetHashDeformatter.Name = "btnGetHashDeformatter";
this.btnGetHashDeformatter.Size = new System.Drawing.Size(117, 36);
this.btnGetHashDeformatter.TabIndex = 14;
this.btnGetHashDeformatter.Text = "获取哈稀码(验证)";
this.btnGetHashDeformatter.Click += new System.EventHandler(this.btnGetHashDeformatter_Click);
//
// txtGetHashSignature
//
this.txtGetHashSignature.Location = new System.Drawing.Point(137, 251);
this.txtGetHashSignature.Multiline = true;
this.txtGetHashSignature.Name = "txtGetHashSignature";
this.txtGetHashSignature.Size = new System.Drawing.Size(602, 44);
this.txtGetHashSignature.TabIndex = 15;
this.txtGetHashSignature.Text = "";
//
// txtGetHashDeformatter
//
this.txtGetHashDeformatter.Location = new System.Drawing.Point(137, 346);
this.txtGetHashDeformatter.Multiline = true;
this.txtGetHashDeformatter.Name = "txtGetHashDeformatter";
this.txtGetHashDeformatter.Size = new System.Drawing.Size(602, 44);
this.txtGetHashDeformatter.TabIndex = 16;
this.txtGetHashDeformatter.Text = "";
//
// frmRSACryptionTest
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(764, 444);
this.Controls.Add(this.txtGetHashDeformatter);
this.Controls.Add(this.txtGetHashSignature);
this.Controls.Add(this.txtSignature);
this.Controls.Add(this.txtRSADecrypt);
this.Controls.Add(this.txtRSAEncrypt);
this.Controls.Add(this.txtSource);
this.Controls.Add(this.txtKeyPrivate);
this.Controls.Add(this.txtKeyPublic);
this.Controls.Add(this.btnGetHashDeformatter);
this.Controls.Add(this.btnGetHashSignature);
this.Controls.Add(this.btnDeformatter);
this.Controls.Add(this.btnSignature);
this.Controls.Add(this.btnRSADecrypt);
this.Controls.Add(this.btnRSAEncrypt);
this.Controls.Add(this.btnBuildKey);
this.Name = "frmRSACryptionTest";
this.Text = "RSA加密解密";
this.ResumeLayout(false);

}
#endregion

#region 应用程序的主入口点
/// <summary>
/// 应用程序的主入口点
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmRSACryptionTest());
}
#endregion

#region 产生密钥

private void btnBuildKey_Click(object sender, System.EventArgs e)
{

try
{
RSACryption RC = new RSACryption();

RC.RSAKey(out m_strKeyPrivate, out m_strKeyPublic);

this.txtKeyPrivate.Text = m_strKeyPrivate;
this.txtKeyPublic.Text = m_strKeyPublic;
}
catch(Exception ex)
{
MessageBox.Show(this,ex.Message,"错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
}
}
#endregion

#region 加密解密

private void btnRSAEncrypt_Click(object sender, System.EventArgs e)
{
try
{
RSACryption RC = new RSACryption();

this.txtRSAEncrypt.Text = RC.RSAEncrypt(m_strKeyPublic, this.txtSource.Text);
}
catch(Exception ex)
{
MessageBox.Show(this,ex.Message,"错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
}
}

private void btnRSADecrypt_Click(object sender, System.EventArgs e)
{
try
{
RSACryption RC = new RSACryption();

this.txtRSADecrypt.Text = RC.RSADecrypt(m_strKeyPrivate, this.txtRSAEncrypt.Text);
}
catch(Exception ex)
{
MessageBox.Show(this,ex.Message,"错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
}
}

#endregion

#region 签名、验证

#region 获取Hash码---针对签名

private void btnGetHashSignature_Click(object sender, System.EventArgs e)
{
try
{
RSACryption RC = new RSACryption();

if( RC.GetHash(this.txtSource.Text,ref m_strHashbyteSignature) == false)
{
MessageBox.Show(this,"取Hash码错误!","提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
}

this.txtGetHashSignature.Text = m_strHashbyteSignature;
}
catch(Exception ex)
{
MessageBox.Show(this,ex.Message,"错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
}
}
#endregion

#region 签名
private void btnSignature_Click(object sender, System.EventArgs e)
{
try
{
RSACryption RC = new RSACryption();

if( RC.SignatureFormatter(m_strKeyPrivate,m_strHashbyteSignature, ref m_strEncryptedSignatureData) == false)
{
MessageBox.Show(this,"RSA数字签名错误!","提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
}

this.txtSignature.Text = m_strEncryptedSignatureData;
}
catch(Exception ex)
{
MessageBox.Show(this,ex.Message,"错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
}
}
#endregion

#region 获取Hash码---针对验证
private void btnGetHashDeformatter_Click(object sender, System.EventArgs e)
{
try
{
RSACryption RC = new RSACryption();

if( RC.GetHash(this.txtSource.Text,ref m_strHashbyteDeformatter) == false)
{
MessageBox.Show(this,"取Hash码错误!","提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
}

this.txtGetHashDeformatter.Text = m_strHashbyteDeformatter;
}
catch(Exception ex)
{
MessageBox.Show(this,ex.Message,"错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
}
}
#endregion

#region 验证
private void btnDeformatter_Click(object sender, System.EventArgs e)
{
try
{
RSACryption RC = new RSACryption();

if( RC.SignatureDeformatter(m_strKeyPublic,m_strHashbyteDeformatter, m_strEncryptedSignatureData) == false)
{
MessageBox.Show(this,"身份验证失败!","提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
}
else
{
MessageBox.Show(this,"身份验证通过!","提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
}
}
catch(Exception ex)
{
MessageBox.Show(this,ex.Message,"错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
}
}
#endregion

#endregion

}
}

时间: 2024-10-02 20:55:07

RSA加密解密及RSA签名和验证的相关文章

Golang加密解密之RSA(附带php)_Golang

RSA加密算法简史 RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA就是他们三人姓氏开头字母拼在一起组成的. RSA加密算法原理 学过算法的朋友都知道,计算机中的算法其实就是数学运算.所以,再讲解RSA加密算法之前,有必要了解一下一些必备的数学知识.我们就从数学知识开始讲解. 必备数学知识 RSA加密算法中,只用到素数.互质数.指数运算.模运算等

PHP rsa加密解密使用方法_php实例

php服务端与客户端交互.提供开放api时,通常需要对敏感的部分api数据传输进行数据加密,这时候rsa非对称加密就能派上用处了,下面通过一个例子来说明如何用php来实现数据的加密解密 1.加密解密的第一步是生成公钥.私钥对,私钥加密的内容能通过公钥解密(反过来亦可以) 下载开源RSA密钥生成工具openssl(通常Linux系统都自带该程序),解压缩至独立的文件夹,进入其中的bin目录,执行以下命令: openssl genrsa -out rsa_private_key.pem 1024 o

刚开始学OPENSSL , 请问rsa加密解密和验证签名有关系吗? 为什么需要验证签名。

问题描述 刚开始学OPENSSL , 请问rsa加密解密和验证签名有关系吗? 为什么需要验证签名. 我只想知道具体的工作过程,不必知道里面的算法是怎么实现的. 知道怎么去用就可以了,感觉自己越看与糊涂了. 求助 谢谢 解决方案 因为RSA算法是用的公钥,私钥机制来进行加解密,而私钥另一个用途就是验证身份,所以RSA加密本身也能用在身份验证,数字签名等场合. 这相当于RSA的多个用途 解决方案二: 公钥和私钥是一对的,这对密钥由个人向认证机构申请,最后由认证机构颁发私钥给你并在其网站上公布你的公钥

iOS在RSA加密解密和签名的时候已经生成密钥、摘要和密文。向服务器请求为何后台没有反应

问题描述 iOS在RSA加密解密和签名的时候已经生成密钥.摘要和密文.向服务器请求为何后台没有反应 是RSA加密签名这里的问题吗 解决方案 先要看后台有没有收到数据,然后就是后台是否正确处理

JAVA加密解密之RSA算法

RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.1987年首次公布,当时他们三人都在麻省理工学院工作.RSA就是他们三人姓氏开头字母拼在一起组成的. RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的绝大多数密码攻击,已被ISO推荐为公钥数据加密标准. 今天只有短的RSA钥匙才可能被强力方式解破.到2008年为止,世界上还没有任何可靠的攻击RSA算法的方式.只

C# Java间进行RSA加密解密交互

原文:C# Java间进行RSA加密解密交互 这里,讲一下RSA算法加解密在C#和Java之间交互的问题,这两天纠结了很久,也看了很多其他人写的文章,颇受裨益,但没能解决我的实际问题,终于,还是被我捣鼓出来了. 首先,介绍一下写这代码的目的:完成webService验证问题,服务器端采用C#开发,客户端采用Java开发.服务器端给客户端提供公钥,已进行数据加密,客户端加密后提数据提交给服务器,服务器用私钥对数据解密,进行验证.  这里遇到的主要问题是C# RSACryptoServiceProv

RSA加密解密(附源码工程)

版权声明:本文为博主原创文章,转载注明出处http://blog.csdn.net/u013142781 目录(?)[+] 一.RSA加密介绍 RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.1987年首次公布,当时他们三人都在麻省理工学院工作.RSA就是他们三人姓氏开头字母拼在一起组成的. RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的绝大多数密码攻击,

PHP rsa加密解密使用方法

  PHP rsa加密解密使用方法        这篇文章主要介绍了PHP rsa加密解密使用方法,本文讲解了生成公钥.私钥及PHP中使用生成的公钥.私钥进行加密解密实例,需要的朋友可以参考下 php服务端与客户端交互.提供开放api时,通常需要对敏感的部分api数据传输进行数据加密,这时候rsa非对称加密就能派上用处了,下面通过一个例子来说明如何用php来实现数据的加密解密 1.加密解密的第一步是生成公钥.私钥对,私钥加密的内容能通过公钥解密(反过来亦可以) 下载开源RSA密钥生成工具open

mvc-Mvc使用RSA加密解密出现问题

问题描述 Mvc使用RSA加密解密出现问题 mvc中,cshtml前端的JavaScript中RSA加密,后台controller的action进行解密,我使用IE浏览器进行RSA加密解密是没问题的,到那时我使用google浏览器进行RSA加密解密就出问题了,报错:数据不正确,请问哪位大神知道为什么呀?不吝赐教,谢谢了