Re: RSACryptoServiceProvider.ImportParameters exception isn't helpful

From: meno abels (meno.abels_at_adviser.com)
Date: 03/17/04


Date: Wed, 17 Mar 2004 08:32:36 +0100

Hi Jonathan,

thanks for you quick comment but the problem is not that my private key is in an
specified format. I read it in from an internal format and generate out of these
the values for the RSAParameters struct. So I can only have the problem that
the expected byte[] are not directly interpreted as large binary numbers.
I will do a cross check by writing out the privatekey with ExportParameters and
try to use the key with openssl and import it back to my interal format.
So i can prooven that my conversion is ok. But I like to use the openssl
generate key infact of my infrastructure.

Meno
> 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.
>
> Regards,
> 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();
> rsa.ImportParameters(BuildRSAParameters());
> 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"
>
> meno
>



Relevant Pages

  • Re: Format of the Public/Private key BLOB
    ... I have a PEM file with private key to decrypt it. ... CryptAPI and Linux using Openssl. ... default DER format? ...
    (microsoft.public.platformsdk.security)
  • Re: CryptAPI(encryption/decryption)
    ... The openssl encrypted data format is in bigendian ... Why there is so many compatibility difference between MS Crypt and openssl? ... I misspelled the Private Key as Primary Key. ... Is there any variation in the encryption format in openssl compared to ...
    (microsoft.public.pocketpc.developer)
  • Re: Converting from a openssl dsa key to a one line SSH2 key (for authorized_keys)
    ... by openssl and convert it to a ssh2 public key without knowing the ... very hard at the key data you posted and reverse-engineering the ... format, and there was one part of the format I didn't fully ... without the corresponding private key I can't _actually_ test that ...
    (comp.security.ssh)
  • Re: CryptAPI(encryption/decryption)
    ... data generated by openssl rsautl is the reverse of that which is accepted by ... I misspelled the Private Key as Primary Key. ... and the priavte key in PEM format. ... Is there any variation in the encryption format in openssl compared to ...
    (microsoft.public.pocketpc.developer)
  • Re: CryptAPI(encryption/decryption)
    ... The PFX format encrypts the private key with the user supplied password so ... exchanging private keys using this format is as safe as using the PEM ...
    (microsoft.public.pocketpc.developer)