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
查看全文
相关阅读:
Java学习01-使用maven插件tomcat搭建web maven项目
Anaconda3安装及使用
python标准日志模块logging及日志系统设计
python traceback捕获并打印异常
python time
PIL 中的 Image 模块
python zipfile使用
PHP 判断用户是否手机访问
php数组相加 两个数组键名相同 后者不能覆盖前者
解决Ubuntu Server 12.04换了网卡MAC地址后 网络不可用的问题.
原文地址:https://www.cnblogs.com/SummerRain/p/1011990.html
最新文章
kindeditor集成ckplayer(带右键编辑菜单)
java正则表达式获取指定HTML标签的指定属性值
表单字段长度限制
在java中和javascript中过滤掉类似于img形式的字符串,从而不显示图片
Java ZIP压缩和解压缩文件并兼容linux
service引用
注册表检测office版本
利用caffe的solverstate断点训练
caffe中的Accuracy+softmaxWithLoss
train val test区别
热门文章
训练自己的数据遇到的问题
机器学习中的范数规则化之(二)核范数与规则项参数选择
机器学习中的范数规则化之(一)L0、L1与L2范数
caffe数据集——LMDB
正则表达式
python代码格式规范
如何用好 github 中的 watch、star、fork
PHP漏洞函数
Java学习03-进制学习
Java学习02-web.xml配置详解
Copyright © 2011-2022 走看看