I'm writing a simple cryptography program that uses ISAAC, and I've run into a wall. I am planning on XORing each of my input bytes with 1 random byte each that gets returned from the ISAAC PRNG.

This is basically what I'm trying to do

``````while(input character != EOF){
xor that byte with a random byte from ISAAC;
print that byte;
}``````

I can't seem how to limit the uint32_t that gets returned from the prng to a single byte, instead I have several. What can I do to isolate a byte from a uint32_t typedef?

## All 3 Replies

What can I do to isolate a byte from a uint32_t typedef?

You can use the bitwise operators to split your 32-bit value into four bytes:

``````uint32_t value;
uint8_t bytes[4];

/* ... */

bytes[0] = (value >> 24) & 0xFF;
bytes[1] = (value >> 16) & 0xFF;
bytes[2] = (value >> 8) & 0xFF;
bytes[3] = (value & 0xFF);``````

thanks for the speedy reply :) Do you mind explaining what exactly is happening in that code?

Are the bits with higher value are getting shifted towards the lower indexes of bytes[]?

Do you mind explaining what exactly is happening in that code?

It's extracting the bytes from most significant to least significant by shifting and masking away all but the least significant byte:

``````01010101111111110000000011110000
00000000000000000000000001010101 -- Shift right 24 bits
01010101 -- Mask the low byte

01010101111111110000000011110000
00000000000000000101010111111111 -- Shift right 16 bits
11111111 -- Mask the low byte

01010101111111110000000011110000
00000000010101011111111100000000 -- Shift right 8 bytes
00000000 -- Mask the low byte

01010101111111110000000011110000
11110000 -- Mask the low byte``````

Each of those bytes are then assigned to a smaller type (uint8_t).

commented: Not only did narue help solve something that I've been working for hours, but helped me understand a new concept as well. Thanks! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.