偶写的最简单的数字签名核心代码

    //准备存储加密后的值
   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("经过验证改签名无效");
   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值