//准备存储加密后的值
byte[] SignedHashValue;
RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
RSAPKCS1SignatureFormatter rsaFormatter=new RSAPKCS1SignatureFormatter(rsa);
rsaFormatter.SetHashAlgorithm("SHA1");
//导出公钥和私钥
myPublic=rsa.ExportParameters(true);
myPrivate=rsa.ExportParameters(true);
//执行生成数字签名
SignedHashValue=rsaFormatter.CreateSignature(HashValue);
//创建新的RSA对象
RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();
//导入发布的公钥
RSAParameters rasKeyInfo=new RSAParameters();
rasKeyInfo.Modulus = myPublic.Modulus;
rasKeyInfo.Exponent = myPublic.Exponent;
RSA2.ImportParameters(rasKeyInfo);
//使用公钥进行解密
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA2);
//指定原加密算法
RSADeformatter.SetHashAlgorithm("SHA1");
//进行比较 判断签名是否有效
if(RSADeformatter.VerifySignature(HashValue, SignedHashValue))
{
Response.Write("经过验证改签名有效");
}
else
{
Response.Write("经过验证改签名无效");
}
上面这个是 先用自己的私钥加密后发送给对方 对方使用公钥进行解密
下面这个是 对方向我发文件 先用公钥加密后发送给我 我使用私钥进行解密 完成数字签名的校验
转载自由 LVLV
RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
RSAPKCS1SignatureFormatter rsaFormatter=new RSAPKCS1SignatureFormatter(rsa);
rsaFormatter.SetHashAlgorithm("SHA1");
//导出公钥和私钥
myPublic=rsa.ExportParameters(false);
myPrivate=rsa.ExportParameters(true);
//执行生成数字签名
SignedHashValue=rsaFormatter.CreateSignature(HashValue);
//创建新的RSA对象
RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();
//导入发布的公钥
RSAParameters rasKeyInfo=new RSAParameters();
rasKeyInfo.Modulus = myPublic.Modulus;
rasKeyInfo.Exponent = myPublic.Exponent;
RSA2.ImportParameters(rasKeyInfo);
//使用公钥进行解密
RSAPKCS1SignatureFormatter RSAformatter = new RSAPKCS1SignatureFormatter(RSA2);
//指定原加密算法
RSAformatter.SetHashAlgorithm("SHA1");
RSACryptoServiceProvider rsa3=new RSACryptoServiceProvider();
RSAParameters rasKeyInfo2=new RSAParameters();
rasKeyInfo2.Modulus = myPrivate.Modulus;
rasKeyInfo2.Exponent = myPrivate.Exponent;
rsa3.ImportParameters(rasKeyInfo2);
RSAPKCS1SignatureDeformatter rsade=new RSAPKCS1SignatureDeformatter(rsa3);
rsade.SetHashAlgorithm("SHA1");
//进行比较 判断签名是否有效
if(rsade.VerifySignature(HashValue, SignedHashValue))
{
Response.Write("经过验证改签名有效");
}
else
{
Response.Write("经过验证改签名无效");
}