【问题标题】:Bit extraction and Steganography位提取和隐写术
【发布时间】:2011-09-27 18:35:09
【问题描述】:

我在玩隐写术。我正在尝试从图像中提取文本文件。我能够读取文件,获取位,但我在提取这些位时遇到问题。

int getbits( pixel p) {
    return p & 0x03;   
}

char extract ( pixel* image ) {
    static int postion;
    postion = 0;

    postion = *image;

    postion++;

    char curChar;
    curChar = '\0';
    for(int i = 0; i<4; ++i) {
        curChar = curChar << 2;
        curChar = curChar | getbits(postion);
    }
    return curChar;
}

像素是一个无符号字符。我有调用extract()fputc(3) 返回值的循环。我觉得我从这些碎片中得到了垃圾。这导致我有大量 (1.5 gig) txt 文件作为回报。

void decode( PgmType* pgm, char output[80] )
{
FILE*outstream;
int i, length;

outstream = fopen(output, "w");

if(!outstream)
{
    fatal("Could not open");
}
for(i=0; i < 16; ++i)
{
    length = length << 2;
    length = length | getbits(*pgm->image);
}
if ((length* 4) < (pgm->width * pgm->height))
{
    fatal("File Too Big");
}
for (i = 0 ;i<length; ++i)
{
    fputc(extract(pgm->image), outstream);

}
fclose(outstream);

}

【问题讨论】:

  • 速记或隐写术 ?
  • 显示调用提取的循环 - 因为您应该证明您正确地循环图像。
  • @borrible 我包含了循环

标签: c bit-manipulation steganography


【解决方案1】:

您实际上只是在读取图像中的第一个像素 - [编辑] 因为当您尝试使用静态变量来保持计数时,正如 Oli 指出的那样,您会立即覆盖它。

改为使用位置来跟踪您的计数;但将数据保存在另一个变量中:

extract() 应该看起来像:

char extract ( pixel* image )
{
   static int postion = 0;

   pixel data = image[position];

   postion++;

   // use 'data' for processing
}

【讨论】:

  • 这是一条红鲱鱼; OP 立即用*image 覆盖position ...
  • @Joe:我假设您调用 extract 的次数超过了图像数组中的像素数。你能看到你在哪个迭代中得到错误吗?
  • @Dave 它不是第一个它正在创建文件并且只是在运行数组
  • @Dave 我还必须将 position++ 放在循环中。
【解决方案2】:

Dave Rigby 的 excellent diagnosis 是正确的,但是将 position 作为参数传递(并且在这里增加它)会导致更容易理解和更灵活的例程:

char extract ( pixel* image, int position ) {
    char curChar = '\0';
    for(int i = 0; i<4; ++i) {
        curChar = curChar << 2;
        curChar = curChar | getbits(postion);
    }
    return curChar;
}

char *build_string(pixel *image) {
    int i;
    char *ret = malloc(SECRET_SIZE);
    for (i=0; i<SECRET_SIZE; i++) {
        ret[i]=extract(image, i);
    }
    ret[i] = '\0';
    return ret;
}

然后,当您意识到更改一行中的所有像素会使其非常明显,并且您宁愿使用位于斐波那契值处的像素时,更改很容易:

char *build_string_via_fib(pixel *image) {
    int i;
    char *ret = malloc(SECRET_SIZE);

    for (i=0; i<SECRET_SIZE; i++) {
        ret[i]=extract(image, fib(i));
    }
    ret[i]='\0';
    return ret;
}

也可以将斐波那契计算填充到您的 extract() 例程中,但是将函数分解为最小、最有用的部分,可为您提供出色的易读性、出色的可测试性和未来代码的最佳机会重复使用。

【讨论】:

    猜你喜欢
    • 2014-08-19
    • 2010-11-17
    • 2017-02-03
    • 2014-07-11
    • 2020-03-12
    • 1970-01-01
    • 2016-02-19
    • 2018-07-29
    • 2011-02-17
    相关资源
    最近更新 更多