0

Here is my code:

/* 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);
 }

First of all, I created a AES-Encrypted file by using openssl: openssl enc -e -aes-192-ecb -in C:/sth.txt -out C:/sth.txt.aes I tried my program, but the output wasn't same as the original file.

Any solutions?

rmtheis
  • 5,992
  • 12
  • 61
  • 78
Kong Chun Ho
  • 268
  • 4
  • 14
  • 2
    Don't use ECB. http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation – SLaks Aug 10 '12 at 21:20
  • My file is using ECB, and i need to decrypt it. – Kong Chun Ho Aug 10 '12 at 21:21
  • 1
    You seem to be trying to use the first 16 bytes of the file as an IV, but ECB doesn't use an IV. – Jerry Coffin Aug 10 '12 at 21:45
  • i see, but i still can't get the (decrypted) original file if i use inFile2(which reads the whole file and throw to aes_ecb_decrypt, without ignoring the IV) – Kong Chun Ho Aug 10 '12 at 21:51
  • You probably need to create the `openssl` specific key derivation method as well. You cannot just use a password directly as a key. See also http://stackoverflow.com/questions/11783062/how-to-decrypt-an-encrypted-file-in-java-with-openssl-with-aes for my Java solution (in this case for CBC, but that should not matter). – Maarten Bodewes Aug 11 '12 at 11:15
  • Whats the purpose of that Java code? I still can't get your meaning. – Kong Chun Ho Aug 11 '12 at 18:43

1 Answers1

0

I would suggest that you use a portable library such as Crypto++ or Botan for cryptography in C++. Those libraries are well-regarded and have good communities for support.