我是 C 程序新手。当前使用 EVP 我想解密文本。当我解密时,我得到 p>
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/