1

I created a key-pair using the RSA_generate_key() API and wrote the public key to a file using PEM_write_RSAPublicKey() API.

When I use the the following openssl command to encrypt data,

openssl rsautl -encrypt -pkcs -inkey pubkey.pem -pubin -in encme.data > 
encrytedyou.data

I get,

Unable to load public key.

However, when I use the:-

RSA_private_decrypt(encrypt_len, encrypt, decrypt, keypair, 
RSA_PKCS1_PADDING) 

The encrypted data get successfully decrypted.

How should I convert the public key to a format compatible with openssl?

Thanks

jww
  • 97,681
  • 90
  • 411
  • 885
  • [Use OpenSSL RSA key with .Net](https://stackoverflow.com/a/30491534/608639). It provides the code in C++ and shows you the formats. You will have to choose the one that works for you. Instead of `PEM_write_RSAPublicKey` or `PEM_write_bio_RSAPublicKey`, use `PEM_write_PUBKEY` or `PEM_write_bio_PUBKEY`. – jww Jul 19 '17 at 08:21
  • Thanks! I had to use convert the RSA structure to EVP_PKEY in order to use PEM_write_bio_PUBKEY and it worked perfectly with openssl – user2303907 Jul 19 '17 at 11:23
  • This was probably causing the need for the different format: `-pubin`. If you look at the ASN.1/DER files created by `i2d_RSAPublicKey_bio` and `i2d_RSA_PUBKEY_bio`, the differences will become readily apparent. They are lost in the PEM encoding. – jww Jul 19 '17 at 12:28
  • This ended up being your duplicate: [OpenSSL's rsautl cannot load public key created with PEM_write_RSAPublicKey](https://stackoverflow.com/q/30547646/608639). – jww Jul 19 '17 at 12:46

0 Answers0