rsa加密算法使用示例分享
时间:2021-08-03 20:47:29
产生私钥和公钥
System.Security.Cryptography.RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
//得到私钥主要保存了RSAParameters中的8各参数
privateKey = myrsa.ToXmlString(true);
//得到公钥保存了RSAParameters中2个参数
publicKey = myrsa.ToXmlString(false);
RAS实现加密
System.Security.Cryptography.RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
//得到公钥
myrsa.FromXmlString(publicKey);
//把你要加密的内容转换成byte[]
byte[] PlainTextBArray = (new UnicodeEncoding()).GetBytes("这里是你要加密的内容");
//使用.NET中的Encrypt方法加密
byte[] CypherTextBArray = myrsa.Encrypt(PlainTextBArray, false);
//最后吧加密后的byte[]转换成Base64String,这里就是加密后的内容了
Result = Convert.ToBase64String(CypherTextBArray)
RAS实现解密
System.Security.Cryptography.RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
//得到私钥
myrsa.FromXmlString(xmlPrivateKey);
//把原来加密后的String转换成byte[]
byte[] PlainTextBArray = Convert.FromBase64String("刚才加密后的string");
//使用.NET中的Decrypt方法解密
byte[] DypherTextBArray = myrsa.Decrypt(PlainTextBArray, false);
//转换解密后的byte[],这就得到了我们原来的加密前的内容了
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
byte[] messagebytes = Encoding.UTF8.GetBytes("luo罗");
RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider();
string privatekey = oRSA.ToXmlString(true);
string publickey = oRSA.ToXmlString(false);
//私钥签名
RSACryptoServiceProvider oRSA3 = new RSACryptoServiceProvider();
oRSA3.FromXmlString(privatekey);
byte[] AOutput = oRSA3.SignData(messagebytes, "SHA1");
//公钥验证
RSACryptoServiceProvider oRSA4 = new RSACryptoServiceProvider();
oRSA4.FromXmlString(publickey);
bool bVerify = oRSA4.VerifyData(messagebytes, "SHA1", AOutput);
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java源码解析之ClassLoader
Spring中Bean的生命周期使用解析
![](https://img.aspxhome.com/file/2023/4/78924_0s.jpg)
Java实现分布式系统限流
java中的静态代码块、构造代码块、构造方法详解
关于Mybatis-Plus Wrapper是否应该出现在Servcie类中
详解SpringBoot多跨域请求的支持(JSONP)
![](https://img.aspxhome.com/file/2023/0/74920_0s.png)
Java StringUtils字符串分割转数组的实现
浅谈springioc实例化bean的三个方法
java实现斗地主发牌功能
SpringCloud之Config配置中心与Redis分布式锁详解
![](https://img.aspxhome.com/file/2023/1/77961_0s.jpg)
springboot2.0如何通过fastdfs实现文件分布式上传
![](https://img.aspxhome.com/file/2023/7/71917_0s.png)
Java基于注解的Excel导出方式
IDEA SpringBoot项目配置热更新的步骤详解(无需每次手动重启服务器)
![](https://img.aspxhome.com/file/2023/9/59659_0s.jpg)
Java毕业设计实战之平行志愿管理系统的实现
![](https://img.aspxhome.com/file/2023/2/74872_0s.jpg)
Android RecyclerBarChart绘制使用教程
![](https://img.aspxhome.com/file/2023/1/83521_0s.png)
Day14基础不牢地动山摇-Java基础
![](https://img.aspxhome.com/file/2023/7/67617_0s.jpg)
浅谈自定义校验注解ConstraintValidator
![](https://img.aspxhome.com/file/2023/9/61529_0s.jpg)
C#枚举类型与位域枚举Enum
Java 执行CMD命令或执行BAT批处理方式
![](https://img.aspxhome.com/file/2023/3/67663_0s.png)
SpringBoot下如何实现支付宝接口的使用
![](https://img.aspxhome.com/file/2023/7/58747_0s.png)