I'm trying to follow the examples provided to load a public RSA key. I generated the key as:
openssl genrsa -out my_priv_key.pem 4096
openssl rsa -in my_priv_key.pem -out my_pub_key.pem -pubout
Then converted the PEM files to DER files like this:
openssl rsa -in my_priv_key.pem -inform PEM -out my_priv_key.der -outform DER
openssl rsa -in my_priv_key.pem -pubout -out my_pub_key.der -outform DER
Parsing the private key works fine and I can successfully extract the public key components from this key to encrypt and decrypt a message (using rsaPub.SetModulus(n); rsaPub.SetPublicExponent(e)), but when I try to do:
TaoCrypt::RSA_PublicKey rsaPub(public_key_src);
TaoCrypt::PK_Lengths lengths(rsaPub.GetModulus());
printf("Pub key: FixedCiphertextLength= %u\n",lengths.FixedCiphertextLength());
I get the result:
Pub key: FixedCiphertextLength= 0
The failure seems to happen in RSA_Public_Decode::Decode() :
// Decode a BER encoded RSA Public Key
void RSA_Public_Decoder::Decode(RSA_PublicKey& key)
{
ReadHeader();
if (source_.GetError().What()) return;
// public key
key.SetModulus(GetInteger(Integer().Ref())); <---- Fails
key.SetPublicExponent(GetInteger(Integer().Ref()));
}
and more specifically here:
void Integer::Decode(Source& source)
{
byte b = source.next();
if (b != INTEGER) {
source.SetError(INTEGER_E); <-- exit path
return;
}
[..]
What is the proper way to store the public key so that it can be parsed correctly?
Regards,
Kristofer Pettersson