我正在尝试使用ECB模式使用OpenSSL库创建AES加密的示例。很难找到任何有关ECB的文档,所以我举了一个使用CBC模式的代码示例,并尝试针对ECB对其进行修改。我摆脱了ECB中未包括的内容,例如初始化 vector ,并尝试尽我所能修改代码。完成后,我在编译后遇到了一些问题:
AES-256-ECB-Encryption.cpp: In function ‘int encrypt(unsigned char*, int, unsigned char*, unsigned char*)’:
AES-256-ECB-Encryption.cpp:27:63: error: too few arguments to function ‘int EVP_EncryptInit_ex(EVP_CIPHER_CTX*, const EVP_CIPHER*, ENGINE*, const unsigned char*, const unsigned char*)’
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key))
该错误表明我在int加密函数中无法使用的参数太少。我的int解密功能也有此错误。我想知道这里是否有人可以帮助我澄清问题。我知道ECB模式附带的漏洞,但是我仍然想熟悉它。另外,我知道密钥不应该硬编码,但是我只想运行一个示例以确保我有正确的主意。我正在从OpenSSL的libcrypto库中使用EVP对称加密和解密。如果那很重要,我在Ubuntu 16.0.4上。如果有人可以阐明我的问题或提供有关ECB的更多文档,将不胜感激。
谢谢
这是其余的代码:
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
* In this example we are using 256 bit AES (i.e. a 256 bit key).
*/
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key))
handleErrors();
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
/* Initialise the decryption operation. IMPORTANT - ensure you use a key
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
*/
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key))
handleErrors();
/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
int main (void)
{
/* A 256 bit key */
unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
/* Message to be encrypted */
unsigned char *plaintext =
(unsigned char *)"This is a test.";
/* Buffer for ciphertext. Ensure the buffer is long enough for the
* ciphertext which may be longer than the plaintext, dependant on the
* algorithm and mode
*/
unsigned char ciphertext[128];
/* Buffer for the decrypted text */
unsigned char decryptedtext[128];
int decryptedtext_len, ciphertext_len;
/* Initialise the library */
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
/* Encrypt the plaintext */
ciphertext_len = encrypt (plaintext, strlen ((char *)plaintext), key, ciphertext);
/* Do something useful with the ciphertext here */
printf("Ciphertext is:\n");
BIO_dump_fp (stdout, (const char *)ciphertext, ciphertext_len);
/* Decrypt the ciphertext */
decryptedtext_len = decrypt(ciphertext, ciphertext_len, key,
decryptedtext);
/* Add a NULL terminator. Expecting printable text */
decryptedtext[decryptedtext_len] = '\0';
/* Show the decrypted text */
printf("Decrypted text is:\n");
printf("%s\n", decryptedtext);
/* Clean up */
EVP_cleanup();
ERR_free_strings();
return 0;
}
最佳答案
该函数有5个参数,将NULL
传递给iv
参数。
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key, NULL))
从docs:
int EVP_EncryptInit_ex( EVP_CIPHER_CTX *ctx,
const EVP_CIPHER *type,
ENGINE *impl,
unsigned char *key,
unsigned char *iv);
当旧的绝地大师@zaph冷静地指示@ akfe79“信任错误消息”。
关于c++ - 使用ECB操作模式的OpenSSL库进行AES-256加密,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38342326/