CryptographicException “Key not valid for use in specified state.” while trying to export RSAParameters of a X509 private key

后端 未结 7 1639
悲&欢浪女
悲&欢浪女 2020-12-03 06:34

I am staring at this for quite a while and thanks to the MSDN documentation I cannot really figure out what\'s going. Basically I am loading a PFX file from the disc into a

7条回答
  •  醉话见心
    2020-12-03 07:03

    I believe that the issue may be that the key is not marked as exportable. There is another constructor for X509Certificate2 that takes an X509KeyStorageFlags enum. Try replacing the line:

    X509Certificate2 x = new X509Certificate2(@"C:\temp\certs\1\test.pfx", "test");
    

    With this:

    X509Certificate2 x = new X509Certificate2(@"C:\temp\certs\1\test.pfx", "test", X509KeyStorageFlags.Exportable);
    

提交回复
热议问题