欢迎光临
我们一直在努力

RSA加密解密及RSA签名和验证-.NET教程,安全和优化

建站超值云服务器,限时71元/月

此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

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » RSA加密解密及RSA签名和验证-.NET教程,安全和优化
分享到: 更多 (0)

相关推荐

  • 暂无文章