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

后端 未结 7 1592
悲&欢浪女
悲&欢浪女 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:11

    For others that end up here through Google, but don't use any X509Certificate2, if you call ToXmlString on RSACryptoServiceProvider but you've only loaded a public key, you will get this message as well. The fix is this (note the last line):

    var rsaAlg = new RSACryptoServiceProvider();
    
    rsaAlg.ImportParameters(rsaParameters);
    
    var xml = rsaAlg.ToXmlString(!rsaAlg.PublicOnly);
    

提交回复
热议问题