how do I encrypt an int or some other data structure that has a larger size than an unsigned char with a symmetric stream cipher with a keystream that produces unsigned char as each element?
Let's say I have something like:
unsigned char getNextKeystreamElement();
how do I use it to encrypt(XOR) all the bytes in say an int or a struct?
struct MyStructToBeEncrypted
{
char field1[SOME_LENGTH_1];
int field2[SOME_LENGTH_2];
// ... and so on
};
MyStructToBeEncrypted clear_data = GetData(); // fill in the record to be encrypted
unsignedchar* clear_buffer = reinterpret_cast<unsignedchar*>(&clear_data);
size_t buffersz = sizeof(clear_data);
unsignedchar* encrypted_buffer = newunsignedchar[buffersz];
if (encrypted_buffer)
{
for (size_t i = 0; i != buffersz; ++i)
{
encrypted_buffer[i] = getNextKeystreamElement()^ buffer[i];
}
}
In this example:
1. the record should be packed (byte aligned)
2. the record should contain only built in types or records make only of built in types (i.e. it must be a POD type).
3. I've just written this into the editor. If it doesn't work, you can easily fix it.