如何解决这个问题;执行副总裁“DecryptFInal”ex:bad decrypt“;在文件解密期间

如何解决这个问题;执行副总裁“DecryptFInal”ex:bad decrypt“;在文件解密期间,c,encryption,openssl,C,Encryption,Openssl,我有以下问题。谁能给我一个解决方案吗 我第一次从事文件的加密和解密工作 我使用以下命令通过命令提示符对文件进行了加密: openssl enc -aes-256-cbc -in file.txt -out file.enc -k "key value" -iv "iv value" 我必须以编程方式解密它。我已经为它编写了程序,但它抛出了以下错误: ./exe_file enc_file_directory ... error: 06065064: digital envelope routi

我有以下问题。谁能给我一个解决方案吗

我第一次从事文件的加密和解密工作

我使用以下命令通过命令提示符对文件进行了加密:

openssl enc -aes-256-cbc -in file.txt -out file.enc -k "key value" -iv "iv value"
我必须以编程方式解密它。我已经为它编写了程序,但它抛出了以下错误:

./exe_file enc_file_directory
...
error: 06065064: digital envelope routines: EVP_DecryptFInal_ex: bad decrypt: evp_enc.c

下面的程序将输入作为目录路径,搜索加密文件“.enc”,并尝试将其解密并读入缓冲区

代码:

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
无效句柄错误(字符*消息)
{
{
错误打印错误fp(stderr);
printf(“%s”,msg);
中止();
}
}
无效空闲内存(char*mem)
{
if(NULL!=mem)
{
免费(mem);
mem=NULL;
}
}
/*函数来解密XML文件*/
int decryptXML(无符号字符*indata,无符号字符*outdata,int fsize)
{
int-outlen1=0,outlen2=0;
无符号字符iv[]=“b63e541bc9ece19a1339df4f8720dcc3”;
无符号字符ckey[]=“70bbc518c57acca2c2001694648c40ddaf19e3b4fe1376ad656de8887a0a5ec2”;
如果(NULL==indata)
{
printf(“输入数据为空\n”);
返回0;
}
如果(0>=fsize)
{
printf(“文件大小为零\n”);
返回0;
}
outdata=(char*)malloc(sizeof(char)*fsize*2);
执行副总裁;
EVP_CIPHER_CTX_init(&CTX);
if(!EVP_DecryptInit_ex(&ctx,EVP_aes_256_cbc(),NULL,ckey,iv))
{
EVP_密码_CTX_清理(&CTX);
handleErrors(“DInit”);
}
如果(!EVP_DecryptUpdate(&ctx,outdata,&outlen1,indata,fsize))
{
EVP_密码_CTX_清理(&CTX);
handleErrors(“DUpdate”);
}
如果(!EVP_DecryptFinal_ex(&ctx、outdata+outlen1和outlen2))
{
EVP_密码_CTX_清理(&CTX);
handleErrors(“DFinal”);
}
EVP_密码_CTX_清理(&CTX);
返回outlen1+outlen2;
}
int isDirectory(字符*路径)
{
DIR*DIR=NULL;
文件*fin=NULL,*fout=NULL;
int enc_len=0,dec_len=0,fsize=0,ksize=0;
无符号字符*indata=NULL,*outdata=NULL;
char buff[BUFFER\u SIZE],file\u path[BUFFER\u SIZE],cur\u dir[BUFFER\u SIZE];
结构方向*in_dir;
结构统计;
if(NULL==(dir=opendir(path)))
{
printf(“错误:无法打开目录%s\n”,路径);
perror(“无法打开”);
出口(1);
}
while(NULL!=(in_dir=readdir(dir)))
{
如果(!strcmp(in_dir->d_name,“.”)| |!strcmp(in_dir->d_name,“…”))
继续;
sprintf(buff,“%s/%s”,路径,在目录->文件名中);
如果(-1==stat(buff,&s))
{
佩罗(“stat”);
出口(1);
}
中频(S_ISDIR(S.st_模式))
{
isDirectory(buff);
}
其他的
{
strcpy(文件路径,buff);
if(strstr(文件路径“.enc”))
{
/*要解密的文件*/
fout=fopen(文件路径,“rb”);
fseek(fout,0L,SEEK_END);
fsize=ftell(fout);
fseek(fout,0L,SEEK_SET);
indata=(char*)malloc(fsize);
fread(indata、sizeof(字符)、fsize、fout);
如果(NULL==fout)
{
perror(“无法打开enc文件:”);
返回1;
}
dec_len=decryptXML(indata、outdata、fsize);
outdata[dec_len]='\0';
printf(“%s\n”,outdata);
财务总监(财务);
fclose(fout);
}
}
}
closedir(dir);
自由内存(outdata);
自由存储器(indata);
返回1;
}
int main(int argc,char*argv[])
{
int结果;
如果(argc!=2)
{
printf(“用法:文件的路径\n”);
返回-1;
}
错误加载加密字符串();
OpenSSL_添加_所有算法();
OPENSSL_配置(空);
/*正在检查目录是否存在*/
结果=isDirectory(argv[1]);
EVP_cleanup();
ERR_free_strings();
如果(0==结果)
返回1;
其他的
返回0;
}

谢谢。

我认为使用命令行进行加密和使用您的程序进行解密时使用的密钥和IV是不同的

请注意,当您使用“-k”(与“-k”不同)时,所提供的输入被视为密钥派生的密码。通常在这种情况下,不需要“-iv”选项,因为密钥和密码都将从“-k”选项提供的输入中派生

您的问题并不清楚,您如何确保加密和解密之间的密钥和IV相同

在我的建议中,最好使用“-K”和“-iv”选项在加密过程中显式指定密钥和iv,并将其用于解密。如果需要使用“-k”,则使用“-p”选项打印用于加密的密钥和iv,并在解密程序中使用相同的密钥和iv


有关更多详细信息,请访问

此消息
数字信封例程:EVP_decrypt final_ex:bad decrypt
,当您使用不兼容版本的openssl进行加密和解密时,也会发生此情况

我遇到的问题是,我在版本为1.1.0的Windows上加密,然后在版本为1.0.2g的通用Linux系统上解密

这不是一条非常有用的错误消息


工作解决方案: 来自@AndrewSavinykh的一个可能的解决方案,对许多人都有效(见评论):

默认摘要在这些版本之间已从md5更改为sha256。可以在命令行上将默认摘要分别指定为
-md sha256
-md md5


当您指定不正确的解密密码时,也会出现此消息(是的,很蹩脚,但从错误消息中不太明显)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/conf.h>
#include <libxml/globals.h>

void handleErrors(char *msg)
{
    {
        ERR_print_errors_fp(stderr);
        printf("%s", msg);
        abort(); 
    }
}

void freeMemory(char *mem)
{
    if (NULL != mem)
    {
        free(mem);
        mem = NULL;
    }
}

/* Function to decrypt the XML files */

int decryptXML(unsigned char *indata, unsigned char *outdata, int fsize)
{

    int outlen1 = 0, outlen2 = 0;

    unsigned char iv[] = "b63e541bc9ece19a1339df4f8720dcc3";
    unsigned char ckey[] = "70bbc518c57acca2c2001694648c40ddaf19e3b4fe1376ad656de8887a0a5ec2" ;

    if (NULL == indata)
    {
        printf ("input data is empty\n");
        return 0;
    }

    if (0 >= fsize)
    {
        printf ("file size is zero\n");
        return 0;
    }

    outdata = (char *) malloc (sizeof (char) * fsize * 2);

    EVP_CIPHER_CTX ctx;

    EVP_CIPHER_CTX_init(&ctx);

    if (! EVP_DecryptInit_ex (&ctx, EVP_aes_256_cbc(), NULL, ckey, iv))
    {
        EVP_CIPHER_CTX_cleanup(&ctx);
    handleErrors("DInit");
    }

    if (! EVP_DecryptUpdate (&ctx, outdata, &outlen1, indata, fsize))
    {
        EVP_CIPHER_CTX_cleanup(&ctx);
        handleErrors("DUpdate");
    }

    if (! EVP_DecryptFinal_ex (&ctx, outdata + outlen1, &outlen2))
    {

        EVP_CIPHER_CTX_cleanup(&ctx);
        handleErrors("DFinal");
    }

    EVP_CIPHER_CTX_cleanup(&ctx);

    return outlen1+outlen2;

}

int isDirectory(char *path)
{
    DIR *dir = NULL;
    FILE *fin = NULL, *fout = NULL;
    int enc_len = 0, dec_len = 0, fsize = 0, ksize = 0;
    unsigned char *indata = NULL, *outdata = NULL;
    char buff[BUFFER_SIZE], file_path[BUFFER_SIZE], cur_dir[BUFFER_SIZE];

    struct dirent *in_dir;
    struct stat s;

    if (NULL == (dir = opendir(path)))
    {
        printf ("ERROR: Failed to open the directory %s\n", path);
        perror("cannot open.");
        exit(1);
    }

    while (NULL != (in_dir = readdir(dir)))
    {

        if (!strcmp (in_dir->d_name, ".") || !strcmp(in_dir->d_name, ".."))
            continue;

        sprintf (buff, "%s/%s", path, in_dir->d_name);

        if (-1 == stat(buff, &s))
        {
            perror("stat");
            exit(1);
        }

        if (S_ISDIR(s.st_mode))
        {

            isDirectory(buff);
        }
        else
        {
            strcpy(file_path, buff);

            if (strstr(file_path, ".enc"))
            {

                /* File to be decrypted */

                fout = fopen(file_path,"rb"); 

                fseek (fout, 0L, SEEK_END);
                fsize = ftell(fout);
                fseek (fout, 0L, SEEK_SET);

                indata = (char*)malloc(fsize);

                fread (indata, sizeof(char), fsize, fout);

                if (NULL == fout)
                {
                    perror("Cannot open enc file: ");
                    return 1;
                }


                dec_len = decryptXML (indata, outdata, fsize);
                outdata[dec_len] = '\0';
                printf ("%s\n", outdata);
                fclose (fin);
                fclose (fout);

            }
        }
    }



    closedir(dir);
    freeMemory(outdata);
    freeMemory(indata);

    return 1; 
}


int main(int argc, char *argv[])
{
    int result;

    if (argc != 2)
    {
        printf ("Usage: <executable> path_of_the_files\n");
        return -1;
    }

    ERR_load_crypto_strings();
    OpenSSL_add_all_algorithms();
    OPENSSL_config(NULL);

    /* Checking for the directory existance */

    result = isDirectory(argv[1]);

    EVP_cleanup();
    ERR_free_strings();

    if (0 == result)
        return 1;
    else
       return 0;
}
echo -ne "\x32\xc8\xde\x5c\x68\x19\x7e\x53\xa5\x75\xe1\x76\x1d\x20\x16\xb2\x72\xd8\x40\x87\x25\xb3\x71\x21\x89\xf6\xca\x46\x9f\xd0\x0d\x08\x65\x49\x23\x30\x1f\xe0\x38\x48\x70\xdb\x3b\xa8\x56\xb5\x4a\xc6\x09\x9e\x6c\x31\xce\x60\xee\xa2\x58\x72\xf6\xb5\x74\xa8\x9d\x0c" | openssl aes-128-cbc -d -K 31323334353637383930313233343536 -iv 79169625096006022424242424242424 | od -t x1
bad decrypt
140181876450560:error:06065064:digital envelope 
routines:EVP_DecryptFinal_ex:bad decrypt:../crypto/evp/evp_enc.c:535:
0000000 2f 2f 07 02 54 0b 00 00 00 00 00 00 04 29 00 00
0000020 00 00 04 a9 ff 01 00 00 00 00 04 a9 ff 02 00 00
0000040 00 00 04 a9 ff 03 00 00 00 00 0d 79 0a 30 36 38
echo -ne "\x32\xc8\xde\x5c\x68\x19\x7e\x53\xa5\x75\xe1\x76\x1d\x20\x16\xb2\x72\xd8\x40\x87\x25\xb3\x71\x21\x89\xf6\xca\x46\x9f\xd0\x0d\x08\x65\x49\x23\x30\x1f\xe0\x38\x48\x70\xdb\x3b\xa8\x56\xb5\x4a\xc6\x09\x9e\x6c\x31\xce\x60\xee\xa2\x58\x72\xf6\xb5\x74\xa8\x9d\x0c" | openssl aes-128-cbc -d -K 31323334353637383930313233343536 -iv 79169625096006022424242424242424 -nopad | od -t x1
0000000 2f 2f 07 02 54 0b 00 00 00 00 00 00 04 29 00 00
0000020 00 00 04 a9 ff 01 00 00 00 00 04 a9 ff 02 00 00
0000040 00 00 04 a9 ff 03 00 00 00 00 0d 79 0a 30 36 38
0000060 30 30 30 34 31 33 31 2f 2f 2f 2f 2f 2f 2f 2f 2f
0000100
EVP_CIPHER_CTX_set_padding(context, 0);