Reputation: 491
I'm trying to read a file, encrypt the contents and stream it to an output file. When I run my program, I get no errors. Everything completes smoothly, however the target output file reads 0kb, and is empty. I've tried removing the encryption aspect of the function and simply did
fs(inputFile, new FileSink(outputFile)
to see if it would copy over - It did not.
#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;
}
Upvotes: 0
Views: 38
Reputation: 491
FileSource fs(inputFile, true, new StreamTransformationFilter(cbcEnc, new FileSink(outputFile)));
fs was missing the second argument. It needed to take a bool. Interesting that it didn't throw an error.
Upvotes: 0