JAVA,NET RSA密鑰格式轉換

來源:互聯網
上載者:User

標籤:

JAVA和NET RSA密鑰格式相互轉換(公開金鑰,私密金鑰)

不多說直接上代碼,需要引用開源類庫BouncyCastle.Crypto.dll

也可以在這裡下載http://downloads.bouncycastle.org/csharp/bccrypto-net-1.7-bin.zip

以下為轉化代碼

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

 

 

JAVA,NET RSA密鑰格式轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.