C#.net call Bouncycastle to generate PEM-formatted private and public keys

Source: Internet
Author: User

Rsakeypairgenerator r =NewRsakeypairgenerator (); R.init (NewKeygenerationparameters (NewSecureRandom (),1024x768)); Asymmetriccipherkeypair Keys=R.generatekeypair (); Asymmetrickeyparameter Private_key=keys.            Private; Asymmetrickeyparameter Public_key=keys.            public; TextWriter TextWriter=NewStringWriter (); Pemwriter Pemwriter=NewPemwriter (TextWriter); Pemwriter.writeobject (keys.            Private);            PemWriter.Writer.Flush (); stringPrivatekey =textwriter.tostring (); Txtpri.text=Privatekey; TextWriter Textpubwriter=NewStringWriter (); Pemwriter Pempubwriter=NewPemwriter (Textpubwriter); Pempubwriter.writeobject (keys.            public);            PempubWriter.Writer.Flush (); stringPubKey =textpubwriter.tostring (); Txtpub.text= PubKey;

-

Reference:

using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Generators; using ORG.BOUNCYCASTLE.OPENSSL; using Org.BouncyCastle.Security;

--xml format to PEM format, reference: http://www.jianshu.com/p/faefcc58c79b

  Public Static voidXMLCONVERTTOPEM ()//XML Format key to PEM    {        varRSA2 =NewRSACryptoServiceProvider (); using(varSR =NewStreamReader ("E:\\privatekey.xml") {rsa2. Fromxmlstring (Sr.        ReadToEnd ()); }        varp = rsa2. Exportparameters (true); varKey =NewRsaprivatecrtkeyparameters (NewBigInteger (1, P.modulus),NewBigInteger (1, P.exponent),NewBigInteger (1, P.D),NewBigInteger (1, P.P),NewBigInteger (1, P.Q),NewBigInteger (1, P.DP),NewBigInteger (1, P.DQ),NewBigInteger (1, P.inverseq)); using(varSW =NewStreamWriter ("E:\\privatekey.pem"))        {            varPemwriter =NewOrg.BouncyCastle.OpenSsl.PemWriter (SW);        Pemwriter.writeobject (key); }    }

The online example reads the XML-formatted private key from the file and is stored in a PEM file, which is the converted format.    But the code that was deposited directly into string strings has not been researched. The code that reads the XML private key can be replaced by generating an XML private key.

C#.net call Bouncycastle to generate PEM-formatted private and public keys

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.