. NET PEM key file generated using OpenSSL [1024 bits]

Source: Internet
Author: User
Tags begin rsa private key modulus

Using system;using system.text;using system.security.cryptography;using system.web;using System.IO;namespace thinhunan.cnblogs.com.rsautility{public class Pemconverter {//<summary>///Convert PEM format key to Rsapa Rameters//</summary>//<param name= "pemfileconent" &GT;PEM public key contents </param>//<ret Urns> converted rsaparamenters</returns> public static RSAParameters Convertfrompempublickey (String Pemfileconen T) {if (string. IsNullOrEmpty (pemfileconent)) {throw new ArgumentNullException ("Pemfileconent", "This arg cann"            t be empty. "); Pemfileconent = Pemfileconent.replace ("-----BEGIN public KEY-----", ""). Replace ("-----END public KEY-----," "). Replace ("\ n", "").            Replace ("\ R", "");            byte[] KeyData = convert.frombase64string (pemfileconent); if (Keydata.length < 162) {throw new ArgumentException ("Pem file ContenT is incorrect. ");            byte[] Pemmodulus = new byte[128];            byte[] pempublicexponent = new Byte[3];            Array.copy (KeyData, Pemmodulus, 0, 128);            Array.copy (KeyData, 159, Pempublicexponent, 0, 3);            RSAParameters para = new RSAParameters (); Para.            modulus = Pemmodulus; Para.            Exponent = pempublicexponent;        return para; }///<summary>//Convert PEM format private key to RSAParameters//</summary>//<param name= "PE Mfileconent "&GT;PEM private key content </param>///<returns> convert rsaparamenters</returns> public static R Saparameters Convertfrompemprivatekey (String pemfileconent) {if (string. IsNullOrEmpty (pemfileconent)) {throw new ArgumentNullException ("Pemfileconent", "This arg cann"            t be empty. "); Pemfileconent = Pemfileconent.replace ("-----BEGIN RSA PRIVATE KEY-----," "). Replace ("-----end RSA PRIVATE KEY-----"," "). Replace ("\ n", "").            Replace ("\ R", "");            byte[] KeyData = convert.frombase64string (pemfileconent);            if (Keydata.length < 609) {throw new ArgumentException ("Pem file content is incorrect.");            } int index = 11;            byte[] Pemmodulus = new byte[128];            Array.copy (KeyData, index, Pemmodulus, 0, 128);            Index + = 128;            Index + = 2;//141 byte[] pempublicexponent = new Byte[3];            Array.copy (KeyData, index, pempublicexponent, 0, 3);            Index + = 3;            Index + = 4;//148 byte[] pemprivateexponent = new byte[128];            Array.copy (KeyData, index, pemprivateexponent, 0, 128);            Index + = 128;            Index + = ((int) keydata[index+1] = = 64?2:3);//279 byte[] pemPrime1 = new byte[64];            Array.copy (KeyData, index, PEMPRIME1, 0, 64);            Index + = 64; Index + = ((int) keydata[Index + 1] = = 64?            2:3);//346 byte[] pemPrime2 = new byte[64];            Array.copy (KeyData, index, pemPrime2, 0, 64);            Index + = 64;            Index + = ((int) Keydata[index + 1] = = 2:3);//412/413 byte[] pemExponent1 = new byte[64];            Array.copy (Keydata,index, pemExponent1, 0, 64);            Index + = 64;            Index + = ((int) Keydata[index + 1] = = 2:3);//479/480 byte[] PemExponent2 = new byte[64];            Array.copy (KeyData, index, PemExponent2, 0, 64);            Index + = 64;            Index + = ((int) Keydata[index + 1] = = 2:3);//545/546 byte[] pemcoefficient = new byte[64];            Array.copy (KeyData, index, pemcoefficient, 0, 64);            RSAParameters para = new RSAParameters (); Para.            modulus = Pemmodulus; Para.            Exponent = pempublicexponent; Para.            D = pemprivateexponent; Para.            P = pemPrime1; Para.            Q = pemPrime2; Para. DP =PemExponent1; Para.            DQ = PemExponent2; Para.            Inverseq = pemcoefficient;        return para; }}} Test Pem for RSAParameters success, using via: Using system;using system.security.cryptography;using system.text;using System . Io;using System.web;namespace thinhunan.cnblogs.com.rsautility{class Program {#region Keys const STR ing publickey =@ "-----BEGIN public KEY-----migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdpsdr+w45afhikvotzagk/ Thlffpuzfutghhwkham3h7yvl42j4xhrtr6ieudcl4eke6qiigvysnol3u4sergoeymv1f+cocu9imgnnoicbh1zvw6e8/igt3xayqizjovuwa        /tc/zdds2ihcjfhdbdsouocxecpapywcgqnsh5sqidaqab-----END Public KEY-----"; Const string Privatekey =@ "-----BEGIN RSA PRIVATE KEY-----miicxqibaakbgqdpsdr+w45afhikvotzagk/ Thlffpuzfutghhwkham3h7yvl42j4xhrtr6ieudcl4eke6qiigvysnol3u4sergoeymv1f+cocu9imgnnoicbh1zvw6e8/igt3xayqizjovuwa /tc/zdds2ihcjfhdbdsouocxecpapywcgqnsh5sqidaqabaogbam/jbfs4y5wbmncrmjpqj+uroxvocelrvrc/4kq+zgcvtpwywbagwiuro+ Czcxrvq6bggu362e9hr8f4xfvikemdl4smjbgsda1k71i+/lnnzf6sjidbfq/ja9sk4pyry7a3ikebqnjmknanykugyq1xmcjbuh556foerpahnhx1akea/flrxjsy1z+ ny1rpgdoedqyg6mhwu1jl0yj1sw3or4qgrxhjtegscrkqv0/ajqdkdem7fnkqnmsb+vpd116j6wjbaouny3oowvy2fq32mj6xv+ s2vcg1oseuaeuwvegkgqj9co6100qpj15036aqeedqbjdqs0shferswevtjziap9mcqcemgddjkrnda5cfb0yiq4frchj7a6o90wdahw3fp6lsah59mzfmc6e a0xwhdlpz8stkcmalvnkyprwztz6ctqmcqqc8iwbeay+apvbhhmjg4hjrdpnbwo6mbleud3curzfedftrlu2mevdv20xc6ziy3qtq/ 4fpzzngdzcseuc3km5rakapgkzmwetnwdjmcujbsbrqmffrqobqmpbpe+        GENIQQTTWU1OULHLMUG9EW31WRI2UIXCFCJMHURO6IOQ1VJ4QS-----END RSA PRIVATE KEY-----";                    #endregion static void Main (string[] args) {testsignandverify (); } public static void Testsignandverify () {//sign RSAParameters para = Pe            Mconverter.convertfrompemprivatekey (Privatekey);            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider (); Rsa.            ImportParameters (para); Byte[] TestdatA = Encoding.UTF8.GetBytes ("Hello");            MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider (); byte[] SignData = RSA.            SignData (TestData, MD5);            Verify RSAParameters parapub = Pemconverter.convertfrompempublickey (PublicKey);            RSACryptoServiceProvider rsapub = new RSACryptoServiceProvider ();            Rsapub.importparameters (parapub);            if (Rsapub.verifydata (TestData, MD5, SignData)) {Console.WriteLine ("OK");            } else {Console.WriteLine ("No"); }        }    }}

  

Original address: Http://www.itstrike.cn/Question/-NET-generates-pem-using-OpenSSL-key-file

. NET PEM key file generated using OpenSSL [1024 bits]

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.