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
查看全文
相关阅读:
javascript循环结构练习
个人复习JavaScript重点(总结一)
第十章汽车租凭系统
员工执行
第六章预习
第五章使用Dictionary替换List<t>实现功能
第5章体检套餐管理系统
JAVA面试题
AJAX
SSM中的登陆验证码
原文地址:https://www.cnblogs.com/SummerRain/p/1011991.html
最新文章
Linux C代码 获取IP地址
Ubuntu禁用Compiz
通过Qt从URL下载文件
QWebView使用
梦断代码阅读笔记之三
梦断代码阅读笔记之二
梦断代码阅读笔记之一
你的灯还亮着吗阅读笔记之三
站立会议总结03
站立会议总结02
热门文章
站立会议总结01
蹭课神器NABCD分析
二维最大和子数组
周六周日收获
听指令的小方块
移动web学习笔记
不用js实现无缝滚动---marquee
p中div -- a中a
解决chrome浏览器input背景色偏黄的问题
元素定位后如何居中
Copyright © 2011-2022 走看看