how do I encrypt an int with a keystream that produces unsigned char?

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?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
unsigned char* clear_buffer = reinterpret_cast<unsigned char*>(&clear_data);
size_t buffersz = sizeof(clear_data);

unsigned char* encrypted_buffer = new unsigned char[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.
Last edited on
Topic archived. No new replies allowed.