Hiding number in large pointer array by modifying

2019-08-27 04:20发布

问题:

Straight of the bat I will say I barely know what I'm doing here - I'm having a major trouble grasping bitwise operators in C. As an exercise in one of my courses I'm supposed to hide a number (unsigned int) in large pointer array (unsigned char) containing numbers. I'm doing it using srand (with key, so that I can decode it later) to choose specific elements of the array and then take one bit of the number I'm supposed to hide (iterating through all the bits) and changing the least significant bit of the array element. The choosing elements.

While I get the general idea I cannot, despite googling, figure out the bit operations. So having the size that I'm supposed to encode in i-th run of the loop (i-th bit of size) and randomly chosen current_element this is what I came up with to get the bit and then alter the element.

for (i=0; i<32; i++){    
    tmp = rand() % max;
    current_element = array[tmp];
    current_element ^= ((size >> i)  & 0x01)<<7;
}

To decode I would write it analogically (where size is wiped unsigned char that I'm trying to write the decoded number to):

for (i=0; i<32; i++){
        tmp = rand() % max;
        current_element = array[tmp];
        size = size ^ ((current_pixel.blue<<0)<<7);

    }

These two are in diffrent functions and srand() is seeded anew in each of them beforehand.

But these are clearly not working and I don't even know which one (I can only check if it decoded correctly). Truth be told these are mostly copied from other things I found online as so far operating on individual bits eludes me. So I'd be grateful for some sort of advice on what is wrong here (and I'm aware probably everything is wrong here and it's all gibberish but I've been trying to fix it to no avail for quite a while now).

回答1:

I'm only going to indirectly answer your question. I'm going to give you some gentle advice that many new programmers need.
If you want to learn to program, Stop googling and Think.

Break the problem down into steps. Write pseudocode:

  encode:
     for each bit in message_word:
        select random array element
        if bit is set:
           toggle LSB of element.

  decode:
     for each bit in message_word:
        select random element
        if LSB is toggled:
           set bit in message_word

Now, write the C code for each of those steps. You actually have most of the pieces.

 // for each bit in message_word
 for (i=0;i<sizeof(message_word); i++) {
   // select random array element
   tmp = rand() % max;
   current_element = array[tmp];
   // if bit is set:
   if ( bit_is_set(message_word,i) ) {
       // toggle LSB of element.
       toggle_lsb(current_element);
   }
 }

Now that you are down to basic steps, maybe you can google "how to toggle a bit". But make sure you understand the answer before plugging it in.

int bit_is_set(word,bit) { return ((word>>bit)&0x01); }
int toggle_lsb(word) { return word ^ 1; }

However - this still won't work. Why? Time to think again. You made a copy of the value at the randomly selected array index. You toggled a bit in the copy. What effect is that going to have on the array?

Solve that and there's at least one more challenge. In the decode function, how will you implement is_lsb_toggled? How will you know if a given bit was supposed to be 1 or 0? You have all the information you need. Good Luck.