java,net RSA key Format conversion

Source: Internet
Author: User
Tags modulus

Java and net RSA key formats are converted to each other (public key, private key)

Not much to say directly on the code, you need to reference the Open source class library BouncyCastle.Crypto.dll

You can also download the http://downloads.bouncycastle.org/csharp/bccrypto-net-1.7-bin.zip here

The following is the conversion code

1 usingSystem;2 usingSystem.Xml;3 usingOrg.BouncyCastle.Asn1.Pkcs;4 usingOrg.BouncyCastle.Asn1.X509;5 usingOrg.BouncyCastle.Crypto.Parameters;6 usingOrg.BouncyCastle.Math;7 usingOrg.BouncyCastle.Pkcs;8 usingOrg.BouncyCastle.Security;9 usingOrg.BouncyCastle.X509;Ten /// <summary> One ///RSA key format conversion A /// </summary> -  Public classRsakeyconvert - { the     /// <summary>     -     ///RSA private key format conversion, Java->.net -     /// </summary>     -     /// <param name= "Privatekey" >Java-generated RSA private key</param>     +     /// <returns></returns>    -      Public Static stringRsaprivatekeyjava2dotnet (stringPrivatekey) +     { ARsaprivatecrtkeyparameters Privatekeyparam =(rsaprivatecrtkeyparameters) Privatekeyfactory.createkey (convert.frombase64string (PrivateKey)); at         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>", - convert.tobase64string (privateKeyParam.Modulus.ToByteArrayUnsigned ()), - convert.tobase64string (privateKeyParam.PublicExponent.ToByteArrayUnsigned ()), - convert.tobase64string (privatekeyparam.p.tobytearrayunsigned ()), - convert.tobase64string (privatekeyparam.q.tobytearrayunsigned ()), - convert.tobase64string (privateKeyParam.DP.ToByteArrayUnsigned ()), in convert.tobase64string (privateKeyParam.DQ.ToByteArrayUnsigned ()), - convert.tobase64string (privateKeyParam.QInv.ToByteArrayUnsigned ()), to convert.tobase64string (privateKeyParam.Exponent.ToByteArrayUnsigned ())); +     } -     /// <summary>     the     ///RSA private key format conversion,. Net->java *     /// </summary>     $     /// <param name= "Privatekey" >. NET-generated private keys</param>    Panax Notoginseng     /// <returns></returns>    -      Public Static stringRsaprivatekeydotnet2java (stringPrivatekey) the     { +XmlDocument doc =NewXmlDocument (); A Doc. LOADXML (Privatekey); theBigInteger m =NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("modulus")[0]. InnerText)); +BigInteger exp =NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("Exponent")[0]. InnerText)); -BigInteger d =NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("D")[0]. InnerText)); $BigInteger p =NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("P")[0]. InnerText)); $BigInteger q =NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("Q")[0]. InnerText)); -BigInteger DP =NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("DP")[0]. InnerText)); -BigInteger dq =NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("DQ")[0]. InnerText)); theBigInteger QINV =NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("Inverseq")[0]. InnerText)); -Rsaprivatecrtkeyparameters Privatekeyparam =Newrsaprivatecrtkeyparameters (M, exp, D, p, Q, DP, DQ, QINV);WuyiPrivatekeyinfo Privatekeyinfo =Privatekeyinfofactory.createprivatekeyinfo (privatekeyparam); the         byte[] Serializedprivatebytes =Privatekeyinfo.toasn1object (). Getencoded (); -         returnconvert.tobase64string (serializedprivatebytes); Wu     } -     /// <summary>     About     ///RSA public key format conversion, Java->.net $     /// </summary>     -     /// <param name= "PublicKey" >Java-generated public key</param>     -     /// <returns></returns>     -      Public Static stringRsapublickeyjava2dotnet (stringPublicKey) A     { +Rsakeyparameters Publickeyparam =(rsakeyparameters) Publickeyfactory.createkey (convert.frombase64string (PublicKey)); the         return string. Format ("<rsakeyvalue><modulus>{0}</modulus><exponent>{1}</exponent></rsakeyvalue >", - convert.tobase64string (publicKeyParam.Modulus.ToByteArrayUnsigned ()), $ convert.tobase64string (publicKeyParam.Exponent.ToByteArrayUnsigned ())); the     } the     /// <summary>     the     ///RSA public key format conversion,. Net->java the     /// </summary>     -     /// <param name= "PublicKey" >. NET-generated public key</param>     in     /// <returns></returns>    the      Public Static stringRsapublickeydotnet2java (stringPublicKey) the     { AboutXmlDocument doc =NewXmlDocument (); Doc. LOADXML (PublicKey); theBigInteger m =NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("modulus")[0]. InnerText)); theBigInteger p =NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("Exponent")[0]. InnerText)); theRsakeyparameters pub =NewRsakeyparameters (false, M, p); +Subjectpublickeyinfo Publickeyinfo =Subjectpublickeyinfofactory.createsubjectpublickeyinfo (pub); -         byte[] Serializedpublicbytes =Publickeyinfo.toasn1object (). Getderencoded (); the         returnconvert.tobase64string (serializedpublicbytes);Bayi     } the}

java,net RSA key Format conversion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.