2012-08-10 50 views
0

這裏是我的代碼不能恢復文件:解密AES-196-ECB文件,試圖對文件進行加密,但是當它解密

/* DECRYPTION */ 
int i; 
aes_decrypt_ctx ctx[1]; 
//aes_encrypt_ctx ctx[1]; 
unsigned char iv[16]; 
unsigned char inBuffer[200], outBuffer[200]; 
FILE *inFile1=fopen("C:/sth.txt.aes", "rb"); /* used to read IV ONLY */ 
FILE *inFile2=fopen("C:/sth.txt.aes", "rb"); /* used to read the whole file, included IV */ 
FILE *outFile=fopen("C:/sth.txt", "wb"); 
if (inFile1 != NULL) { 
    lbl_status->Text+="\r\nFinding IV(first 16 byte) of the file..."; 
    /* read initialization vector from file */ 
    if(fread(iv, 1, 16, inFile1) >= 16) { 
    lbl_status->Text+="\r\nFound IV(first 16 byte) in the file..."; 
    fclose(inFile1); 
    aes_decrypt_key192((unsigned char*)MStringToCCS(decryptsecret), ctx); 

    // Start decrypting 
    lbl_status->Text+="\r\nStart decrypting..."; 
    while((i=fread(inBuffer, 1, sizeof(inBuffer), inFile2)) > 0) { 
     aes_ecb_decrypt(inBuffer, outBuffer, i, ctx); 
     fwrite(outBuffer, 1, i, outFile); 
    } 
    fclose(inFile2); 
    fclose(outFile); 
    }else{ 
    /* error: file doesn't even contain an initialisation vector */ 
    lbl_status->Text+="\r\nError: IV(first 16 byte) not found."; 
    MessageBox::Show("Could not find the IV.", "Error", MessageBoxButtons::OK, MessageBoxIcon::Error); 
    fclose(inFile1); 
    fclose(inFile2); 
    fclose(outFile); 
    } 
}else{ 
    lbl_status->Text+="\r\nError: Could not open the file."; 
    MessageBox::Show("Error for opening the file.", "Error", MessageBoxButtons::OK, MessageBoxIcon::Error); 
    fclose(inFile1); 
    fclose(inFile2); 
    fclose(outFile); 
} 

首先,我創建了一個AES-使用openssl加密文件: openssl enc -e -aes-192-ecb -in C:/sth.txt -out C:/sth.txt.aes 我試過我的程序,但輸出與原始文件不同。

任何解決方案?

+2

請勿使用ECB。 http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation – SLaks 2012-08-10 21:20:28

+0

我的文件正在使用ECB,我需要解密它。 – 2012-08-10 21:21:12

+1

您似乎試圖將文件的前16個字節用作IV,但ECB不使用IV。 – 2012-08-10 21:45:59

回答

0

我建議你在C++中使用便攜庫,例如Crypto++Botan用於加密。這些圖書館受到好評,並有良好的社區支持。

+0

我明白了,我可以讓它成爲一個獨立的exe文件嗎? 這意味着我可以在單個EXE中運行,而不是在啓動程序時需要DLL。 – 2012-08-10 21:36:05

+0

靜態鏈接openssl怎麼樣?或者我錯過了什麼? – 2012-08-11 11:16:38

+0

如何靜態連接cryptopp? – 2012-08-11 15:11:35