使用OpenSSL库中的ECB模式进行AES-256加密

4

我试图使用OpenSSL库和ECB模式创建AES加密示例。在ECB模式下很难找到任何文档,所以我拿了一个使用CBC模式的代码示例并尝试将其修改为ECB模式。我去掉了ECB中不包含的初始化向量,并尽力修改了代码。完成后编译时遇到了一些问题:

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 encrypt函数中参数太少。同时,在int decrypt函数中也存在此错误。我想知道是否有人能帮我澄清问题。我知道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;
}

1
有趣的组合,不安全的ECB模式和过度的AES-256。不要使用ECB模式,它是不安全的,请参见ECB模式,向下滚动到企鹅部分。但你知道这一点并且不在意吗? - zaph
2
错误信息显示您向EVP_EncryptInit_ex传递了4个参数,而应该传递5个参数。 - iRove
是的,我明白了,这解决了问题。在我去掉iv指针后,我完全忽略了它,我应该在那里放一个Null。谢谢。此外,我只是为了学习而这样做,zaph。我意识到它的缺陷,但我仍然想更多地了解ECB包括的所有模式。 - akfe79
1个回答

1
该函数需要5个参数,将iv参数设置为NULL
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“相信错误信息”。

等一下 --- 那不是应该是“相信你的直觉”吗?这在参数列表中行不通? - David C. Rankin
我发现:“正如老绝地武士欧比旺·克诺比镇定地指导卢克要“相信原力。” :-)” - zaph
注意到了,我在回想训练时穿着防护头盔和带有爆炸防护罩的球场景。 - David C. Rankin
没错,这样修复了,现在它运行得很好。感谢您的帮助;我应该知道当我移除iv指针时,我必须放置一个占位符。 - akfe79

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接