博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RSA加密的测试demo
阅读量:6237 次
发布时间:2019-06-22

本文共 1573 字,大约阅读时间需要 5 分钟。

使用.net自带的RSA,需要引用System.Security.Cryptography

测试环境.net4.6

static void Main(string[] args){    var RSA = new RSACryptoServiceProvider();    var publickKey = RSA.ToXmlString(false);    var privateKey = RSA.ToXmlString(true);    var publicKeyRSA = new RSACryptoServiceProvider();    publicKeyRSA.FromXmlString(publickKey);    var privateKeyRSA = new RSACryptoServiceProvider();    privateKeyRSA.FromXmlString(privateKey);    var encryptBytes = publicKeyRSA.Encrypt(Encoding.Default.GetBytes("hellow world"), false);    string tmp = "";    for (int i = 0; i < encryptBytes.Length; i++)    {        tmp += encryptBytes[i] + ",";    }    tmp = tmp.TrimEnd(',');    Console.WriteLine(tmp);    var sign = privateKeyRSA.SignData(encryptBytes, "SHA1");    if (publicKeyRSA.VerifyData(encryptBytes, "SHA1", sign))    {        var decryptBytes = privateKeyRSA.Decrypt(encryptBytes, false);        var str = Encoding.Default.GetString(decryptBytes);        Console.WriteLine(str);    }    else    {        Console.WriteLine("Sign Error!");    }    Console.Read();}

 

 

运行打印:

104,205,171,68,41,206,37,127,112,195,150,236,162,29,144,253,78,197,173,65,75,240,223,35,186,212,218,50,24,60,144,133,174,97,74,90,61,97,97,54,214,231,30,191,33,18,102,103,230,79,231,100,171,129,0,165,33,147,22,69,83,67,105,212,92,118,112,165,200,11,91,241,56,161,218,11,62,179,83,167,141,27,2,188,169,102,222,133,37,191,41,48,246,90,238,164,105,54,238,126,6,140,74,197,208,164,197,209,98,68,243,160,165,66,64,223,234,175,160,5,24,198,169,206,110,100,235,6hellow world

 

转载于:https://www.cnblogs.com/hont/p/5168633.html

你可能感兴趣的文章
2016 年总结
查看>>
将String转化成Stream,将Stream转换成String
查看>>
【工具使用系列】关于 MATLAB 遗传算法与直接搜索工具箱,你需要知道的事
查看>>
Kali-linux Arpspoof工具
查看>>
PDF文档页面如何重新排版?
查看>>
基于http协议使用protobuf进行前后端交互
查看>>
bash腳本編程之三 条件判断及算数运算
查看>>
php cookie
查看>>
linux下redis安装
查看>>
量子通信和大数据最有市场突破前景
查看>>
如何申请开通微信多客服功能
查看>>
Sr_C++_Engineer_(LBS_Engine@Global Map Dept.)
查看>>
非监督学习算法:异常检测
查看>>
jquery的checkbox,radio,select等方法总结
查看>>
Linux coredump
查看>>
Ubuntu 10.04安装水晶(Mercury)无线网卡驱动
查看>>
我的友情链接
查看>>
ElasticSearch 2 (32) - 信息聚合系列之范围限定
查看>>
VS2010远程调试C#程序
查看>>
[MicroPython]TurniBit开发板DIY自动窗帘模拟系统
查看>>