zoukankan
html css js c++ java
基于RSA的数字签名和验证C#源码
/**/
/*
基于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
查看全文
相关阅读:
发布google在线翻译程序(附源码)
基于MVP架构设计ASP.Net的应用研究
发布最新C#3.5开发的ReSharper4.0 for VS2005/2008 注册机
基于元数据驱动模型架构在ASP.Net的应用研究
Silverlight整合Asp.net AjAX的技术应用
在WCF中的异常处理方法
Windows Server 2008 的十四大最新功能特性技术总结
微软Asp.Net架构与项目团队管理建设模型分析
在Biztalk应用中调用程序集的方法
Visual Studio 2008和ASP.NET 3.5的最新技术探索
原文地址:https://www.cnblogs.com/SummerRain/p/1011991.html
最新文章
[日志]小沈阳搞笑经典语录
Android学习笔记39:Android四大组件之Service
Android学习笔记42:XML文件解析(DOM方式)
Android学习笔记37:使用Content Providers方式共享数据
Android学习笔记40:Android四大组件之BroadcastReceiver
Android学习笔记34:使用文件存储数据
清明小感
Android学习笔记35:使用Shared Preferences方式存储数据
Android学习笔记41:XML文件解析(SAX方式)
Android学习笔记38:Android四大组件之Activity
热门文章
Android学习笔记36:使用SQLite方式存储数据
发布CodeBuild.Net代码自动生成器 V2008 2.01(Vs2008)和架构实例源码Demo
北京俱乐部2008.04.12活动,“微软新技术与架构设计应用"PPT
基于Web Services建立Asp与Asp.Net之间Session数据桥的应用研究
使用LINQ解除SQL注入安全问题
北京.Net俱乐部活动在北京邮电大学讲座(已结束)
基于微软Synchronization Services双向同步技术在企业项目中的架构应用研究
如何在VS2008中的Language Integrated Query动态条件查询
开发Office Communicator 2007 在企业里的扩展应用
基于微软Office Communicator 2007 Automation API开发应用
Copyright © 2011-2022 走看看