這裏是我的代碼不能恢復文件:解密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
我試過我的程序,但輸出與原始文件不同。
任何解決方案?
請勿使用ECB。 http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation – SLaks 2012-08-10 21:20:28
我的文件正在使用ECB,我需要解密它。 – 2012-08-10 21:21:12
您似乎試圖將文件的前16個字節用作IV,但ECB不使用IV。 – 2012-08-10 21:45:59