c++ - Use of Pipelines to encrypt a file -
i have startet working crypto++ , have question piplenes , how use them encrypt file.
i want use aes encrypt file.
1.)would enough do:
eax<aes>::encryption encryptor; encryptor.setkeywithiv(derived.data(), 16, ivb, ivb.size()); filesource f("source", new authenticatedencryptionfilter(encryptor,new filesink("deststination")));
2.) if have huge input file, approach automaticly encrypt files in blocks?
3.) automaticly create output file if not there?
edit:
ok, got wok approch.
the 2.) question remains , have new one:
can tell skip first 24 bytes of file?
eax<aes>::encryption encryptor; encryptor.setkeywithiv(derived.data(), 16, ivb, ivb.size()); filesource f("source", new authenticatedencryptionfilter(encryptor,new filesink("deststination")));
close. authenticatedencryptionfilter
coerced bool pumpall
parameter of filesink
. need:
filesource f("source", true, new authenticatedencryptionfilter(encryptor,new filesink("deststination")));
also see filesource on crypto++ wiki. , filesource class reference manual might of interest, too.
if have huge input file, approach automatically encrypt files in blocks?
yes. internally, crypto++ "block" or "chunk" processing in 4096-bytes, iirc. recent discussion occurred on mailing list @ ios locking during encryption.
a program allows blocking provided in post. can use throttle processing, place update progress bar or yield processor, if needed. reproduced below.
would automatically create output file if not there?
yes. filesource
std::ifstream
wrapper, while filesink
std::ofstream
wrapper.
again, here wiki pages:
can tell skip first 24 bytes of file?
yes. in case, use bool pumpall
, set false
. like:
filesource fs("source", false, new authenticatedencryptionfilter(...)); fs.skip(24); size_t remaining = <size of file>; size_t block_size = 512; while(remaining && !fs.sourceexhausted()) { const unsigned int req = stdmin(remaining, block_size); fs.pump(req); fs.flush(false); remaining -= req; }
or, can:
filesource fs("source", false, new authenticatedencryptionfilter(...)); fs.skip(24); fs.pumpall();
also see filesource class reference in manual. skip
part of bufferedtransformation
; , pumpall
part of source
.
there wiki pages covering eax mode , authenticated {en|de}cryption filters. see:
there's page on using java-like init/update/final at:
the program below uses cfb_mode<aes>
, easy enough swap in cipher , mode. demonstrates how place objects on stack , use them in pipeline rather creating them on heap new
.
int main(int argc, char* argv[]) { static const unsigned int big_size = 2u * 1024u * 1024u; static const unsigned int block_size = 4096u; try { secbyteblock key(32); os_generaterandomblock(false, key.data(), key.size()); // cout << "key: "; // arraysource as(key.data(), key.size(), true, new hexencoder(new filesink(cout))); // cout << endl; cfb_mode<aes>::encryption enc; enc.setkeywithiv(key.data(), key.size(), key.data()); meterfilter meter; streamtransformationfilter stf(enc); filesource source("/dev/zero", false); filesink sink("zero.enc"); source.attach(new redirector(stf)); stf.attach(new redirector(meter)); meter.attach(new redirector(sink)); unsigned int remaining = big_size; while(remaining && !source.sourceexhausted()) { if(remaining % (1024) == 0) { cout << "processed: " << meter.gettotalbytes() << endl; } const unsigned int req = stdmin(remaining, block_size); source.pump(req); source.flush(false); remaining -= req; } } catch(const exception& ex) { cerr << ex.what() << endl; } return 0; }
Comments
Post a Comment