加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2012-02-28 18:23:24)
标签:

杂谈

分类: C#

http://blog.163.com/Images/OutliningIndicators/ExpandedBlockStart.gif

http://blog.163.com/Images/OutliningIndicators/None.gif
http://blog.163.com/Images/OutliningIndicators/ExpandedBlockStart.gif

http://blog.163.com/Images/OutliningIndicators/None.gif
http://blog.163.com/Images/OutliningIndicators/None.gif
using System;
http://blog.163.com/Images/OutliningIndicators/None.gif
using System.Security.Cryptography;
http://blog.163.com/Images/OutliningIndicators/None.gif
using System.Text;
http://blog.163.com/Images/OutliningIndicators/None.gif
http://blog.163.com/Images/OutliningIndicators/None.gif
class RSACSPSample
http://blog.163.com/Images/OutliningIndicators/ExpandedBlockStart.gif
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif    
static void Main()
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif        
try
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
string str_DataToSign = @"Data to Sign!Data to Sign!Data to Sign!";
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine(
"原文:" + str_DataToSign);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine(
"长度:" + str_DataToSign.Length.ToString());
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine();
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            RSACryptoServiceProvider RSAalg 
= new RSACryptoServiceProvider();
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
string str_Private_Key = Convert.ToBase64String( RSAalg.ExportCspBlob(true);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
string str_Public_Key = Convert.ToBase64String( RSAalg.ExportCspBlob(false);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine(
"公钥:" + str_Public_Key);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine();
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine(
"私钥:" + str_Private_Key);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine();
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
string str_SignedData = HashAndSign(str_DataToSign, str_Private_Key);// Hash and sign the data.
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
            Console.WriteLine("签名数据:" + str_SignedData);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine();
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
if (VerifySignedHash(str_DataToSign, str_SignedData, str_Public_Key))
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif                Console.WriteLine(
"验证签名OK.");
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif            }

http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
else
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif                Console.WriteLine(
"签名不匹配!");
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif            }

http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine();
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://blog.163.com/Images/OutliningIndicators/InBlock.gif        
catch (ArgumentNullException)
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine(
"The data was not signed or verified");
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }

http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/InBlock.gif    
//对数据签名
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
    public static string HashAndSign(string str_DataToSign, string str_Private_Key)
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif        ASCIIEncoding ByteConverter 
= new ASCIIEncoding();
http://blog.163.com/Images/OutliningIndicators/InBlock.gif        
byte[] DataToSign = ByteConverter.GetBytes(str_DataToSign);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif        
try
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            RSACryptoServiceProvider RSAalg 
= new RSACryptoServiceProvider();
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            RSAalg.ImportCspBlob( Convert.FromBase64String(str_Private_Key) );
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
byte[] signedData = RSAalg.SignData(DataToSign, new SHA1CryptoServiceProvider());
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
string str_SignedData = Convert.ToBase64String(signedData);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
return str_SignedData;
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://blog.163.com/Images/OutliningIndicators/InBlock.gif        
catch (CryptographicException e)
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine(e.Message);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
return null;
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }

http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/InBlock.gif    
//验证签名
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
    public static bool VerifySignedHash(string str_DataToVerify, string str_SignedData, string str_Public_Key)
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif        
byte[] SignedData = Convert.FromBase64String(str_SignedData);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/InBlock.gif        ASCIIEncoding ByteConverter 
= new ASCIIEncoding();
http://blog.163.com/Images/OutliningIndicators/InBlock.gif        
byte[] DataToVerify = ByteConverter.GetBytes(str_DataToVerify);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif        
try
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            RSACryptoServiceProvider RSAalg 
= new RSACryptoServiceProvider();
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            RSAalg.ImportCspBlob( Convert.FromBase64String(str_Public_Key) );
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
return RSAalg.VerifyData(DataToVerify, new SHA1CryptoServiceProvider(), SignedData);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://blog.163.com/Images/OutliningIndicators/InBlock.gif        
catch (CryptographicException e)
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
{
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            Console.WriteLine(e.Message);
http://blog.163.com/Images/OutliningIndicators/InBlock.gif
http://blog.163.com/Images/OutliningIndicators/InBlock.gif            
return false;
http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://blog.163.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }

http://blog.163.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有