How to encrypt with RSA programmatically
OpenSSL 3.0 provides the following APIs for RSA encryption:
- A legacy API with the
RSA_
prefix and theRSA_public_encrypt()
function. This API has been deprecated since OpenSSL 3.0, so we are not going to use it. - The
EVP_PKEY
API, particularly theEVP_PKEY_encrypt()
function. We are going to use this API. - The
EVP_Seal
API. This is a hybrid encryption API that generates a session key, encrypts the session key with RSA, and then encrypts the user data with the session key. This API contains theEVP_SealInit()
,EVP_SealUpdate()
, andEVP_SealFinal()
functions, which work similarly toEVP_EncryptInit()
,EVP_EncryptUpdate()
, andEVP_EncryptFinal()
.EVP_SealUpdate()
is just#define
forEVP_EncryptUpdate()
. There are also the correspondingEVP_Open
functions for decrypting the seals –EVP_OpenInit()
,EVP_OpenUpdate()
, andEVP_OpenFinal()
. Unfortunately, theEVP_Seal
API is rather inflexible. It supports only RSA encryption of...