I generated an RSA Public Key in C# with RSACryptoServiceProvider with a key size of 512-bits.
// Generate a public/private key using RSA
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(512);
// Read public key in a string
string str = RSA.ToXmlString(true);
Console.WriteLine(str);
// XML output
<RSAKeyValue>
<Modulus>26+2lMVkqh99O3nBV/EU5KkO3FYx7R/0Fnlh6FES2wvIAZ6yaHrAhrX6a/BWag4N/XRH3z+nxQBRiG1pPUDQKQ==</Modulus>
<Exponent>AQAB</Exponent>
<P>9y6RKpt6OsQFoPfsxo9+NpSJF2xrWg9+h+f6ri7svv8=</P>
<Q>44YJ0p9qDJuvIM+PaFi9p8NpYerSUAfO49camwH7mNc=</Q>
<DP>Zw9ebXJn8yqZ4jSc32kiyaUCx+ZnmCRPsGCzr35XLYc=</DP>
<DQ>Gn4OAL9dKtCp6KkiaqUCmFkxmRwtlvIBzhfK2ke10ws=</DQ>
<InverseQ>6bwnrFXgBggw2+9fyaPP4femrMH3VQgxBkj8P1B3TTs=</InverseQ>
<D>SJpisfomkZ7EiZJsln7DU+qXUbRe3aowxfippdidbaxzM3+lMgLH2V8q9Ba7M8leG8mxkvttdilojclFFi9q3Q==</D>
</RSAKeyValue>
I throw the Modulus value into a PEM file:
-----BEGIN PUBLIC KEY-----
26+2lMVkqh99O3nBV/EU5KkO3FYx7R/0Fnlh6FES2wvIAZ6yaHrAhrX6a/BWag4N/XRH3z+nxQBRiG1pPUDQKQ==
-----END PUBLIC KEY-----
OpenSSL is not able to decode the Public Key, however. Here is the command I used:
openssl rsa -pubin -inform PEM -text -noout < public-key.pem
And here is the result with errors:
unable to load Public Key
30844:error:0D07207B:asn1 encoding routines:ASN1_get_object:header too long:crypto\asn1\asn1_lib.c:101:
30844:error:0D068066:asn1 encoding routines:asn1_check_tlen:bad object header:crypto\asn1\tasn_dec.c:1137:
30844:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:crypto\asn1\tasn_dec.c:309:Type=X509_PUBKEY
30844:error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib:crypto\pem\pem_oth.c:33:
Why can't OpenSSL decode this Public Key? I even tried the default key size of 1024-bits and it still couldn't read it. Is there a compatibility issue between OpenSSL and the RSACryptoServiceProvider class? How can I get OpenSSL to properly read and decode this Public Key?