【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); } } }
相关文章推荐
- OpenSSL编程之RSA
- 每 172 个活动 RSA 证书中就有一个容易受到攻击
- c#调用COM组件
- 用ASP编写的加密和解密类
- 解密网页加密的两个方法
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- .NET实现一机一码加密、把EXE变成图片运行,被破解自动销毁随时授权回收