您的位置:首页 > 编程语言 > C#

【C#】RSA 密钥生成 加密 解密

2016-06-28 16:52 579 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Cryptography;

namespace RSA
{
class Program
{
public static string privatekey;
public static string publickey;
static void Main(string[] args)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
privatekey = rsa.ToXmlString(true);
publickey = rsa.ToXmlString(false);
string content = "hello cxc";
string s = RSAEncrypt(content);
string c = RSADecrypt(s);
}

public static string RSAEncrypt(string content)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(publickey);
cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
return Convert.ToBase64String(cipherbytes);
}
public static string RSADecrypt(string s)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(privatekey);
cipherbytes = rsa.Decrypt(Convert.FromBase64String(s), false);
return Encoding.UTF8.GetString(cipherbytes);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# rsa 加密 解密