RE: RSACryptoServiceProvider.ImportParameters exception isn't helpful

From: Jonathan Ruckert (
Date: 03/17/04

Date: Tue, 16 Mar 2004 22:46:05 -0800

Hi Meno,

 Seems like this is a bit of a problem that happens usually.

 Its a bit wierd but this is the way that is suggested to solve your problem. Reading the key in an XML form using the FromXMLString will not work, as it will expect two elements modulus and exponent. There must be an easier way of doing this but if you extract the modulus from the key, convert to hex and load it in byte by byte into the RSAParameters.

Also it seems that the standard DER ASN1-PKCS7 encoded key and the PEM key (base 64 encoded with header and footer) is not supported. However i need to do a bit more research into this fact to be 100% sure.

Hope this points you in the right direction.

Jonathan Ruckert

     ----- meno abels wrote: -----
     I try to load an private key which was generated by openssl.
     To import that key i converted it to an RSAParameters structure.
     Now I think the convertion of the key is ok. So i try to
     use the key:
     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
     If I run this code i only get the very helpful exception:
     "Invalid key" from ImportParameters.
     So my problem is there is no complete documentation about
     RSAParameters only the members are named nothing about the
     format etc. So I have to use the public knowledge about
     RSA Keys which are not more than large numbers binary coded in
     byte arrays. So why should my large numbers are "Invalid key".
     What i discovered is if I use:
     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(4096);
     RSAParameters p = rsa.ExportParameters(true);
     The structure to my p is very equal. All but the length of some
     key components are to vary by one. Which i can't explain why the
     key from openssl has such odd lengths. So i aligned the lengths
     to the lengths which are given by RSACryptoServiceProvider(4096) in
     of knowledge that the later decrypting is properly failing. But
     ImportParameters also generating the exceptions "Invalid Key"