I have a key that recorded in the file during encryption. I extract key from file to string and try to use for decrypt.
It sounds like you have a file structured similar to below:
[...key...][...iv...][...encrypted data...]
It is a bit unusual to store a key like that, but here is how it goes... Below I assume you are using AES-128 in CTR mode. That means [...key...]
is 16 bytes, and [...iv...]
is 16 bytes. The remainder is encrypted data.
I generate the sample file with:
$ head -c 128 < /dev/urandom > message.enc
$ hexdump -C message.enc
00000000 17 44 79 6b e6 96 ff d0 9e 3e 8c c4 fe 57 56 a2 |.Dyk.....>...WV.|
00000010 bb 59 9c a6 fb ab 73 de a7 a9 4a 22 14 6e c4 af |.Y....s...J".n..|
00000020 31 13 04 4d f2 79 f8 7c 7a 0b 16 2c bd be 6e 4c |1..M.y.|z..,..nL|
00000030 b6 61 0a 6c 33 d3 f0 73 25 44 ec f5 cd f5 cd da |.a.l3..s%D......|
00000040 3d 13 72 98 65 19 e1 c5 f8 49 1e 07 c7 dc ac b7 |=.r.e....I......|
00000050 ce 03 d1 90 94 08 aa 9d a0 8b b0 cd ff 9c b9 67 |...............g|
00000060 8a 2c 6f d9 7e fa d2 07 0f a0 48 99 57 77 2b d1 |.,o.~.....H.Ww+.|
00000070 c7 28 2a bc 80 22 21 fb 4a ba cb b2 0e b6 2c ff |.(*.."!.J.....,.|
The key is 17 44 ... 56 a2
. The iv is bb 59 ... c4 af
. The encrypted data starts at 31 13 04 4d...
.
And the program:
$ cat test.cxx
#include "filters.h"
#include "files.h"
#include "modes.h"
#include "aes.h"
#include "hex.h"
#include <iostream>
int main()
{
using namespace CryptoPP;
// Key and iv are stored at the head of the file
SecByteBlock key(16), iv(16);
FileSource fs("message.enc", false /* DO NOT Pump All */);
// Attach new filter
ArraySink ak(key, key.size());
fs.Detach(new Redirector(ak));
fs.Pump(16); // Pump first 16 bytes
// Attach new filter
ArraySink av(iv, iv.size());
fs.Detach(new Redirector(av));
fs.Pump(16); // Pump next 16 bytes
CTR_Mode<AES>::Decryption decryptor;
decryptor.SetKeyWithIV(key, key.size(), iv, iv.size());
// Detach previously attached filter, attach new filter
ByteQueue queue;
fs.Detach(new StreamTransformationFilter(decryptor, new Redirector(queue)));
fs.PumpAll(); // Pump remainder of bytes
std::cout << "Key: ";
StringSource(key, key.size(), true, new HexEncoder(new FileSink(std::cout)));
std::cout << std::endl;
std::cout << "IV: ";
StringSource(iv, iv.size(), true, new HexEncoder(new FileSink(std::cout)));
std::cout << std::endl;
std::cout << "Message: ";
HexEncoder hex(new FileSink(std::cout));
queue.TransferTo(hex);
std::cout << std::endl;
return 0;
}
You can call either Attach
or Detach
to attach a new filter. Both attach a new filter. The difference is, Attach
returns the old filter and you have to free it. Detach
deletes the old filter for you.
Finally:
$ g++ -Wall -I . test.cxx ./libcryptopp.a -o test.exe
$ ./test.exe
Key: 1744796BE696FFD09E3E8CC4FE5756A2
IV: BB599CA6FBAB73DEA7A94A22146EC4AF
Message: 84F6DC079CA04BDFACB645CB11CC2F828573F1841B1B9267CB296B6A977BE19D68B05FA
AF41AB73498F45629EE050B132174A2798C12C29A7033ADD1999BECD00B101F2616112D7E6968EA0
A1BE159CD0EE43549BA6534C8D4AB8F5E7D9E3E44
Unrelated to Crypto++, you usually want to avoid this:
while (!TextFile1.eof())
{
if (TextFile1.eof())
break;
TextFile1 >> s1;
}
I believe it is usually better to follow Why is iostream::eof inside a loop condition considered wrong? (I even think TextFile1 >> s1;
should be in between the TextFile1.eof()
checks. You don't reach eof()
until you try to perform a read).