设为首页 加入收藏

TOP

RSA非对称 私钥加密(一)
2019-09-23 18:10:58 】 浏览:87
Tags:RSA 对称 加密

RSA生成公钥和私钥对

 1 /// <summary>
 2         /// RSA生成公钥和私钥
 3         /// </summary>
 4         /// <returns></returns>
 5         public static string[] GenerateKeys()
 6         {
 7             try
 8             {
 9                 string[] sKeys = new String[2];
10                 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
11                 sKeys[0] = rsa.ToXmlString(true);//私钥
12                 sKeys[1] = rsa.ToXmlString(false);//公钥
13                 return sKeys;
14             }
15             catch (Exception)
16             {
17                 return null;
18             }
19         }
View Code

RSA私钥格式转换

 1 public class RSAKeyConvert
 2     {
 3         /// <summary>
 4         /// RSA私钥格式转换,java->.net
 5         /// </summary>
 6         /// <param name="privateKey">java生成的RSA私钥</param>
 7         /// <returns></returns>
 8         public static string RSAPrivateKeyJava2DotNet(string privateKey)
 9         {
10             RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
11 
12             return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
13                 Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
14                 Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
15                 Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
16                 Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
17                 Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
18                 Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
19                 Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
20                 Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));
21         }
22 
23         /// <summary>
24         /// RSA私钥格式转换,.net->java
25         /// </summary>
26         /// <param name="privateKey">.net生成的私钥</param>
27         /// <returns></returns>
28         public static string RSAPrivateKeyDotNet2Java(string privateKey)
29         {
30             XmlDocument doc = new XmlDocument();
31             doc.LoadXml(privateKey);
32             BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
33             BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
34             BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));
35             BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));
36             BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));
37             BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));
38             BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));
39             BigInteger qinv = new BigInteger(
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇abp(net core)+easyui+efcore实现.. 下一篇C# 10分钟入门基于WebOffice实现..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目