How to decrypt with RSA programmatically
In this section, we are going to develop a small rsa-decrypt program, so that we can learn how to do RSA decryption.
Our program will take three command-line arguments similar to those taken by rsa-encrypt
, but note that the third argument is the name of a file containing a keypair, not just a public key:
- The input filename
- The output filename
- The RSA keypair filename
Of course, now the input file is expected to contain ciphertext, which will be decrypted, and the resulting plaintext will be written into the output file.
Our high-level implementation plan for rsa-decrypt
will contain the sequence of actions opposite to that of rsa-encrypt
, listed as follows:
- Load an RSA keypair from the RSA keypair file. We need an RSA private key for decryption; a public key will not be enough.
- Create the
EVP_PKEY
context from the key. - Initialize the
EVP_PKEY
context for decryption and set the OAEP padding mode...