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?
Aucun commentaire:
Enregistrer un commentaire