[Scene]
GCM + AES
[Code 01]
encrypt:
write IV to <file-a>
write encryped stuff to <file-b>
decrypt:
using <file-a> and <file-b> -- ok
[Code 02]
encrypt:
open std::ofstream <ofs> for <file-c>
write IV to <ofs>
write encryped stuff to <ofs> ... (FileSource(<src>, true, new
AuthenticatedEncryptionFilter(e, new FileSink(<ofs>)));)
decrypt:
using <file-c> -- error
compare [Code 01] [Code 02] binary output file:
<file-a> + <file-b> != <file-c>
Question:
FileSource(<src>, true, new AuthenticatedEncryptionFilter(e, new
FileSink(<ofs>)));
FileSink (std::ostream &out)
Will CryptoPP::FileSink overwriter previous byte stream of <ofs>?
[Code 01]
#include "console.h"
#include "cryptopp/cryptlib.h"
#include "cryptopp/filters.h"
using CryptoPP::AuthenticatedEncryptionFilter;
using CryptoPP::AuthenticatedDecryptionFilter;
#include "cryptopp/files.h"
using CryptoPP::FileSource;
using CryptoPP::FileSink;
#include "cryptopp/aes.h"
using CryptoPP::AES;
#include "cryptopp/gcm.h"
using CryptoPP::GCM;
#include "cryptopp/secblock.h"
using CryptoPP::SecByteBlock;
#include "cryptopp/osrng.h"
using CryptoPP::AutoSeededRandomPool;
#include <iostream>
using std::cerr;
using std::endl;
#include <cstdio>
#include <fstream>
#include <string>
int main(int argc, char* argv[])
{
auto arg_src_file = "H:/test/crypto/src.exe";
auto arg_enc_file = "H:/test/crypto/enc.dat";
auto arg_iv_file = "H:/test/crypto/iv.dat";
auto arg_dec_file = "H:/test/crypto/dec.exe";
auto arg_key_prompt = "KEY:";
AutoSeededRandomPool prng;
try
{
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
::con_passwd(arg_key_prompt, reinterpret_cast<char *>(key.data()),
key.size());
con_print_hex("KEY:", key, key.size());
SecByteBlock iv(AES::BLOCKSIZE);
prng.GenerateBlock(iv, iv.size());
con_print_hex("IV:", iv, iv.size());
GCM< AES >::Encryption e;
e.SetKeyWithIV(key, key.size(), iv, iv.size());
std::ofstream ofs(arg_iv_file, std::ios_base::trunc);
ofs.write(reinterpret_cast<const char *>(iv.data()), iv.size() *
sizeof(byte) / sizeof(char));
FileSource(arg_src_file, true, new AuthenticatedEncryptionFilter(e, new
FileSink(arg_enc_file)));
}
catch (const CryptoPP::Exception & e)
{
auto x = e.what();
cerr << e.what() << endl;
exit(1);
}
catch (...)
{
exit(1);
}
try
{
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
::con_passwd(arg_key_prompt, reinterpret_cast<char *>(key.data()),
key.size());
con_print_hex("KEY:", key, key.size());
SecByteBlock iv(AES::BLOCKSIZE);
std::ifstream ifs(arg_iv_file);
ifs.read(reinterpret_cast<char *>(iv.data()), iv.size() * sizeof(byte) /
sizeof(char));
con_print_hex("IV:", iv, iv.size());
GCM< AES >::Decryption d;
d.SetKeyWithIV(key, key.size(), iv, iv.size());
FileSource(arg_enc_file, true, new AuthenticatedDecryptionFilter(d, new
FileSink(arg_dec_file)));
}
catch (const CryptoPP::Exception & e)
{
auto x = e.what();
cerr << e.what() << endl;
exit(1);
}
catch (...)
{
exit(1);
}
return 0;
}
--
You received this message because you are subscribed to "Crypto++ Users". More
information about Crypto++ and this group is available at
http://www.cryptopp.com and
http://groups.google.com/forum/#!forum/cryptopp-users.
---
You received this message because you are subscribed to the Google Groups
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.