C#RSA的公钥为啥是XML?怎么跟JAVA的RSA对应啊?

2019-01-03 00:48发布

C#RSA的公钥为啥是XML?怎么跟JAVA的RSA对应啊?

标签: winform mvc
2条回答
地球回转人心会变
2楼-- · 2019-01-03 00:48

用 BouncyCastle组件

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System;
 5 using System.Xml;
 6 using Org.BouncyCastle.Asn1.Pkcs;
 7 using Org.BouncyCastle.Asn1.X509;
 8 using Org.BouncyCastle.Crypto.Parameters;
 9 using Org.BouncyCastle.Math;
10 using Org.BouncyCastle.Pkcs;
11 using Org.BouncyCastle.Security;
12 using Org.BouncyCastle.X509;
13 
14 namespace MO.Security
15 {
16    
17   /// <summary>
18   /// RSA密钥格式转换
19   /// </summary>
20   public class RSAKeyConvert
21   {
22       /// <summary>    
23       /// RSA私钥格式转换,java->.net    
24       /// </summary>    
25       /// <param name="privateKey">java生成的RSA私钥</param>    
26       /// <returns></returns>   
27       public static string RSAPrivateKeyJava2DotNet(string privateKey)
28       {
29           var privateKeyParam =
30               (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
31           return
32               string.Format(
33                   "<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>",
34                   Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
35                   Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
36                   Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
37                   Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
38                   Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
39                   Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
40                   Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
41                   Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));
42       }
43       /// <summary>    
44       /// RSA私钥格式转换,.net->java    
45       /// </summary>    
46       /// <param name="privateKey">.net生成的私钥</param>    
47       /// <returns></returns>   
48       public static string RSAPrivateKeyDotNet2Java(string privateKey)
49       {
50           XmlDocument doc = new XmlDocument();
51           doc.LoadXml(privateKey);
52           BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
53           BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
54           BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));
55           BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));
56           BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));
57           BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));
58           BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));
59           BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText));
60           RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv);
61           PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam);
62           byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
63           return Convert.ToBase64String(serializedPrivateBytes);
64       }
65 
66       /// <summary>    
67       /// RSA公钥格式转换,java->.net    
68       /// </summary>    
69       /// <param name="publicKey">java生成的公钥</param>    
70       /// <returns></returns>    
71       public static string RSAPublicKeyJava2DotNet(string publicKey)
72       {
73           RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
74           return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
75               Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
76               Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
77       }
78       /// <summary>    
79       /// RSA公钥格式转换,.net->java    
80       /// </summary>    
81       /// <param name="publicKey">.net生成的公钥</param>    
82       /// <returns></returns>   
83       public static string RSAPublicKeyDotNet2Java(string publicKey)
84       {
85           XmlDocument doc = new XmlDocument(); doc.LoadXml(publicKey);
86           BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
87           BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
88           RsaKeyParameters pub = new RsaKeyParameters(false, m, p);
89           SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);
90           byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
91           return Convert.ToBase64String(serializedPublicBytes);
92       }
93 
94 
95   }
96      
97 }

 

查看更多
淡お忘
3楼-- · 2019-01-03 00:57

百度下啊,这个有方法在C#中 把秘钥转化为java中的形式;同理 java中也有把java的秘钥转化为C#的样式

查看更多
登录 后发表回答