c - 确定消息是否太长而无法嵌入图像

标签 c steganography ppm

我创建了一个程序,通过弄乱文件中每个字节的最后一位,将消息嵌入到 PPM 文件中。我现在遇到的问题是我不知道我是否正在检查消息是否太长或不正确。到目前为止,这是我得到的:

int hide_message(const char *input_file_name, const char *message, const char *output_file_name)
{
    unsigned char * data;
    int n;
    int width;
    int height;
    int max_color;

    //n = 3 * width * height;
    int code = load_ppm_image(input_file_name, &data, &n, &width, &height, &max_color);

    if (code)
    {
        // return the appropriate error message if the image doesn't load correctly
        return code;
    }

    int len_message;
    int count = 0;
    unsigned char letter;

    // get the length of the message to be hidden
    len_message = (int)strlen(message);


    if (len_message > n/3)
    {
        fprintf(stderr, "The message is longer than the image can support\n");
        return 4;
    }

    for(int j = 0; j < len_message; j++)
    {

        letter = message[j];
        int mask = 0x80;

        // loop through each byte
        for(int k = 0; k < 8; k++)
        {
            if((letter & mask) == 0)
            {
                //set right most bit to 0
                data[count] = 0xfe & data[count];
            }
            else
            {
                //set right most bit to 1
                data[count] = 0x01 | data[count];
            }
            // shift the mask
            mask = mask>>1 ;
            count++;
        }
    }
    // create the null character at the end of the message (00000000)
    for(int b = 0; b < 8; b++){
        data[count] = 0xfe & data[count];
        count++;
    }

    // write a new image file with the message hidden in it
     int code2 = write_ppm_image(output_file_name, data, n, width, height, max_color);

    if (code2)
    {
        // return the appropriate error message if the image doesn't load correctly
        return code2;
    }

    return 0;
}

所以我正在检查消息的长度 (len_message) 是否比 n/3 长,这与宽度*高度相同。这看起来正确吗?

最佳答案

您当前正在执行的检查是检查消息的字节数 是否多于图像的像素数。因为您只使用每像素 1 位来对消息进行编码,所以您需要检查消息的是否多于消息的像素数。

所以你需要这样做:

if (len_message*8 > n/3)

关于c - 确定消息是否太长而无法嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35941826/

相关文章:

c - 我怎样才能减少 2^n + 1 的除法?

c++ - 将 64 位 int 作为输出传递给 32 位内联汇编

python - 使用 python 将数据嵌入到二进制图像中

java - 在 Android 中保存位图

png - 在 png 图像中隐藏 secret 的方法(隐写术)

c - 为什么我的图像与我期望的不符?

c - 不会从文件读取到结构

c - 如何为 BITMAP 设置像素数据存储

c++ - 问题读取 ppm p6 图像完成。 C++

c - 我的功能是以不同的方式将 PGM 图像文件复制到 PPM