zoukankan
html css js c++ java
基于RSA的加密/解密示例C#代码
/**/
/*
* 基于RSA的加密/解密示例C#代码
* (采用字符串作为参数)RSA_Demo2
*
* 夏春涛 Email:xChuntao@163.com
* Blog:
http://bluesky521.cnblogs.com
* 运行环境:.net2.0 framework
*
* 备注:
* 不对称算法通常用于加密少量数据,如加密对称密钥和 IV。通常,
* 执行不对称加密的个人使用由另一方生成的公钥。.NET Framework
* 为此目的而提供了 RSACryptoServiceProvider 类。
*/
using
System;
using
System.Security.Cryptography;
using
System.Text;
class
RSACSPSample
{
static
void
Main()
{
try
{
string
str_Plain_Text
=
"
How are you?How are you?How are you?How are you?=-popopolA
"
;
Console.WriteLine(
"
明文:
"
+
str_Plain_Text);
Console.WriteLine(
"
长度:
"
+
str_Plain_Text.Length.ToString());
Console.WriteLine();
RSACryptoServiceProvider RSA
=
new
RSACryptoServiceProvider();
string
str_Public_Key;
string
str_Private_Key;
string
str_Cypher_Text
=
RSA_Encrypt(str_Plain_Text,
out
str_Public_Key,
out
str_Private_Key);
Console.WriteLine(
"
密文:
"
+
str_Cypher_Text);
Console.WriteLine(
"
公钥:
"
+
str_Public_Key);
Console.WriteLine(
"
私钥:
"
+
str_Private_Key);
string
str_Plain_Text2
=
RSA_Decrypt(str_Cypher_Text, str_Private_Key);
Console.WriteLine(
"
解密:
"
+
str_Plain_Text2);
Console.WriteLine();
}
catch
(ArgumentNullException)
{
Console.WriteLine(
"
Encryption failed.
"
);
}
}
//
RSA加密,随机生成公私钥对并作为出参返回
static
public
string
RSA_Encrypt(
string
str_Plain_Text,
out
string
str_Public_Key,
out
string
str_Private_Key)
{
str_Public_Key
=
""
;
str_Private_Key
=
""
;
UnicodeEncoding ByteConverter
=
new
UnicodeEncoding();
byte
[] DataToEncrypt
=
ByteConverter.GetBytes(str_Plain_Text);
try
{
RSACryptoServiceProvider RSA
=
new
RSACryptoServiceProvider();
str_Public_Key
=
Convert.ToBase64String(RSA.ExportCspBlob(
false
));
str_Private_Key
=
Convert.ToBase64String(RSA.ExportCspBlob(
true
));
//
OAEP padding is only available on Microsoft Windows XP or later.
byte
[] bytes_Cypher_Text
=
RSA.Encrypt(DataToEncrypt,
false
);
str_Public_Key
=
Convert.ToBase64String(RSA.ExportCspBlob(
false
));
str_Private_Key
=
Convert.ToBase64String(RSA.ExportCspBlob(
true
));
string
str_Cypher_Text
=
Convert.ToBase64String(bytes_Cypher_Text);
return
str_Cypher_Text;
}
catch
(CryptographicException e)
{
Console.WriteLine(e.Message);
return
null
;
}
}
//
RSA解密
static
public
string
RSA_Decrypt(
string
str_Cypher_Text,
string
str_Private_Key)
{
byte
[] DataToDecrypt
=
Convert.FromBase64String(str_Cypher_Text);
try
{
RSACryptoServiceProvider RSA
=
new
RSACryptoServiceProvider();
//
RSA.ImportParameters(RSAKeyInfo);
byte
[] bytes_Public_Key
=
Convert.FromBase64String(str_Private_Key);
RSA.ImportCspBlob(bytes_Public_Key);
//
OAEP padding is only available on Microsoft Windows XP or later.
byte
[] bytes_Plain_Text
=
RSA.Decrypt(DataToDecrypt,
false
);
UnicodeEncoding ByteConverter
=
new
UnicodeEncoding();
string
str_Plain_Text
=
ByteConverter.GetString(bytes_Plain_Text);
return
str_Plain_Text;
}
catch
(CryptographicException e)
{
Console.WriteLine(e.ToString());
return
null
;
}
}
}
备注:经测试明文长度超过58时会出现异常.
源码附件:
/Files/bluesky521/RSA_Demo.rar
查看全文
相关阅读:
使用NPOI读取Excel表格内容并进行修改
JSON.parse()和JSON.stringify()
切图时图片的选择:JPG、PNG、GIF的区别
用js把数据从一个页面传到另一个页面
iframe中positioin:fixed失效问题
YUV格式&像素
JS中插入节点的方法appendChild和insertBefore的应用
CSS的相对定位和就对定位
CSS中的相对定位和绝对定位
POJ-1734
原文地址:https://www.cnblogs.com/SummerRain/p/1011990.html
最新文章
Eclipse+Python环境配置
Tomcat的部署+第一个Servlet
mysql保存emoji表情(微信开发用户昵称..)
sql语句中查询出的数据添加一列,并且添加默认值
mybatis在xml文件中处理大于号小于号的方法
关于mybatis插入数据库返回主键id
数据库的一些定义
mybatis参数查询
苦逼的男人
回顾我四年的开发经历,总结心得之公元前
热门文章
package.json
搭建GitLab+Jenkins
跨年操作--new Date()
js中将类数组转换为数组的几种方法
AngularJS 中{{}}与ng-bind指令
使用CodeSmith编写ADO.Net三层
使用aspnet_compiler对web程序进行预编译
Barcodex帮助文档
winform使用Barcodex控件预览和打印一维码
使用NPOI读取Word文档内容并进行修改
Copyright © 2011-2022 走看看