位提取和隐写术

发布于 2024-11-18 10:35:22 字数 1047 浏览 10 评论 0原文

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

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

Pixel 是一个无符号字符。我有一个循环调用 extract()fputc(3) 返回值。我觉得我从这些碎片中得到了垃圾。这导致我得到大量(1.5 GB)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);

}

I am playing around with steganography. I am trying to pull a text file from a image. I am able to read the file, get the bits, but I have an issue with the extraction of these bits.

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

Pixel is an unsigned char. I have loop that calls extract() and fputc(3) the return value. I feel like I am getting garbage from these bits. Which causes me to have massive (1.5 gig) txt files in return.

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-11-25 10:35:22

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

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

相反,extract() 应该类似于:

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

   pixel data = image[position];

   postion++;

   // use 'data' for processing
}

You are only actually reading the first pixel in the image - [Edit] because while you are trying to use a static var to keep count, as Oli points out you're immediately overwriting it.

Instead use position to track your count; but hold the data in another var:

Instead extract() should look something like:

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

   pixel data = image[position];

   postion++;

   // use 'data' for processing
}
贩梦商人 2024-11-25 10:35:22

Dave Rigby 的出色的诊断是正确的,但超过了位置 作为参数(并且在这里递增它)将导致更容易理解和更灵活的例程:

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()中例程也是如此,但是将函数分解为最小的、最有用的部分,可以为您提供出色的易读性、出色的可测试性以及未来代码重用的最佳机会。

Dave Rigby's excellent diagnosis is correct, but passing position as a parameter (and not incrementing it here) would lead to easier to understand and more flexible routines:

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

Then, when you realize that changing all the pixels in a line makes it pretty obvious, and you'd rather use pixels located at Fibonacci values, the change is easy to make:

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

You could stuff the Fibonacci computation into your extract() routine too, but decomposing functions into the smallest, most useful, pieces, gives you excellent legibility, excellent testability, and best chances for future code re-use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文