问题
I am trying to use AES GCM encryption mechanism provided by OpenSSL in C++ and using example on this link as reference: https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
However, following statement gives me error:
/* Set IV length if default 12 bytes (96 bits) is not appropriate */
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
handleErrors();
The error that I get is:
error: ‘EVP_CTRL_GCM_SET_IVLEN’ was not declared in this scope".
I do not understand, why I cannot set IVLEN to 16 bytes? I do not want to use the default value of 12 bytes. Any pointers will be great.
回答1:
I resolved the error. Actually, in the example code, the order for initializing encryption operation and setting IV length is as follows:
* Initialise the encryption operation. */
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
handleErrors();
/* Set IV length if default 12 bytes (96 bits) is not appropriate */
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
handleErrors();
I had reverse order for these statements i.e. setting IV length first and then initializing encryption operation. I thought, these were independent steps and the order didn't matter. But, maybe, the interface needs to know, which encryption mechanism it has use before setting any parameters.
来源:https://stackoverflow.com/questions/39088633/unable-to-set-iv-for-aes-gcm-using-openssl