Recently in Google Developer Console by mistake I generated a new SSL certificate for my project’s Service Account so had to convert it again from p12 file to the base64 representation and store its thumbprint separately.
Here’s how you can do it:
var cert = new X509Certificate2("project-xyz.p12", "notasecret", X509KeyStorageFlags.Exportable); var thumbprint = cert.Thumbprint; var base64 = Convert.ToBase64String(cert.Export(X509ContentType.Pfx, "notasecret")); // or Pkcs12, what works as well
And just to make sure output string actually works and the resulting certificate contains the private key:
var test = new X509Certificate2(Convert.FromBase64String(base64), "notasecret"); Debug.Assert(test.PrivateKey != null);