Realization of "extinct article" C#rsa algorithm

Source: Internet
Author: User

Now the most popular RSA encryption algorithm, only the public key and private key have to crack the encryption information, RSA encryption algorithm appears in favor of data security transmission

The 1.c# in the RSACryptoServiceProvider class allows you to generate XML-formatted public and private keys in a very good way, with two lines of code.

2. However, the generated XML format front-end is not very good to use and read, so in the generated XML format needs to be converted to PEM format, so as to copy directly to the verification tool encryption and decryption, very convenient
First, we first import a third-party library, because the conversion code involved in the following is required to rely on this library to implement, the import operation is as follows

Input in console
PM > Install-package bouncycastle
Import into a reference

3. OK, here is the pure Code implementation, the purpose is to convert the XML format to PEM format, regardless of the public key or private key, no wordy direct code walk

        static void Main (string[] args) {rsacryptoserviceprovider RSA = new RSACryptoServiceProvider (); String xmlprivatekey = RSA.            Toxmlstring (TRUE);//xml key String pemprivatekey = Xml2pemprivate (Xmlprivatekey, "f:/privatepem.txt");//pem key String xmlpublickey = RSA.        Toxmlstring (false);//xml Public key string pempublickey = Xml2pempublic (Xmlpublickey, "f:/publicpem.txt");//pem key }///<summary>///XML format public key to PEM format public keys///</summary>//<param name= "xml" >x ML format public key </param>///<param Name= "SaveFile" > Save physical path to file </param> publicly static string xml2pemp            Ublic (string xml, String saveFile) {var RSA = new RSACryptoServiceProvider (); Rsa.            Fromxmlstring (XML); var p = RSA.            Exportparameters (FALSE);            Rsakeyparameters key = new Rsakeyparameters (False, new BigInteger (1, P.modulus), New BigInteger (1, p.exponent)); using (var sw = new StreamWriter (saveFile)) {var pemwriter = new Org.BouncyCastle.OpenSsl.PemWrit                ER (SW);            Pemwriter.writeobject (key);            } subjectpublickeyinfo Publickeyinfo = Subjectpublickeyinfofactory.createsubjectpublickeyinfo (key); byte[] serializedpublicbytes = Publickeyinfo.toasn1object ().            Getderencoded ();            String publickey = Convert.tobase64string (serializedpublicbytes);        Return Format (PublicKey, 1); }///<summary>///XML format private key to PEM///</summary>//<param name= "xml" >xml format private Key </param>//<param name= "SaveFile" > Save physical path to file </param> public static string xml2pemprivate        (string XML, String saveFile)            {var RSA = new RSACryptoServiceProvider (); Rsa.            Fromxmlstring (XML); var p = RSA.            Exportparameters (TRUE); var key = new Rsaprivatecrtkeyparameters (New BigInteger (1, P.modulus), New BigInteger (1, p.exponent), New BigInteger (1, P.D), New BigInteger (1, P.P), new B            Iginteger (1, P.Q), New BigInteger (1, P.DP), New BigInteger (1, P.DQ), New BigInteger (1, p.inverseq)); using (var sw = new StreamWriter (saveFile)) {var pemwriter = new ORG.BOUNCYCASTLE.OPENSSL .                Pemwriter (SW);            Pemwriter.writeobject (key);            } privatekeyinfo Privatekeyinfo = Privatekeyinfofactory.createprivatekeyinfo (key); byte[] serializedprivatebytes = Privatekeyinfo.toasn1object ().            Getencoded ();            String privatekey = Convert.tobase64string (serializedprivatebytes);        Return Format (Privatekey, 2); }///<summary>//Format public key/private key////</summary>//<param name= "key" > generated public/private key & lt;/param>//<param name= "type" >1: Public key 2: Private key </param>///&LT;RETURNS&GT;PEM format public/private key </return        S>public static string Format (string key, int type) {string result = string.            Empty; int length = key.            LENGTH/64;                for (int i = 0; i < length; i++) {int start = i * 64; result = result + key.            Substring (start, +) + "\ r \ n"; } result = result + key.            Substring (length * 64); if (type = = 1) {result = result.                Insert (0, "-----BEGIN public KEY-----\ r \ n");            result + = "\ r \ n-----END public KEY-----"; } if (type = = 2) {result = result.                Insert (0, "-----BEGIN PRIVATE KEY-----\ r \ n");            result + = "\ r \ n-----END PRIVATE KEY-----";        } return result; }

4. The final output in the TXT file on the F-drive can be directly used to copy the data directly into the verification tool for encryption/decryption

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: