Search code examples
c#certificatex509certificate2

Remove password from PFX certificate


I have x509Certificate2 protected with a password in the FPX file. I would like to create a function that will remove password from it.

Stream CreatePFXWithoutPassword(string filenamePfx, string password)
{
...return new file
}

Is that even possible?


Solution

  • Sure, just import it, and export it again with "no" password (which really means the empty password).

    X509Certificate2Collection coll = new X509Certificate2Collection();
    coll.Import(filename, password);
    byte[] nopw = coll.Export(X509ContentType.Pfx);
    
    // Optional: Clean up unnecessary resources.
    foreach (X509Certificate2 cert in coll)
    {
        cert.Dispose();
    }
    
    return nopw;