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

时间:2016-07-13 03:44:36

标签: c++ c encryption aes

我正在尝试使用ECB模式使用OpenSSL库创建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 个答案:

答案 0 :(得分:1)

该函数接受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“信任错误信息”。