Solved

Using C# to extract publick key from pfx file without a password

Posted on 2016-10-18
2
78 Views
Last Modified: 2016-10-22
I want to know if it is possible, and how, to use C# to extract publick keys from pfx files without a password. I understand the password is there to protect the private key, so the public keys should be available without the password right?
0
Comment
Question by:itnifl
2 Comments
 
LVL 61

Accepted Solution

by:
btan earned 500 total points
Comment Utility
You still need password as pfx is protected by password. Your can extract if it is plain pem or crt but no pfx without password.  You should be able to get a collection object containing the certs in your .pfx file by using the X509Certificate2Collection class
string certPath = <YOUR PFX FILE PATH>;
string certPass = <YOUR PASSWORD>;

// Create a collection object and populate it using the PFX file
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet);

foreach (X509Certificate2 cert in collection)
{
    Console.WriteLine("Subject is: '{0}'", cert.Subject);
    Console.WriteLine("Issuer is:  '{0}'", cert.Issuer);

    // Import the certificates into X509Store objects
}
In .NET, the X509Certificate2 object has properties for the PublicKey and PrivateKey. But that's largely for convenience. A certificate is something you are supposed to present to someone to prove something, and by design, it's only the public portion of the public/private key pair that is ever presented to anyone. When an X509 certificate is presented to someone, .NET of course strips out the private key. Having the private key property on the certificate object is a bit of a misrepresentation, especially since, as we'll see, there's a big difference in how the public and private key are dealt with.
http://paulstovell.com/blog/x509certificate2

May be better to use openssl
•Private key: openssl pkcs12 -in yourP12File.pfx -nocerts -out privateKey.pem

•Certificates: openssl pkcs12 -in yourP12File.pfx -clcerts -nokeys -out publicCert.pem
http://anuchandy.blogspot.sg/2012/04/extracting-public-certificate-and.html
0
 
LVL 2

Author Closing Comment

by:itnifl
Comment Utility
Find it weird that OpenSSL can do it, but not .Net.
What you tell me is the same as what I found.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SSL RA VPN 7 74
Please explain: Aspect Oriented Programming 2 51
Chat Room 1 25
Performance  Html.BeginForm vs jQuery 3 10
SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now