首页 > 解决方案 > ‘AES_ctr128_encrypt’ was not declared in this scope

问题描述

I am testing this OpenSSL AES-128 CTR mode encryption.

#include <openssl/aes.h>
#include <openssl/rand.h>
#include <stdio.h>
#include <string.h>

I got this error (build in C++):

../src/AES_OpenSSL_CTR.cpp:128:3: error: ‘AES_ctr128_encrypt’ was not declared in this scope.

I included openssl/aes.h already. AES_ctr128_encrypt is removed from OpenSSL 1.1.1.c? Why still not declared?

标签: c++eclipseopensslaes

解决方案


../src/AES_OpenSSL_CTR.cpp:128:3: error: ‘AES_ctr128_encrypt’ was not declared in this scope

OpenSSL 1.1.1 and 1.1.0 only provide AES_encrypt. It is a software only implementation. It operates on full blocks. You need to manage the counter, provide the increments, encrypt the counter, and XOR the plain text.

Instead of using the low level AES_encrypt, you should use the EVP interfaces. The EVP algorithms use hardware acceleration when available, and combine ciphers and modes like AES-128/CTR. The function you are looking for is EVP_aes_128_ctr(). Also see EVP Symmetric Encryption and Decryption on the OpenSSL wiki.

If you provide a Minimal, Complete, and Verifiable example, then we can say more about what you can do to fix the issue.


If you want to use AES_ctr128_encrypt, then you need to drop back to OpenSSL 1.0.2. OpenSSL 1.1.1 and 1.1.0 no longer provide it.

$ git checkout OpenSSL_1_0_2-stable
Branch 'OpenSSL_1_0_2-stable' set up to track remote branch 'OpenSSL_1_0_2-stable' from 'origin'.
Switched to a new branch 'OpenSSL_1_0_2-stable'

$ grep -IR AES_ctr128_encrypt
util/libeay.num:AES_ctr128_encrypt                      3216    EXIST::FUNCTION:AES
crypto/aes/aes.h:void AES_ctr128_encrypt(const unsigned char *in, unsigned char *out,
crypto/aes/aes_ctr.c:void AES_ctr128_encrypt(const unsigned char *in, unsigned char *out,

If you go back to OpenSSL 1.0.2, then see AES CTR 256 Encryption Mode of operation on OpenSSL.


Since you have a C++ program, you might be interested in EVP Encryption ... | C++ Programs on the OpenSSL wiki. It uses std::unique_ptr and a secure_string to manage OpenSSL resources. secure_string is a std::basic_string typedef with a secure allocator. It cleans up the OpenSSL C code nicely.


I am testing this OpenSSL AES-128 CTR mode encryption...

Yeah, so that is the problem with blogs (not to mention one from 2012). It is probably best to follow what the project says. The OpenSSL project says to use the EVP interfaces.


推荐阅读