Я работаю над этим в течение нескольких часов, сначала я хотел бы объяснить, что я пытаюсь сделать; Шифровать/дешифровать файлы на C с помощью OpenSSL. Проблема, похоже, возникает в методе дешифрования. Документация малочисленна, и я собрал ее вместе со ссылкой на книги и т. Д.Расшифрованный файл по-прежнему зашифрован
Когда код запущен, на выходе нет проблем. При открытии файла, предназначенного расшифрованный его на самом деле до сих пор зашифрованное ...
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <openssl/aes.h>
#include <openssl/bio.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include <string.h>
#include <openssl/evp.h>
#include <sys/stat.h>
#include <errno.h>
/*=========================================================================|
Encryption Method below, This Will Take The File And Use The Keys And IV
To Encrypt The Data/File. We Use Long Here Because Int Only Is Good For <2GB
Data Size And Will Get ERRORS If File Is Larger Than >= 2GB Which Is As Common
as gold-diggers. */
void encrypt_process()
{
unsigned char key[] = "badidea";
unsigned char vec[] = "again";
FILE *Input_File;
FILE *Output_file;
Input_File =fopen("french.txt", "rb");
Output_file = fopen("ult.txt", "wb");//File to be written; cipher text
fseek(Input_File, 0, SEEK_END);
unsigned long len = (unsigned long)ftell(Input_File); // use long as the file if >2GB will blow past int
printf("length of the file is : %lu", len);
unsigned long outLen1 = 0;
unsigned long outLen2 = 0;
unsigned char *indata = malloc(len);
unsigned char *outdata = malloc(len);
fread(indata,sizeof(char),len, Input_File);//Read Entire File
/*-----------------------------------------------------------------*\
<||| Set Up Encryption As Defined in OPENSSH using their syntax etc.>>>
*-----------------------------------------------------------------*/
//initiating cipher
EVP_CIPHER_CTX ctx;
EVP_EncryptInit(&ctx,EVP_aes_128_cbc(),key,vec);
EVP_EncryptUpdate(&ctx,outdata,&outLen1,indata,len);
EVP_EncryptFinal(&ctx,outdata + outLen1,&outLen2);
fwrite(outdata,sizeof(char),outLen1 + outLen2,Output_file);
fclose(Input_File); // free all pointers and clean up
fclose(Output_file);
Input_File = NULL;
printf("\n Encryption Process Complete");
}
/*=========================================================================|
Decryption Method below, This Will Take The File And Use The Keys And IV
To Decrypt The Data/File. We Use Long Here Because Int Only Is Good For <= 2GB
Data Size And Will Get ERRORS If File Is Larger Than 2GB Which Is As Common
as gold-diggers */
void decrypt_process()
{
unsigned char key[] = "badidea";
unsigned char vec[] = "again";
FILE *Input_File;
FILE *Output_file;
Input_File =fopen("ult.txt", "rb");
Output_file = fopen("claro.txt", "wb");
fseek(Input_File, 0, SEEK_END);
unsigned long len = (unsigned long)ftell(Input_File); // use long as the file if >2GB will blow past int
printf("length of the file is : %lu", len); //xcode underlines this?
unsigned long outLen1 = 0;
unsigned long outLen2 = 0;
unsigned char *indata = malloc(len);
unsigned char *outdata = malloc(len);
fread(indata,sizeof(char),len, Input_File);//Read Entire File
/*-----------------------------------------------------------------*\
<||| Set Up Decryption As Defined in OPENSSH using their syntax etc.>>>
*-----------------------------------------------------------------*/
//initiating decrypt
EVP_CIPHER_CTX ctx;
EVP_DecryptInit(&ctx,EVP_aes_128_cbc(),key,vec);
EVP_DecryptUpdate(&ctx,outdata,&outLen1,indata,len);
EVP_DecryptFinal(&ctx,outdata + outLen1,&outLen2);
fwrite(outdata,sizeof(char),outLen1 + outLen2,Output_file);
fclose(Input_File); // free all pointers and clean up
fclose(Output_file);
Input_File = NULL;
printf("\n Decryption Process Complete");
}
// main entry point
int main(int argc, const char * argv[])
{
char option[5]; //local buffer to get input
printf("\n **********Welcome To Encryption And Decryption Services!");
printf("****** \n Type e for ecryption or d for decryption (more options later) ");
scanf("%s", option); // should use fgets
if(strcmp(option, "e")== 0){
printf("********* Encryption Process Initiated \n What File Do You Want To Encrypt? Complete Path Needed");
encrypt_process(); // our encryption method
}
else if(strcmp(option,"d")== 0){
printf("********* Decryption Process Initiated");
decrypt_process();
}
return 0;
}
Вы следовали этой https: // вика .openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption или что-то еще –
Последующие биты и куски различной документации до тех пор, пока я не собрал их вместе. Когда шифрование работало, я был взволнован и сделал обратный метод, пытаясь дешифровать, но не смог, я действительно Не понимаю, где я ошибаюсь. У меня нет ошибок для выхода из либо .. – BinaryMind