Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have PKCS12 file (secured by password) which contains Aux key and main key generated using aux key.
Now I am trying to read the keys programmatically in C#.


Please let me know am I missing something? or the approach I am following is not correct? If so, please share how to achieve it without any third party paid library

What I have tried:

1. Key store explorer
Using key store explorer, I can read the key using password, but I want to
do it programmatically.

2. Pkcs12Store (Org.BouncyCastle.Pkcs)
Pkcs12Store store = new Pkcs12Store(new FileStream(jksFilePath,
FileMode.Open, FileAccess.Read), passWord.ToCharArray());
Here I am not getting any exception but store returns 0 key count.

3. Using X509Certificate2Collection and X509Certificate2
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import(jksFilePath, passwordToOpenJKS,
X509KeyStorageFlags.DefaultKeySet);
Here, I collection does not contains any key

4. using X509Certificate2
X509Certificate2 cert = new X509Certificate2(jksFilePath, secString,
X509KeyStorageFlags.UserKeySet);
Here I am getting an error "Parameter incorrect". I have tried all
possible X509KeyStorageFlags
Posted
Comments
Sandeep Mewara 24-Dec-22 0:38am    
Did you try with X509KeyStorageFlags.EphemeralKeySet/different keyset?

Few references:
https://www.daimto.com/extract-pub-file-from-a-p12-certificate-with-c/
https://docs.hidglobal.com/auth-service/docs/buildingapps/csharp/read-different-certificate-key-file-formats-with-c-.htm
Vir Pratap Singh Chauhan 31-Mar-23 9:19am    
Did you get your solution, I am also struggling with the same situation. Please suggest.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900