C中的隐写术

use*_*536 6 c steganography pixel image-processing ppm

尝试在PPM图像上进行基本的隐写术.

我已完成基本算法.读入文件,检查标题以P6开头,获取图像的宽度和高度,以及像素数据.

我需要总共有四种方法:ReadPPM,WritePPM,WriteMsg和ReadMsg.

我有ReadImg和WriteImg方法,但我遇到的是我的WriteMsg方法.这是基本的隐写术,只是将字符串的每个位写入每个字节的最后一位.假设前8个字节包含被隐藏的字符串的大小,然后每个字节开始隐藏的消息.

我的想法是创建一个大型数组,其中包含字符串大小的二进制代码,然后是字符串本身的二进制代码.我只想弄清楚如何将该数组添加到图像中的每个字节.

任何帮助深表感谢.这是我目前的代码:

#include<stdio.h>
#include<stdlib.h>

typedef struct {
     unsigned char red,green,blue;
} PPMPixel;

typedef struct {
     int x, y;
     PPMPixel *data;
} PPMImage;

#define CREATOR "RPFELGUEIRAS"
#define RGB_COMPONENT_COLOR 255

static PPMImage *readPPM(const char *filename)
{
    char buff[16];
    PPMImage *img;
    FILE *fp;
    int c, rgb_comp_color;
    //open PPM file for reading
    fp = fopen(filename, "rb");
    if (!fp) {
         fprintf(stderr, "Unable to open file '%s'\n", filename);
         exit(1);
    }

    //read image format
    if (!fgets(buff, sizeof(buff), fp)) {
         perror(filename);
         exit(1);
    }

    //check the image format
    if (buff[0] != 'P' || buff[1] != '6') {
         fprintf(stderr, "Invalid image format (must be 'P6')\n");
         exit(1);
    }

    //alloc memory form image
    img = (PPMImage *)malloc(sizeof(PPMImage));
    if (!img) {
         fprintf(stderr, "Unable to allocate memory\n");
         exit(1);
    }

    //check for comments
    c = getc(fp);
    while (c == '#') {
    while (getc(fp) != '\n') ;
         c = getc(fp);
    }

    ungetc(c, fp);
    //read image size information
    if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
         fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
         exit(1);
    }

    //read rgb component
    if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
         fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
         exit(1);
    }

    //check rgb component depth
    if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
         fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
         exit(1);
    }

    while (fgetc(fp) != '\n') ;
    //memory allocation for pixel data
    img->data = (PPMPixel*)malloc(img->x * img->y * sizeof(PPMPixel));

    if (!img) {
         fprintf(stderr, "Unable to allocate memory\n");
         exit(1);
    }

    //read pixel data from file
    if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
         fprintf(stderr, "Error loading image '%s'\n", filename);
         exit(1);
    }

    fclose(fp);
    return img;
}

void writePPM(const char *filename, PPMImage *img)
{
    FILE *fp;
    //open file for output
    fp = fopen(filename, "wb");
    if (!fp) {
         fprintf(stderr, "Unable to open file '%s'\n", filename);
         exit(1);
    }

    //write the header file
    //image format
    fprintf(fp, "P6\n");

    //comments
    fprintf(fp, "# Created by %s\n",CREATOR);

    //image size
    fprintf(fp, "%d %d\n",img->x,img->y);

    // rgb component depth
    fprintf(fp, "%d\n",RGB_COMPONENT_COLOR);

    // pixel data
    fwrite(img->data, 3 * img->x, img->y, fp);
    fclose(fp);
}

void writeMsg(PPMImage *img, char *s)
{   

    int i;
    int len;
    len = sizeof(s);

    if (img)
    {
         j = 0;
         for (i=0; i < img->x * img->y; i++)
         {
              while(j < 8)
              {
                   if(len & 0x80)   
                   {
                        img->data[i].red= img->data[i].red | 0x01;
                   }
                   else 
                   {
                        img->data[i].red= img->data[i].red & 0xFE;
                   }

                   len=len << 1;
                   j++;

                   if (len & 0x80)  
                   {
                        img->data[i].green= img->data[i].green | 0x01;
                   }
                   else
                   {
                        img->data[i].green= img->data[i].green & 0xFE;
                   }


                   len = len << 1;
                   j++;

                   if (len & 0x80)  
                   {
                        img->data[i].blue= img->data[i].blue | 0x01;
                   }
                   else
                   {
                        img->data[i].blue= img->data[i].blue & 0xFE;
                   }
                   j++;
              }
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

Pat*_*shu 2

要从字节中提取一位,请执行以下操作:

bit(i) = byte >> i & 0x1

这会将字节的位向右移动 i 次,然后and将其设置为 0000 0001(这样,除了最低位之外的所有位均为零,最低位为 0 表示 0,1 表示 1)。

您可以对 16 位短、32 位整数、64 位长......甚至char字符串的 s 执行类似的操作。您可以使用 sizeof(char) 查看一个 char 有多少字节。

但是,当然,您将有多个字符(或长或整数或...)来获取额外的位。要决定从哪个元素中添加一点:

如果你想要第ith 位并且元素是x位宽,那么i%x从元素中获取该位[i/x]

现在您已经有了这个位,要将其放置在字节(或 int 或 char 或...)内,请执行以下操作:

隐写术字节 = 原始字节&(~0x1) + 位

这意味着,你取数字... 0000 0001,反转它的位,所以它是... 1111 1110,and它与原始字节一起,这样除了最低位之外的所有位都被保留,然后只需添加你的位即可。