Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5983281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:12:29+00:00 2026-05-22T22:12:29+00:00

I am currently able to extract a private key from a PFX file using

  • 0

I am currently able to extract a private key from a PFX file using OpenSSL using the following commands:

openssl pkcs12 -in filename.pfx -nocerts -out privateKey.pem

openssl.exe rsa -in privateKey.pem -out private.pem

The private.pem file begins with ---BEGIN RSA PRIVATE KEY--- and ends with ---END RSA PRIVATE KEY---

I want to do the same in C# using .NET libraries or the Bouncy Castle library.

How do I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:12:29+00:00Added an answer on May 22, 2026 at 10:12 pm

    This is what worked for me. Should also work for you:

    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    using Org.BouncyCastle.Crypto;
    using Org.BouncyCastle.OpenSsl;
    using Org.BouncyCastle.Security;
    
    namespace SO6258771
    {
        class Program
        {
            static void Main()
            {
                // Load your certificate from file
                X509Certificate2 certificate = new X509Certificate2("filename.pfx", "password", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
    
                // Now you have your private key in binary form as you wanted
                // You can use rsa.ExportParameters() or rsa.ExportCspBlob() to get you bytes
                // depending on format you need them in
                RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)certificate.PrivateKey;
    
                // Just for lulz, let's write out the PEM representation of the private key
                // using Bouncy Castle, so that we are 100% sure that the result is exaclty the same as:
                // openssl pkcs12 -in filename.pfx -nocerts -out privateKey.pem
                // openssl.exe rsa -in privateKey.pem -out private.pem
    
                // You should of course dispose of / close the streams properly. I'm skipping this part for brevity
                MemoryStream memoryStream = new MemoryStream();
                TextWriter streamWriter = new StreamWriter(memoryStream);
                PemWriter pemWriter = new PemWriter(streamWriter);
    
                AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetRsaKeyPair(rsa);
                pemWriter.WriteObject(keyPair.Private);
                streamWriter.Flush();
    
                // Here is the output with ---BEGIN RSA PRIVATE KEY---
                // that should be exactly the same as in private.pem
                Console.Write(Encoding.ASCII.GetString(memoryStream.GetBuffer()));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extract some info from a website using simple_html_dom. Currently I
I am currently reading a CSV file. I have to extract the dates from
Currently, I'm finding a lib able to stream video from multiple sources through one
I am currently doing some socket programming using C/C++. To be able to use
I am using iTextSharp to read text contents from PDF. I am able to
I'm currently indexing webpage using lucene. The aim is to be able to quickly
I am looking to extract the camera related information from a jpg using Java.
Is it possible to discover/extract the currently displayed events from the FullCalendar object, (ref:
I am currently working on a project where users need to be able to
Currently, I have: outByte.writeInt(0x49492a00); outByte.writeInt(0x08000000); But i want to be able to write all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.