Bitwise operations in the steganography program (C)

I am relatively new to C and bitwise operations, and I am having problems with the assignment I was given in my class. Most of the code was provided to me, but I had trouble calculating the part related to bitwise operations. As soon as I exit this part, I will become gold. I hope someone can help!

Here is an excerpt from my assignment:

You will need to use 8 bytes of image to hide one byte of information (remember that only LSB covers can be changed). You will use the remaining 16 bytes of the cover to insert 16 bits of b.size (the two least significant bytes of the size field for binary data), the next 32 bytes of the cover will be used to embed the file extension for the payload file, after which you will use 8 * b. size bytes to insert the payload (b.data).

What this program does is shorthand the image, and I need to change the least significant bits of the image read using the data from the file I created. As I said, all the code for this has already been written. I just can't figure out how to change LSB. Any help would be greatly appreciated.

The functions that I should use to reformat LSB are as follows:

byte getlsbs(byte *b);
void setlsbs(byte *b, byte b0);

This is what I tried to do so far:

/* In main function */
b0 = getlsbs(&img.gray[0])

/* Passing arguments */
byte getlsbs(byte *b)
{
    byte b0;
    b0[0] = b >> 8;
    return b0;
}

I honestly lost. I have been working on this all night, and I barely had time to move forward.

+3
source share
1 answer

To set LSB from b to 1:

b |= 1;

To set LSB from b to 0:

b &= 0xFE;

Here's how to implement the functions. This code has not been verified.

byte getlsbs(byte *b)
{
    byte result = 0;
    for (int i = 0; i < 8; ++i)
    {
        result >>= 1;
        if (*b & 1)
            result |=  0x80;
        ++b;
    }
    return result;
}

void setlsbs(byte *b, byte b0)
{
    for (int i = 0; i < 8; ++i)
    {
        if (b0 & 1)
            *b |= 1;
        else
            *b &= 0xFE;
        ++b;
        b0 >>= 1;
    }
}
+4
source

All Articles