SummerRain

软件开发/信息安全
随笔 - 246, 文章 - 0, 评论 - 240, 阅读 - 36万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

基于RSA的数字签名和验证C#源码

Posted on   SummerRain  阅读(3327)  评论(2编辑  收藏  举报

/* 基于RSA的数字签名和验证C#源码
 * (采用字符串作为参数)RSA_Demo3
 * 
 * 夏春涛 Email:xChuntao@163.com 
 * Blog:
http://bluesky521.cnblogs.com
 * 运行环境:.net2.0 framework
 
*/


/* 备注:
 * 若要使用公钥系统对消息进行数字签名,发送方先向该消息应用哈希函数以创建消息摘要。
 * 然后,发送方用自己的私钥对消息摘要进行加密,以创建发送方的个人签名。在收到消息和
 * 签名后,接收方使用发送方的公钥解密该签名,以恢复消息摘要,并使用发送方所用的同一
 * 哈希算法对该消息进行哈希运算。如果接收方计算的消息摘要与从发送方收到的消息摘要完
 * 全匹配,则接收方可以假定消息在传输中未被更改。请注意,因为发送方的公钥是公共知识,
 * 所以任何人都可以验证签名。
 
*/


using System;
using System.Security.Cryptography;
using System.Text;

class RSACSPSample
{
    
static void Main()
    
{
        
try
        
{

            
string str_DataToSign = @"Data to Sign!Data to Sign!Data to Sign!";
            Console.WriteLine(
"原文:" + str_DataToSign);
            Console.WriteLine(
"长度:" + str_DataToSign.Length.ToString());
            Console.WriteLine();

            RSACryptoServiceProvider RSAalg 
= new RSACryptoServiceProvider();

            
string str_Private_Key = Convert.ToBase64String( RSAalg.ExportCspBlob(true) );
            
string str_Public_Key = Convert.ToBase64String( RSAalg.ExportCspBlob(false) );
            Console.WriteLine(
"公钥:" + str_Public_Key);
            Console.WriteLine();
            Console.WriteLine(
"私钥:" + str_Private_Key);
            Console.WriteLine();

            
string str_SignedData = HashAndSign(str_DataToSign, str_Private_Key);// Hash and sign the data.
            Console.WriteLine("签名数据:" + str_SignedData);
            Console.WriteLine();

            
if (VerifySignedHash(str_DataToSign, str_SignedData, str_Public_Key))
            
{
                Console.WriteLine(
"验证签名OK.");
            }

            
else
            
{
                Console.WriteLine(
"签名不匹配!");
            }

            Console.WriteLine();

        }

        
catch (ArgumentNullException)
        
{
            Console.WriteLine(
"The data was not signed or verified");

        }

    }


    
//对数据签名
    public static string HashAndSign(string str_DataToSign, string str_Private_Key)
    
{
        ASCIIEncoding ByteConverter 
= new ASCIIEncoding();
        
byte[] DataToSign = ByteConverter.GetBytes(str_DataToSign);
        
try
        
{
            RSACryptoServiceProvider RSAalg 
= new RSACryptoServiceProvider();
            RSAalg.ImportCspBlob( Convert.FromBase64String(str_Private_Key) );
            
byte[] signedData = RSAalg.SignData(DataToSign, new SHA1CryptoServiceProvider());
            
string str_SignedData = Convert.ToBase64String(signedData);
            
return str_SignedData;
        }

        
catch (CryptographicException e)
        
{
            Console.WriteLine(e.Message);
            
return null;
        }

    }


    
//验证签名
    public static bool VerifySignedHash(string str_DataToVerify, string str_SignedData, string str_Public_Key)
    
{
        
byte[] SignedData = Convert.FromBase64String(str_SignedData);

        ASCIIEncoding ByteConverter 
= new ASCIIEncoding();
        
byte[] DataToVerify = ByteConverter.GetBytes(str_DataToVerify);
        
try
        
{
            RSACryptoServiceProvider RSAalg 
= new RSACryptoServiceProvider();
            RSAalg.ImportCspBlob( Convert.FromBase64String(str_Public_Key) );

            
return RSAalg.VerifyData(DataToVerify, new SHA1CryptoServiceProvider(), SignedData);

        }

        
catch (CryptographicException e)
        
{
            Console.WriteLine(e.Message);

            
return false;
        }

    }

}

附件:/Files/bluesky521/RSA_Demo.rar

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示