c - 数字信封例程:EVP_DecryptFinal_ex:wrong final block length:evp_enc in C program

标签 c encryption openssl

我是 C 程序新手。当前使用 EVP 我想解密文本。当我解密时,我得到

error as "digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:evp_enc.c:533:IOT/Abort trap(coredump)".

谁能告诉我问题出在哪里吗?

以及如何解决该问题

这是代码:

#include <openssl/aes.h>
#include <openssl/engine.h>
#include <string.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>

 char b64[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


int main (int argc,unsigned char *input[])
{

  char textToDecrypt[120];  
 unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
 unsigned char *iv = (unsigned char *)"01234567890123456";
 unsigned char *ciphertext =(unsigned char *)input[1];
 unsigned char decryptedtext[128];

int decryptedtext_len,ciphertext_len;

unsigned char ciphertext1[128];

static char dec_temp[250];
  ERR_load_crypto_strings();
  OpenSSL_add_all_algorithms();
  OPENSSL_config(NULL);


b64_decode(ciphertext,dec_temp);

strcpy((char*)ciphertext1,dec_temp);

ciphertext_len= strlen(ciphertext1);

decryptedtext_len = decrypt(ciphertext1, ciphertext_len, key, iv,
    decryptedtext);


  decryptedtext[decryptedtext_len] = '\0';
  printf("Decrypted text is:\n");
  printf("%s\n", decryptedtext);

  EVP_cleanup();

  ERR_free_strings();

  return (0);

}


void handleErrors(void)
{
  ERR_print_errors_fp(stderr);
  abort();
}


int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
  unsigned char *iv, unsigned char *plaintext)
{
 EVP_CIPHER_CTX *ctx;

  int len;

  int plaintext_len;


  if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();


  EVP_CIPHER_CTX_set_padding(ctx, 0);
  if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))

        handleErrors();


  if(1 != EVP_DecryptUpdate(ctx,(unsigned char *) plaintext, &len, ciphertext, ciphertext_len))
    handleErrors();
  plaintext_len = len;


  if(1 != EVP_DecryptFinal_ex(ctx,(unsigned char *) plaintext + len, &len)) handleErrors();
  plaintext_len += len;



  EVP_CIPHER_CTX_free(ctx);
  plaintext += len;
  return plaintext_len;
}

void decodeblock(unsigned char in[], char *dec_temp) {
    unsigned char out[4];
    out[0] = in[0] << 2 | in[1] >> 4;
    out[1] = in[1] << 4 | in[2] >> 2;
    out[2] = in[2] << 6 | in[3] >> 0;
    out[3] = '\0';
    strncat(dec_temp, out, sizeof(out));
    }

int b64_decode(char *textToDecrypt, char *dec_temp)  //base64 decode
    {
        int c, phase, i;
        unsigned char in[4];
        char *p;

        dec_temp[0] = '\0';
        phase = 0; i=0;
        while(textToDecrypt[i])
        {
            c = (int) textToDecrypt[i];
            if(c == '=')
            {
            decodeblock(in, dec_temp); 
            break;
            }
            p = strchr(b64, c);
            if(p)
            {
            in[phase] = p - b64;
            phase = (phase + 1) % 4;
            if(phase == 0) {
            decodeblock(in, dec_temp);
            in[0]=in[1]=in[2]=in[3]=0;
      }
    }
    i++;
  }
  return 0;
}

最佳答案

加密值是字节,而不是字符(尽管它们在 C 中具有相同的数据类型)。您正在对密码“文本”调用 strlen 但 strlen 不合适。它会告诉您第一个零在密文段开始的位置(甚至可能不在密文本身内)。

您需要捕获从 b64_decode 写入的真实字节数,并将其作为 ciphertext_len 传递。

关于c - 数字信封例程:EVP_DecryptFinal_ex:wrong final block length:evp_enc in C program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39101228/

相关文章:

c - 从C中的函数获取返回值

创建插件管理系统

c - 为什么 `double i = 3.3, j = 1.1; int k = i/j; printf("%d\n", k);` 得到 2?

java - Blowfish CBC算法空间Padding如何实现

c++ - 如何解密被 CryptProtectData 函数加密的数据?

c++ - 为什么每个架构的 opensslconf.h 不同?

c - 位移位与数组索引,更适合 32 位 MCU 上的 uart 接口(interface)

python - PyECC 示例 - Python 椭圆曲线密码学

mysql - 使用 openSSL 构建 MySQL 5.6.34

访问受 PKI 保护的 php 站点 (HTTPS) 的 Python 示例