{"id":2089,"date":"2011-11-22T14:09:58","date_gmt":"2011-11-22T12:09:58","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=2089"},"modified":"2013-04-25T15:28:39","modified_gmt":"2013-04-25T13:28:39","slug":"import-certificate-private-public-keys-pem-cer-pfx","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/import-certificate-private-public-keys-pem-cer-pfx","title":{"rendered":"Import certificate, private or public keys (PEM, CER, PFX)"},"content":{"rendered":"<h3>Encrypted private key, RSA private key in PEM file<\/h3>\n<p>PEM stands for Privacy Enhanced Mail format. The PEM format is the most common format that Certificate Authorities issue certificates in. PEM certificates usually have extensions such as .pem, .crt, .cer, and .key. They are Base64 encoded ASCII files.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPemReader pem = new PemReader();\r\nRSACryptoServiceProvider rsa = pem.ReadEncryptedPrivateKeyFromFile(\r\n   &quot;EncryptedPrivateKey.pem&quot;, \/\/ &quot;EncryptedRSAPrivateKey.pem&quot;\r\n   &quot;cypher&quot;);\r\n<\/pre>\n<p>This code handles following formats:<\/p>\n<p><strong>PKCS #8  EncryptedPrivateKeyInfo Encrypted Format:<\/strong><br \/>\n<code><br \/>\n        -----BEGIN ENCRYPTED PRIVATE KEY-----<br \/>\n        MIICojAcBgoqhkiG9w0BD .....<br \/>\n<\/code><\/p>\n<p><strong>Private Key (Traditional SSLeay RSAPrivateKey format) Encrypted:<\/strong><br \/>\n<code><br \/>\n-----BEGIN RSA PRIVATE KEY-----<br \/>\nProc-Type: 4,ENCRYPTED<br \/>\nDEK-Info: DES-EDE3-CBC,24A667C253F8A1B9<\/code><\/p>\n<p><code>mKz .....<br \/>\n<\/code><\/p>\n<p>You can remove the passphrase from the private key using openssl:<br \/>\n<code>openssl rsa -in EncryptedPrivateKey.pem -out PrivateKey.pem<\/code><\/p>\n<h3>Unencrypted private key in PEM file<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPemReader pem = new PemReader();\r\nRSACryptoServiceProvider rsa = pem.ReadPrivateKeyFromFile(&quot;PrivateKey.pem&quot;);\r\n<\/pre>\n<p>This code handles following formats:<\/p>\n<p><strong>PKCS #8  PrivateKeyInfo Unencrypted:<\/strong><br \/>\n<code><br \/>\n        -----BEGIN PRIVATE KEY-----<br \/>\n        MIICdgIBADANBgkqhkiG9w0B ......<br \/>\n<\/code><\/p>\n<p><strong>Private Key (Traditional SSLeay RSAPrivateKey format) Unencrypted:<\/strong><br \/>\n<code><br \/>\n        -----BEGIN RSA PRIVATE KEY-----<br \/>\n        MIICXQIBAAKBgQCcHVm  .....<br \/>\n<\/code><\/p>\n<h3>Public key in PEM file<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPemReader pem = new PemReader();\r\nRSACryptoServiceProvider rsa = pem.ReadPublicKeyFromFile(&quot;PublicKey.pem&quot;)\r\n<\/pre>\n<p>This code handles following formats:<\/p>\n<p><strong>Public Key (SubjecPublicKeyInfo):<\/strong><br \/>\n<code><br \/>\n        -----BEGIN PUBLIC KEY-----<br \/>\n        MIGfMA0GCSqGSIb3DQEB .....<br \/>\n<\/code><\/p>\n<h3>Certificate\/private key in PFX file<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nX509Certificate2 certificate  = new X509Certificate2(\r\n   &quot;certificate.pfx&quot;,\r\n   &quot;&quot;,\r\n   X509KeyStorageFlags.PersistKeySet)\r\n\r\nif (certificate.HasPrivateKey)\r\n{\r\n  using (var rsa = (RSACryptoServiceProvider)certificate.PrivateKey)\r\n  {\r\n    \/\/ ...\r\n  }\r\n}\r\n<\/pre>\n<h3>Certificate in PEM\/CER file<\/h3>\n<p>Note: The private key is never stored in a .pem\/.cer certificate file.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nX509Certificate2 certificate = new X509Certificate2(&quot;certificate.cer&quot;);\r\n<\/pre>\n<p>-or-<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPemReader pem = new PemReader();\r\nX509Certificate2 certificate = pem.ReadCertificateFromFile(&quot;certificate.cer&quot;);\r\n<\/pre>\n<p>This code handles following formats:<\/p>\n<p><code><br \/>\n        -----BEGIN CERTIFICATE-----<br \/>\n        MIIFsTCCA5mgAwIBAgIKYQ .....<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Encrypted private key, RSA private key in PEM file PEM stands for Privacy Enhanced Mail format. The PEM format is the most common format that Certificate Authorities issue certificates in. PEM certificates usually have extensions such as .pem, .crt, .cer, and .key. They are Base64 encoded ASCII files. PemReader pem = new PemReader(); RSACryptoServiceProvider rsa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[18,33,49],"class_list":["post-2089","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-dkim","tag-email-component","tag-smime"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/2089"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=2089"}],"version-history":[{"count":7,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/2089\/revisions"}],"predecessor-version":[{"id":3980,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/2089\/revisions\/3980"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=2089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=2089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=2089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}