2017-01-08 19 views
0

私は数時間この作業を行っています。まずは何をしようとしているのか説明します。 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; 
} 
+0

に設定されている

unsigned char key[32] = "badidea"; unsigned char vec[16] = "again"; 

://ウィキ

は、これらの配列に明示的なサイズを与えます.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryptionその他のもの –

+0

暗号化がうまくいってから、復号化を試みる逆の方法を作りましたが、失敗しました私はどこに間違っているのか理解していないいずれか.. – BinaryMind

答えて

0

ここでの最初の主な問題はエラーチェックをしていないことです。だから事がうまくいかないとき、あなたは知る方法がありません。

すべてのI/Oルーチン(fopenfreadfwritefseekは)perrorを使用して印刷されたエラーコードとエラーをチェックする必要があります。

同様に、すべてのOpenSSL関数もエラーをチェックする必要があります。 ERR_error_stringおよびERR_get_error関数は、エラーの内容を通知します。エラー文字列を読み込むには、プログラムの最初にに電話する必要があります。

あなたの問題を引き起こす原因は2つあります。

まず、入力ファイル(暗号化ルーチンと復号化ルーチンの両方)から読み込むと、すでにファイルの最後にあるので、何も読み込まない。読み込む前に、ファイルポインタをファイルの先頭にリセットする必要があります。

fseek(Input_File, 0, SEEK_SET); 
    fread(indata,sizeof(char),len, Input_File);//Read Entire File 

他の問題は、あなたの鍵のサイズとIVです:

unsigned char key[] = "badidea"; 
    unsigned char vec[] = "again"; 

明示的なサイズは、これらの配列のために指定されていないので、それぞれでその初期化子、8と6つのバイトと同じくらい大きな存在この場合(文字列内の各文字に1つプラスnul終端バイトに1つ) AES-128はキーに32バイト、IVに16バイト必要です。これらの配列のサイズが十分でないため、暗号化/復号化ルーチンはこれらの配列の最後を読み取って、undefined behaviorとなります。あなたはこのHTTPSに従わなかったこれらの各変数は、初期化子を持っているので、明示的に初期化されていない残りのバイトは0

+0

ありがとう、私はこれを今実装し、あなたに連絡します。 – BinaryMind

+0

も無料(メンバー)。メンバーの価値は何ですか? –

+0

お詫び申し上げます、他のコードもコメントアウトしていますが、この文章では非関連であるため、この記事のコードは削除されています。同じエラーと結果を出す別のバージョンのテストでは、以前は構造体にVARSしかありませんでした。 – BinaryMind

関連する問題