欢迎光临

木子屋

标题: C#使用RSA加密字符串示例

作者: dnawo

日期: 2013-04-20 01:33 PM

分类: Win编程

内容: RSA加/解密类EncryptHelpr：
复制内容到剪贴板 程序代码using System;
using System.Security.Cryptography;
using System.Text;
namespace Library
{
public class EncryptHelpr
{
private static readonly string publickey = "<RSAKeyValue><Modulus>3aCH4xP82iontja5TQKyYU+GOpUsGN+uKL8oaoji5TC1iB3qK2zXojxuNRotC8p7QVLj4vH5RLkD9re1O0KmmPXmhudOe0ZG9XfvmPrE/zHiWLv4u/jx79n7WJ3wR0eKpHxzJ3BvEd9LA2+fCc5vNaoPbJqt9TZb9C7P8NRXmwc=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
private static readonly string privatekey = "<RSAKeyValue><Modulus>3aCH4xP82iontja5TQKyYU+GOpUsGN+uKL8oaoji5TC1iB3qK2zXojxuNRotC8p7QVLj4vH5RLkD9re1O0KmmPXmhudOe0ZG9XfvmPrE/zHiWLv4u/jx79n7WJ3wR0eKpHxzJ3BvEd9LA2+fCc5vNaoPbJqt9TZb9C7P8NRXmwc=</Modulus><Exponent>AQAB</Exponent><P>+Lfc3IC7dZNxSiTpVAUOr9M5sPpyMbc7zrshFtRTWgdKyGVChywpXGs1ZtrVhZxQ5GkmRumrYQWrpd8dNtwcqw==</P><Q>5B2e343rQRuXx3JdltD8zShhEaI7M0ZbV55sfWY8Wrm7GIZGKpDRgLadC+d9wyXkn2XWYL4EsBjfljNNGVnDFQ==</Q><DP>J4t0Pkrpg0QyGwvLXWSAdUwsPAtV5LmxdKkkFbEkn4fRSBNxhHRtDxisoPHiSSAVbHubbmLwsLhqFjFnP7W3iQ==</DP><DQ>imEdm2ewKpLeg/Lwqyij8qBoIhZT60rm5kQIdBmsyyyYPsxVjETFkls08Z/APr2h7sAMSCt9FIZCPA9q2dIF8Q==</DQ><InverseQ>jtsK5aykDk5KxVT8ltGvuJqy5R/UyJ3nWthpuwp8Vh1zsyfvHbu8u9NeVdQ9G6icMIuveLktLhS1uGwCAqqh+Q==</InverseQ><D>BkejMhMB6iihoW8hhwYZidpYAlbxve6wn6Ty/znLvBTKQAYg+YQHBCjUMpsARcJCBdLUdDzJBGJ9s0L3VquHzuY97mQWVe6Prnoj7IdZDPzGQQHM0vjsRosofXlT9dSp892SVTQwFajRA/tWQbG9+jAbSVDIj7MDb3LZXfNmhUk=</D></RSAKeyValue>";
/// <summary>
/// 加密
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string Encrypt(string data)
{
return Encrypt(data, Encoding.UTF8);
}
/// <summary>
/// 加密
/// </summary>
/// <param name="data"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string Encrypt(string data, Encoding encode)
{
byte[] dataByteArray = encode.GetBytes(data);
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(publickey);
byte[] encrypt = provider.Encrypt(dataByteArray, false);
return Convert.ToBase64String(encrypt);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string Decrypt(string data)
{
return Decrypt(data, Encoding.UTF8);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="data"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string Decrypt(string data, Encoding encode)
{
byte[] dataByteArray = Convert.FromBase64String(data);
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(privatekey);
byte[] decrypt = provider.Decrypt(dataByteArray, false);
return encode.GetString(decrypt);
}
}
}
调用示例：
复制内容到剪贴板 程序代码string str = EncryptHelpr.Encrypt("http://www.mzwu.com/");
Console.WriteLine(str);
Console.WriteLine(EncryptHelpr.Decrypt(str));
说明：相同字符串相同公钥RSA每次生成的结果不一样！
常见问题
问：怎么生成RSA公钥和密钥？
答：RSA公钥和密钥生成方法如下：
复制内容到剪贴板 程序代码RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
string publickey = provider.ToXmlString(false);
string privatekey = provider.ToXmlString(true);

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.617 ms

返回

标题: C#使用RSA加密字符串示例

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.617 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.633 ms

返回