C# RSA
2013-11-18 14:54
162 查看
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;
}
}
}
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;
}
}
}
相关文章推荐
- C#线程通信与异步委托
- C# 导出EXCEL报错
- C#做端口转发程序支持正向连接和反向链接
- C# Winform 使用Adobe Acrobat 实现 PDF 转 图片
- C#中enum的总结
- C# 窗体渐变Show出来
- C# TextBox智能提示
- 聊聊可空类型
- c#中如何跨线程调用windows窗体控件
- C#线程使用学习
- C#DB相关收集
- C# 调用颜色的RGB值_RGB颜色转换十六进制颜色
- C# 计算时间间隔的方法
- C#示例代码
- 插件技术原理及其C#实现(一)插件技术原理及其特性
- C# 线程
- C# 在datagridview中的单元格中加入button按钮
- 未安装C#的Visual Studio语言支持,代码编辑Intellisense将不可用
- C# partial 说明
- C# using作用透析 三种用法