博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RSA生成签名和验证签名
阅读量:6683 次
发布时间:2019-06-25

本文共 4137 字,大约阅读时间需要 13 分钟。

///     /// RSA注册码生成验证帮助类    ///     public class RSASignature    {        ///         /// 生成密匙        ///         /// 私匙        /// 公匙>        public static void GenerateKeys(out string privateKey, out string publicKey)        {            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())            {                privateKey = rsa.ToXmlString(true);                publicKey = rsa.ToXmlString(false);            }        }        ///         /// RSA签名        ///         /// 明文        /// 私匙        /// 
密文
public static string CreateSignature(string sSource, string sPrivateKey) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(sPrivateKey); RSAPKCS1SignatureFormatter sf = new RSAPKCS1SignatureFormatter(rsa); sf.SetHashAlgorithm("SHA256"); byte[] source = System.Text.ASCIIEncoding.ASCII.GetBytes(sSource); SHA256Managed sha2 = new SHA256Managed(); byte[] result = sha2.ComputeHash(source); byte[] signature = sf.CreateSignature(result); return Convert.ToBase64String(signature); } } /// /// RSA签名验证 /// /// 密文 /// 需要比较的明文字符串 /// 公匙 ///
是否相同
public static bool VerifySignature(string sEncryptSource, string sCompareString, string sPublicKey) { try { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(sPublicKey); RSAPKCS1SignatureDeformatter df = new RSAPKCS1SignatureDeformatter(rsa); df.SetHashAlgorithm("SHA256"); byte[] signature = Convert.FromBase64String(sEncryptSource); SHA256Managed sha2 = new SHA256Managed(); byte[] compareByte = sha2.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sCompareString)); return df.VerifySignature(compareByte, signature); } } catch (Exception) { return false; } } }

改进:将SHA1改为SHA256,提高安全性

测试

string publicKey = "
2DF7PLozXLLJb2vTknucTOK8+ucjYlVFOThnRwqULjeSuA2ZlReaBVXRy8PonVkXXfN2tjosy2ToTwHNX4UQP5kkEbBWYk8knOZPF/96OI2GfjGYnwNUT0CIKHda8B8ztfh1LQanNOCrrByHIuGTORL1WR2hlbaIHRrOIRkeOMc=
AQAB
"; string privateKey = "
2DF7PLozXLLJb2vTknucTOK8+ucjYlVFOThnRwqULjeSuA2ZlReaBVXRy8PonVkXXfN2tjosy2ToTwHNX4UQP5kkEbBWYk8knOZPF/96OI2GfjGYnwNUT0CIKHda8B8ztfh1LQanNOCrrByHIuGTORL1WR2hlbaIHRrOIRkeOMc=
AQAB

+WLUzPp7LeMmXaZ3Ebcw6+Ks1HA6XlkpfhLS4DOnViw2O1V/8EXFO77KwFLSwlCzfXo1LN0JuTEsacGdU3f5yw==

3e1K+J8Ns4UrbhQzgIbyGf8JX+VaETWClJSq9vEXelHGfG9/+5bbGvnF4Wo1c/Kdkuz5IQHfYbuTgMU4LpVNdQ==
bV/6IrhMo/B22CX1HNDyZNIyqK1b/BswxnltASXGY1XapyU1imSfPzfLZpQsmUqZnhOmFLoQ1KcLaoqe/IenBw==
J3CndT3XEoNlL3/5kyroVcRkZx18fzhyR3OQWqiIEp3711t9dDngIJApIaFddIi2hkkFEV+i0lyinyztiiHQjQ==
Fdk/2JOc/gj7YKsKgMEEVC0+X+WlAMhF+PJVgs188vIQSuXJTRi8Z4A214fvX49s2G81Th7KdYUWCvBAeD153Q==
wgJFkY2XIgzfb7nXt5BuByFCbfaWblwPLvZK2BnKOF9urvSlaoIvmbIV/0HjsbCNo6YkVguGv0/imWQIpTuIA6O5S1NX/dbYLp2/rSNl0/xSB5ZjX06VAYbeIsB7YWw87+35K8DrIrlZDMP4Mk6h+pawdgLTtIo/tjTpBhCfB+E=
"; private void btnCreateRegister_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtComputerInfo.Text)) { txtRegister.Text = RSASignature.CreateSignature(txtComputerInfo.Text.Trim(), privateKey); } else { MessageBox.Show("请输入机器码!"); txtComputerInfo.Focus(); } } private void button1_Click(object sender, EventArgs e) { bool result = RSASignature.VerifySignature(txtRegister.Text, txtComputerInfo.Text.Trim(), publicKey); MessageBox.Show(result.ToString()); }

 

转载地址:http://ghxao.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
酒有两不喝,财有两不发,忙有三不帮,亲有三不走!
查看>>
IPFS 服务的Python访问
查看>>
DllMain详解
查看>>
Class bytes found but defineClass()failed for error when deploying EAR
查看>>
IIS7.0安装的FTP建账号
查看>>
spring --理解
查看>>
前台中文数据后台achieveRequest().getParameter获取乱码问题
查看>>
sed工具扩展学习
查看>>
db4o 参考资料
查看>>
mysql生产环境___主从同步修复案例
查看>>
对Controller的单元测试
查看>>
人工智能无法挑战人心
查看>>
移动web 1px边框解决方案
查看>>
centos7.4 Rsync配置和触发备份
查看>>
Oracle12c 安装
查看>>
DX11之D3DXMatrixIdentity 函数
查看>>
四项重要标准 让金融机构评选合适的DDoS防护提供商
查看>>
iOS开发的插件和工具
查看>>
设置UIButton,UITextFild边框圆角(上半边或下半边)
查看>>