मेरे पास निम्न क्वेरी है। क्या कोई भी मुझे समाधान का सुझाव दे सकता है।फ़ाइल डिक्रिप्शन के दौरान "EVP_DecryptFInal_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;
}
धन्यवाद।
सीबीसी मोड केवल गोपनीयता प्रदान करता है, और आपको आमतौर पर सीबीसी मोड का सुरक्षित उपयोग करने के लिए एक मैक जोड़ना होगा। आपको शायद प्रमाणीकृत एन्क्रिप्शन का उपयोग करना चाहिए क्योंकि यह * दोनों * गोपनीयता और प्रामाणिकता प्रदान करता है। ओपनएसएसएल विकी पर [ईवीपी प्रमाणीकृत एन्क्रिप्शन और डिक्रिप्शन] देखें (https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption)। – jww
अपना कोड सरलीकृत करें। सभी फाइल सिस्टम पढ़ने के साथ दूर करो। एक फ़ाइल एन्क्रिप्ट करें और उस फ़ाइल को डिक्रिप्ट करने का प्रयास करें। तब डिबगिंग बहुत आसान है। – adlag
उत्तर के लिए धन्यवाद। क्या आपका मतलब है कि प्रोग्राम से सिस्टम कॉल का उपयोग? – Sai