I am trying to write an AES key to a file then read it later. I'm using the Crypto++ library, and the AES key is initialized as follows. Below, byte
is a typedef of unsigned char
.
byte key[CryptoPP::AES::MAX_KEYLENGTH]
The key is 32 bytes long. I try to write it into a file with this:
FILE* file = fopen("C:\\key", "wb");
fwrite(key, 1, sizeof(key), file);
fclose(file);
And recover it using:
FILE* read_file = fopen("C:\\key", "rb");
fseek(read_file, 0, SEEK_END);
long int size = ftell(read_file);
fclose(read_file);
read_file = fopen("C:\\key", "rb");
unsigned char * in = (unsigned char *)malloc(size);
byte readed_key = fread(in, sizeof(unsigned char), size, read_file);
fclose(read_file);
if (key == readed_key)
{
cout << "this is the right key !";
}
free(in);
However, I get an error message:
incompatible operand types : byte* and byte.
I don't understand why, since readed_key
and key
are initialized with byte
and not byte*
.
I looked at AES on the Crypto++ wiki, and the key is generated as follow. I figured out that I was just creating the key (not generating it):
SecByteBlock key(0x00, AES::MAX_KEYLENGTH);
rnd.GenerateBlock( key, key.size() );
With that I can't use
std::vector<byte> key(32);
rnd.GenerateBlock(key, key.size());
Because rnd.Generateblock
can't convert std::vector< byte > into byte*
This is driving me crazy....