sanket
sanket

Reputation: 32

Retrieve Private Key for the generated CSR

I am create CSR using certenroll object. Primary key is created on the same machine(Ccertificate store).

How to retrieve primary key which is stored on local store for the CSR generated.

Upvotes: 0

Views: 1485

Answers (1)

Chris Chilvers
Chris Chilvers

Reputation: 6479

You can get the private key from X509Certificate2.PrivateKey.

var store = new X509Store (StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);

var thumbprint = "AA99202885098B541C1ECD09C85351ED084A4A12";
var certificate = store.Certificates.Find (X509FindType.FindByThumbprint, thumbprint, false) [0];

var privateKey = (RSACryptoServiceProvider) certificate.PrivateKey;

Also, X509Certificate offers methods to export as various formats including pfx.

Upvotes: 1

Related Questions