c-隐写术函数可以很好地处理文本,但会混淆二进制输出



我试图通过在每个像素中使用每种颜色的最后2个LSB(在蓝色的情况下是最后3个(将文件嵌入图像中,无论何时编码/解码,文本都可以正常工作,但任何二进制数据,如ELF文件、随机数据等都会混淆。这是我的隐写和去隐写功能:

bool steganographize(int height, int width, unsigned long long filesize, unsigned char *file, RGBTRIPLE image[height][width], bmpstatus about)
{
int f = 0; //for char access
int i = 0;
int j = 0;
for (i = 0 ; i < height; i++)
{
if (f >= filesize)
{
goto finis;
}
for (j = 0 ; j < width; j++)
{
if (f >= filesize)
{
goto finis;
}
image[i][j].rgbtRed = ((image[i][j].rgbtRed & ~0x3) | ((file[f] & 0x60) >> 5));
image[i][j].rgbtGreen = ((image[i][j].rgbtGreen & ~0x3) | ((file[f] & 0x18) >> 3));
image[i][j].rgbtBlue = ((image[i][j].rgbtBlue & ~0x7) | ((file[f] & 0x7)));
f++;
}
}
finis:
if (f == (filesize))
{
printf("%dn", i);
printf("%dn", j);

print_status("Embedded file into BMP");
print_status("Inserting EOF");
image[i][j].rgbtRed = 0;
image[i][j].rgbtGreen = 92;
image[i][j].rgbtBlue = 183;
image[i][j + 1].rgbtRed = 12;
image[i][j + 1].rgbtGreen = 57;
image[i][j + 1].rgbtBlue = 237;
image[i + 1][j].rgbtRed = 91;
image[i + 1][j].rgbtGreen = 34;
image[i + 1][j].rgbtBlue = 45;
return true;
}
return false;
}
bool desteganographize(int height, int width, RGBTRIPLE image[height][width], bmpstatus about, FILE* output)
{
unsigned char* buffer = calloc(height * width * 3, sizeof(unsigned char));
int i = 0;
int j = 0;
int f = 0;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
if (image[i][j].rgbtRed == 0 && image[i][j].rgbtGreen == 92 && image[i][j].rgbtBlue == 183) {
if (image[i][j + 1].rgbtRed == 12 && image[i][j + 1].rgbtGreen == 57 && image[i][j + 1].rgbtBlue == 237 
&& image[i + 1][j].rgbtRed == 91 && image[i + 1][j].rgbtGreen == 34 && image[i + 1][j].rgbtBlue == 45) {
goto finis;
} 
}
unsigned char c = 
((image[i][j].rgbtRed & 0x3) << 5) | 
((image[i][j].rgbtGreen & 0x3) << 3 ) |
(image[i][j].rgbtBlue & 0x7);
buffer[f] = c;
f++;
}
}
finis: 
fwrite(buffer, sizeof(unsigned char), f, output);
printf("%dn", i);
printf("%dn", j);
fclose(output);
return true;

return false;
}

以下是我主要功能的相关部分:

int main(int argc, char** argv) {
if (strstr(argv[1], encode) != NULL) {
FILE *bmp = fopen(bitmap, "rb");
bmpstatus *result = is_valid_bmp(bmp);
if (result == NULL)
{
print_error("Bad BMP. Maybe you specified the wrong file?");
}
//Get BMP into memory
char buffer[BUFFER_LEN];
sprintf(buffer, "Found BMP of Height %d and width %d", result->height, result->width);
print_status(buffer);
cleararr(buffer);
int width = result->width;
int height = result->height;
RGBTRIPLE(*image)
[width] = calloc(height, width * sizeof(RGBTRIPLE));
if (!scanimage(height, width, bmp, image, *result))
{
print_error("Error scanning BMP into memory. Probably not enough RAM.");
}
print_status("Scanned BMP into memory");
//Scan file into memory
unsigned long long filesize = file_size(fopen(filename, "rb"));
sprintf(buffer, "Found file with %llu kilobytes", filesize / 1024);
print_status(buffer);
cleararr(buffer);
if (filesize > ((unsigned long long)width * height * 3) / 8)
{
print_error("Bitmap not big enough to hold file");
}
unsigned char *file = calloc(filesize, sizeof(unsigned char));
printf("%llun", filesize);
if (!scan_file(file, filesize, fopen(filename, "rb")))
{
print_error("Error scanning file into memory");
}
print_status("Scanned file into memory");

if (!steganographize(height, width, filesize, file, image, *result))
{
print_error("Error embedding file into BMP");
}
//Output manipulated BMP
sprintf(buffer, "Outputting finished BMP to %s", output);
print_status(buffer);
outputimage(height, width, fopen(output, "wb"), image, *result);
cleararr(buffer);

free(result);
free(image);
free(file);
print_success("Finished!");
return 0;
}
if (strstr(argv[1], decode)) {
bmpstatus *result = is_valid_bmp(input);
if (result == NULL)
{
print_error("Bad BMP");
}
int height = result->height;
int width = result->width;
RGBTRIPLE(*image)
[width] = calloc(height, width * sizeof(RGBTRIPLE));
char buffer[BUFFER_LEN];
sprintf(buffer, "Found a BMP with height %d and width %d", height, width);
print_status(buffer);
cleararr(buffer);
if (!scanimage(height, width, input, image, *result))
{
print_error("Cannot scan BMP into memory. Not enough RAM, maybe?");
}
print_status("Scanned BMP into memory");
char tmpname[16] = "/tmp/tmp.XXXXXX";
mkstemp(tmpname);
print_status("Made temporary file");
FILE* finish = fopen(tmpname, "wb");
if (!desteganographize(height, width, image, *result, finish)) {
print_error("Could not extract file.");
}
free(result);
free(image);
return 0;
}
}

这不是最优雅的方式,但它似乎完成了任务(至少对于文本(。bmpstatus是一个具有bmp的高度和宽度的结构,filesize具有输入数据的大小,RGBTRIPLE是一个包含红、绿、蓝三个元素的结构。我已经证实这两个有效。scanimage获取BMP并将其放入RGBTRIPLE矩阵中,scan_file获取一个文件指针并将数据扫描到一个无符号的char数组中。所有这些都有效,所以我假设这是隐写术功能本身的问题。

原来我只从每个字符中读取了7位,而不是8位。一旦我在红色组件中放入用0xe0替换的0x60和用0x7替换的0x3,它就工作了。

最新更新