0

这是我的代码:

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

首先,我使用 openssl 创建了一个 AES 加密文件: openssl enc -e -aes-192-ecb -in C:/sth.txt -out C:/sth.txt.aes 我尝试了我的程序,但输出与原始文件不同。

有什么解决办法吗?

4

1 回答 1

0

我建议您使用可移植库(例如Crypto++Botan)在 C++ 中进行加密。这些图书馆备受推崇,并拥有良好的社区支持。

于 2012-08-10T21:29:09.437 回答