PumpMessages, reading a file?

15 views
Skip to first unread message

codekiddy

unread,
Mar 2, 2015, 1:57:41 PM3/2/15
to cryptop...@googlegroups.com

I'm writing an AES key and iv to the file with crypto++ by using following code:

// write the key:
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
SecByteBlock iv(AES::BLOCKSIZE);

string file = m_file_name + ".key";
FileSink* key_out = new FileSink(file.c_str());
Base64Encoder base64_key_enc(key_out);

base64_key_enc.Put(key.BytePtr(), key.size());
base64_key_enc.MessageEnd();
base64_key_enc.Put(iv.BytePtr(), iv.size());
base64_key_enc.MessageEnd();

and to read a the key and iv from the file back I use following:

// read key
string file = m_file_name + ".key";
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
ArraySink* arr_key_in = new ArraySink(key, key.size());
Base64Decoder* base64_key_dec = new Base64Decoder(arr_key_in);
FileSource source(file.c_str(), false, base64_key_dec);
source.PumpMessages(1); // read only the key 

// read iv
SecByteBlock iv(AES::BLOCKSIZE);
ArraySink* arr_iv_in = new ArraySink(iv, iv.size());
base64_key_dec->Detach(arr_iv_in);
source.PumpAll(); // read the rest (the iv)

Problem is that after reading a file the key is correct but iv is not, so i wornder what could be wrong with my sintax?

the contents of a key file are base64 hex encoded and it looks like so:

2Gnh3TbAJeQPmza9FKdqNg== FowuKut3pBl7g0Or+4FJUg==

the == means end of the message/key... First on is the key, while the other one is the iv, the above code does not read the iv from the file properly.

What is wrong with my code?

Jeffrey Walton

unread,
Mar 3, 2015, 12:11:38 AM3/3/15
to cryptop...@googlegroups.com
For completeness, this was answered at http://stackoverflow.com/q/28816212.

codekiddy

unread,
Mar 3, 2015, 2:16:10 AM3/3/15
to cryptop...@googlegroups.com

Thank you Jeffrey, I was away from keyboard until now and forgot to post the answer.
Reply all
Reply to author
Forward
0 new messages