通过修改元素的最低有效位来隐藏大型指针数组中的数字 [英] Hiding number in large pointer array by modifying least significant bit of elements

查看:100
本文介绍了通过修改元素的最低有效位来隐藏大型指针数组中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直蝙蝠,我几乎不知道自己在这里做什么-在用C掌握按位运算符时遇到了很大的麻烦.作为我其中一门课程的练习,我应该隐藏一个数字(包含数字的大型指针数组(无符号字符)中的unsigned int).我正在使用srand(带有键,以便以后可以对其进行解码)来选择数组的特定元素,然后取我应该隐藏的数字的一位(逐个迭代)并更改数组元素的最低有效位.选择元素.

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.

尽管我有一个大致的想法,但是尽管进行了谷歌搜索,但我还是无法弄清楚位运算.因此,我应该在循环的第i次运行中编码size(i-th位的大小)并随机选择current_element,这就是我想出的方法来获取该位然后更改元素.

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);

    }

这两个是不同的函数,并且srand()预先在每个函数中重新植入.

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).

推荐答案

我只是间接地回答您的问题.我将为您提供许多新程序员需要的温柔建议.
如果您想学习编程,请停止谷歌搜索和思考.

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

现在,为每个步骤编写C代码.实际上,您拥有大部分作品.

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);
   }
 }

现在您可以执行基本步骤了,也许您可​​以在Google 如何进行切换"上.但是,在插入答案之前,请确保您已理解答​​案.

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?

解决这个问题,至少还有一个挑战.在解码功能中,您将如何实现is_lsb_toggled?您怎么知道给定的位应该是1还是0?您拥有所需的所有信息.祝你好运.

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.

这篇关于通过修改元素的最低有效位来隐藏大型指针数组中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆