无法使用openssl为aes gcm设置IV

Unable to set IV for aes gcm using openssl

本文关键字:aes gcm 设置 IV openssl      更新时间:2023-10-16

我正在尝试使用OpenSSL在c++中提供的AES GCM加密机制,并在此链接上使用示例作为参考:https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption

但是,下面的语句给了我错误:

/* 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();
我得到的错误是:

错误:' EVP_CTRL_GCM_SET_IVLEN '未在此范围内声明'。

我不明白,为什么我不能将IVLEN设置为16字节?我不想使用默认值12字节。

我解决了这个错误。实际上,在示例代码中,初始化加密操作和设置IV长度的顺序如下:

* 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();

我对这些语句有相反的顺序,即先设置IV长度,然后初始化加密操作。我想,这些是独立的步骤,顺序无关紧要。但是,在设置任何参数之前,接口可能需要知道它使用了哪种加密机制。