How can constructing an X509Certificate2 from a PKCS#12 byte array throw CryptographicException(“The system cannot find the file specified.”)?

前端 未结 5 1030
有刺的猬
有刺的猬 2020-12-04 19:23

I\'m trying to construct an X509Certificate2 from a PKCS#12 blob in a byte array and getting a rather puzzling error. This code is running in a desktop applica

5条回答
  •  被撕碎了的回忆
    2020-12-04 19:51

    Do you have PKCS#12 or just PFX-file? In the Microsoft world it is the same, but other think another (see this archived page).

    You can try just following

    X509Certificate2 cert = X509Certificate2(byte[] rawData, "password");
    X509Certificate2 cert2 = X509Certificate2(byte[] rawData, "password",
                  X509KeyStorageFlags.MachineKeySet |
                  X509KeyStorageFlags.PersistKeySet |
                  X509KeyStorageFlags.Exportable);
    

    (X509Certificate2(Byte[])) or

    X509Certificate2 cert = X509Certificate2("C:\Path\my.pfx", "password");
    

    (see X509Certificate2(String, String) and Import(String, String, X509KeyStorageFlags) on Microsoft Docs if you need use some flags)

    UPDATED: It would be helpful if you insert a code fragment and not only the exception stack trace.

    Which X509KeyStorageFlags do you use? You can use Process Monitor to find out which file could not find the X509Certificate2 constructor. It can be for example that there are no default key container for the current user on the Windows XP having the problem. You can create it and retry the import.

提交回复
热议问题