sourcecode

'EVP_Decrypt'를 해결하는 방법FInal_ex: 파일 복호화 중 잘못된 복호화"

copyscript 2022. 7. 26. 23:32
반응형

'EVP_Decrypt'를 해결하는 방법FInal_ex: 파일 복호화 중 잘못된 복호화"

저는 다음과 같은 질문이 있습니다.해결책을 제안해 주실 분 있나요?

저는 처음으로 파일의 암호화와 복호화를 하고 있습니다.

다음 명령을 사용하여 명령 프롬프트를 통해 암호화된 파일이 있습니다.

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"을 검색하여 버퍼로 읽혀진 파일의 복호화를 시도합니다.

코드:

#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;
}

감사합니다.

이 메시지digital envelope routines: EVP_DecryptFInal_ex: bad decrypt또한 호환되지 않는 버전의 openssl을 사용하여 암호화 및 복호화할 때도 발생할 수 있습니다.

문제는 버전 1.1.0이 설치된 Windows에서 암호화하고 1.0.2g이 설치된 일반 Linux 시스템에서 암호를 해독하는 것이었습니다.

이것은 그다지 도움이 되지 않는 에러 메시지입니다!


유효한 솔루션:

@AndrewSavinkh가 많은 사람에게 효과가 있는 솔루션(댓글 참조):

이러한 버전 간에 기본 다이제스트가 md5에서 sha256으로 변경되었습니다.명령줄의 기본 다이제스트를 다음과 같이 지정할 수 있습니다.-md sha256또는-md md5각각 다음과 같다.

올바른 바이너리 키(-K)를 가지고 있을 때 openssl 명령줄 인터페이스를 사용하는 동안 유사한 오류 응답이 발생했습니다.옵션 "-nopad"로 문제가 해결되었습니다.

에러를 생성하는 예:

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

커맨드 라인을 사용한 암호화와 당신의 프로그램을 사용한 복호화에 사용되는 Key와 IV가 같지 않다고 생각합니다.

"-k" (-K"와는 다른)를 사용하는 경우 입력된 정보는 키가 파생된 비밀번호로 간주됩니다.일반적으로 이 경우 키와 비밀번호는 모두 "k" 옵션과 함께 제공된 입력에서 파생되므로 "-iv" 옵션은 필요하지 않습니다.

암호화와 복호화 사이에서 Key와 IV가 동일한지 확인하는 방법은 질문에서 명확하지 않습니다.

내 제안으로는 암호화 중에 키와 IV를 명시적으로 지정하고 암호 해독에 동일한 옵션을 사용하려면 "-K" 및 "-iv" 옵션을 사용하는 것이 좋습니다."-k"를 사용해야 하는 경우 "-p" 옵션을 사용하여 암호화에 사용되는 키와 iv를 인쇄하고 암호 해독 프로그램에서 동일한 키를 사용합니다.

상세한 것에 대하여는, https://www.openssl.org/docs/manmaster/apps/enc.html 를 참조해 주세요.

오류: "Bad encrypt/decrypt" "gitencrypt_smudge: FAILURE: openssl error decrypting file"

각 버전 및 시나리오에 따라 openssl에서 발생하는 다양한 오류 문자열이 있습니다.openssl 관련 문제가 발생했을 때 사용하는 체크리스트는 다음과 같습니다.

  1. openssl은 같은 키(+salt)를 사용하여 암호화/복호화할 수 있으며 암호화만 가능합니다.
  2. openssl 버전(암호화/복호화에 사용)이 호환되는지 확인합니다.예를 들어 openssl에서 사용되는 해시가 버전 1.1.0에서 MD5에서 SHA256으로 변경되었습니다.이로 인해 동일한 비밀번호와는 다른 키가 생성됩니다.수정: 1.1.0에서 "-md md5"를 추가하여 하위 버전에서 데이터를 해독하고 하위 버전에서 "-md sha256"을 추가하여 1.1.0에서 데이터를 해독합니다.

  3. 시스템에 단일 openssl 버전이 설치되어 있는지 확인합니다.여러 버전이 동시에 설치되어 있는 경우(내 컴퓨터에는 다음 버전이 설치되어 있습니다).- 'Libre'SSL 2.6.5' 및 'openssl 1.1.1d')는 PATH 변수에 원하는 항목만 표시되도록 합니다.

이 메시지는 잘못된 복호화 패스워드를 지정한 경우에도 발생할 수 있습니다(예, 레임입니다만, 에러 메시지로부터 이것을 인식하는 것은 그다지 명확하지 않습니다).

명령줄을 사용하여 보조 도구의 최근 DataBase 백업을 해독하던 중 갑자기 이 문제가 발생했습니다.

마지막으로, 10분간의 슬픔과 이 질문/답변 내용을 읽고 난 후, 비밀번호가 다르고 올바른 비밀번호로 모든 것이 잘 작동한다는 것을 기억했습니다.

내 경우, 서버는 패딩을 비활성화한 상태로 암호화되어 있었습니다.하지만 클라이언트는 패딩을 활성화한 상태에서 암호를 해독하려고 했습니다.

EVP_CIPER* 를 사용하고 있는 동안은, 디폴트로 패딩은 유효하게 되어 있습니다.명시적으로 비활성화하려면

EVP_CIPHER_CTX_set_padding(context, 0);

패딩 옵션이 일치하지 않는 것도 한 가지 이유일 수 있습니다.

언급URL : https://stackoverflow.com/questions/34304570/how-to-resolve-the-evp-decryptfinal-ex-bad-decrypt-during-file-decryption

반응형