cryptopp 加密输出文件为空

问题描述 投票:0回答:1

我正在尝试读取文件,加密内容并将其流式传输到输出文件。当我运行我的程序时,我没有收到任何错误。一切顺利完成,但目标输出文件读取 0kb,并且是空的。我尝试删除该函数的加密部分,然后简单地做了

 fs(inputFile, new FileSink(outputFile)
看看它是否会复制 - 它没有。

#include <iostream>
#include <fstream>
#include <aes.h>
#include <modes.h>
#include <filters.h>
#include <files.h>
#include <osrng.h>
#include <vector>

using namespace std;
using namespace CryptoPP;

int main() {

    // read the file and encrypt it
    ifstream inputFile("example.txt", ios::binary);
    ofstream outputFile("example2.txt", ios::binary);

    if (!inputFile.is_open()) {
        cout << "Could not open input file"; 
        return 1;
    }

    if (!outputFile.is_open()) {
        cout << "Error opening output file!" << endl;
        return 1;
    }

    cout << "(+) commencing encryption!";
    
    AutoSeededRandomPool rnd;
    byte key[AES::DEFAULT_KEYLENGTH]; // 16 bytes default
    rnd.GenerateBlock(key, sizeof(key));
    byte iv[AES::BLOCKSIZE];
    rnd.GenerateBlock(iv, sizeof(iv));

    AES::Encryption aesEnc(key, sizeof(key));
    CBC_CTS_Mode_ExternalCipher::Encryption cbcEnc(aesEnc, iv);

    if (outputFile.is_open()) {
        FileSource fs(inputFile, new StreamTransformationFilter(cbcEnc, new FileSink(outputFile)));
        fs.PumpAll();

        cout << "(+) encrypted!";
    }
    else {
        cout << "(-) failed to encrypt!";
    }

    return 0;
}
c++ crypto++
1个回答
0
投票

FileSource fs(inputFile, true, new StreamTransformationFilter(cbcEnc, new FileSink(outputFile)));

fs 缺少第二个参数。它需要一个布尔值。有趣的是它没有抛出错误。

© www.soinside.com 2019 - 2024. All rights reserved.