私は数時間この作業を行っています。まずは何をしようとしているのか説明します。 OpenSSLを使用してCのファイルを暗号化/復号化する。この問題は復号化方法で発生しているようです。ドキュメンテーションが不足しており、これをまとめて書籍などに戻しています。復号化されたファイルはまだ暗号化されています
コードを実行すると、出力に問題はありません。意図した暗号化されたファイルを開くと、実際にはまだ暗号化されています...
#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;
}
に設定されている
://ウィキ
は、これらの配列に明示的なサイズを与えます.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryptionその他のもの –
暗号化がうまくいってから、復号化を試みる逆の方法を作りましたが、失敗しました私はどこに間違っているのか理解していないいずれか.. – BinaryMind