For the past couple of days, I've been experimenting with Blowfish
encrypting/decrypting with Crypto++ 5.2.1. I'm having a bit of a problem
that maybe some of you have come across before. I'm hoping that maybe
someone can shed some light as to what I'm doing wrong and/or maybe
suggest a better approach to what I'm trying to accomplish.
I'm trying to encrypt some fairly large, binary files. As you can see
from the short test program below, it's based off the sample code that
comes with Crypto++. I encrypt a large file (~20MB) and then decrypt it.
I then manually compare the original and decrypted files. It always
seems that the last few bytes (it varies depending on the size) of the
decrypted file aren't correct. I'm sure this has something to do with
the final block not being a valid size. But I'm not sure how to deal
with it.
Any thoughts or suggestions?
-Shane
----CODE STARTS HERE----
#include <fstream>
#include "crypto521/cryptlib.h"
#include "crypto521/modes.h"
#include "crypto521/blowfish.h"
#include "crypto521/files.h"
#include "crypto521/hex.h"
using namespace CryptoPP;
static char mykey[] = "testkey";
void Encode() {
std::ifstream infile("orig.bin", std::ios::binary);
std::ofstream outfile("enc.bin", std::ios::binary);
byte inbuffer[1024];
byte outbuffer[1024];
int read = 0;
ECB_Mode<Blowfish>::Encryption enc((byte *)mykey, strlen(mykey));
int oldpos = 0;
while (infile.good() && !(infile.eof())) {
memset(inbuffer, 0, 1024);
memset(outbuffer, 0, 1024);
infile.read((char *)inbuffer, 1024);
int read = infile.gcount();
enc.ProcessData(outbuffer, inbuffer, read);
outfile.write((char *)outbuffer, read);
}
outfile.close();
infile.close();
}
void Decode() {
std::ifstream infile("enc.bin", std::ios::binary);
std::ofstream outfile("dec.bin", std::ios::binary);
byte inbuffer[1024];
byte outbuffer[1024];
int read = 0;
ECB_Mode<Blowfish>::Decryption dec((byte *)mykey, strlen(mykey));
while (infile.good() && !(infile.eof())) {
memset(inbuffer, 0, 1024);
memset(outbuffer, 0, 1024);
infile.read((char *)inbuffer, 1024);
int read = infile.gcount();
dec.ProcessData(outbuffer, inbuffer, read);
outfile.write((char *)outbuffer, read);
}
outfile.close();
infile.close();
}
int main(int argc, char **argv) {
Encode();
Decode();
}