gpt4 book ai didi

c - 如何将 LSB 提取的数据转换为文本或二进制转换为文本

转载 作者:行者123 更新时间:2023-11-30 18:51:22 28 4
gpt4 key购买 nike

更新************************作为引用,我包含了用于打开 PPM 图像的程序 - 将消息嵌入到图像中,然后保存带有嵌入文本的新图像。通过下面的函数,我希望额外添加该消息(隐藏在 LSB 中),然后将其转换为文本以显示它。感谢您到目前为止的回复 - 我将开始测试它们,看看是否有任何效果。

<小时/>

我正在尝试编写一个函数来提取无符号字符值的 LSB - 将它们组合在一起以形成提取的消息。我知道需要从文件中提取多少个 LSB 的长度,但我在如何将其转换为消息时遇到了麻烦。

首先,我将前 8 位提取到一个 int 数组中 - 给了我一些诸如 00110000 之类的东西。现在我有一个包含该值的 INT 数组,我需要将其转换为单个字符来表示字母。但是,我认为我应该将所有 LSB 放入一个 messageLength * 7 的数组中,并以某种方式将该 int 数组转换为文本。它将在转换之前为我提供文本的二进制表示形式。也许他们有一种将一长串 1 和 0 转换为文本的方法?

unsigned char * extBits(PPMImage *img, int messageLen, unsigned char * decMsg)
{
//int count = 2;
int embCount = 0;
int deM = messageLen * 7;
int count = 0;
unsigned char byte;
// int mask;
// unsigned char update;
// unsigned char flipOne = 0x01; //0x01
// unsigned char flipZero = 0xFE; //0x00
unsigned char dMsg[deM];
int byteArray[7];
int takeByte = 0;
unsigned char *extMsg;
char c;

for(int j = 0; j < 7; j++)
{
if(takeByte == 8)
{
//first letter extracted

takeByte = 0;
}
//byte = msgOne[j];
// byte = img->pixel[j];
//encMsg[j] = byte;
byte = img->pixel[j];
//printf("byte: %c\n", byte);
// printf("byte: %x\n", byte);
byte &= 1;
//printf("byte after: %x\n", byte);
dMsg[j] = byte;
byteArray[j] = byte;
data[j] = byteArray[j];
printf("dMsg:%x ", dMsg[j]);
// printf("pixel:%c \n", img->pixel[j]);
embCount++;
takeByte++;
}
/*
for(int r=0;r<7;r++)
{
printf("\n%d\n", byteArray[r]);
}
printf("count: %d", embCount);
printf("%s ", dMsg);
*/
return decMsg = dMsg;
}

嵌入程序********

//////////////////////////////////////////////////////////////
/*
execute as ./emb -i <img2embed> -i <text file> -o <embedIMG>

*/
//////////////////////////////////////////////////////////////


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


typedef struct {
int x, y;
unsigned char *pixel;
} PPMImage;

#define RGB_COMPONENT_COLOR 255

static PPMImage *readPPM(const char *filename)
{
FILE * fp;
PPMImage *img;
int rgb_comp_color;
int size = 0;
fp = fopen(filename, "a+");

fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);

unsigned char *buff;
unsigned char stuff[16];
int c;
int x,y;
buff = (unsigned char*) malloc(sizeof(unsigned char)*size +1);
memset(buff, '\0', sizeof(unsigned char)*size+1);

fgets(stuff, sizeof(stuff), fp);

if (stuff[0] != 'P' || stuff[1] != '3') {
fprintf(stderr, "Invalid image format (must be 'P3')\n");
exit(1);
}

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

c = getc(fp);
while (c == '#') {
while (getc(fp) != '\n') ;
c = getc(fp);
}

ungetc(c, fp);

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

if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
exit(1);
}
if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
fprintf(stderr, "'%s' does not have 8-bits components\n",filename);
exit(1);
}

//printf("x: %d y: %d\n", img->x, img->y);

unsigned char buffer[1024];
memset(buffer,0,1024);
fgets(buffer,1024,fp);

fread(buff, 1, size, fp);

img->pixel = buff;
/*
for(int h = 0; h < 20; h++)
{
printf("%c", buff2[h]);

}


printf("%s", buff2);
*/
fclose(fp);

return img;

}

void writePPM(const char *filename, unsigned char * img, int x, int y)
{
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, "P3\n");

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

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

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

// pixel pixel
fwrite(img,1, strlen(img), fp);
fclose(fp);
}

//unsigned char * embBits(PPMImage *img, int messageLen, unsigned char*msgOne, unsigned char *encMsg)
int embBits(PPMImage *img, int messageLen, unsigned char*msgOne, int embLen)
{
//int count = 2;
int embCount = 0;
int count = 0;
unsigned char *eMsg;
unsigned char byte;
int mask;
unsigned char update;
unsigned char flipOne = 0x01; //0x01
unsigned char flipZero = 0xFE; //0x00

for(int j = 0; j < messageLen; j++)
{
byte = msgOne[j];
//encMsg[j] = byte;

for(int k=7; 0 < k; k--)
{
update = byte;

update = update & (1<<k);
//printf("pixel:%c\n", img->pixel[count]);
//printf("pixel+1:%c\n", img->pixel[count+1]);
// printf("pixel+2:%c\n", img->pixel[count+2]);
if(update == 0)
{
// if i see 1 |=
// if i see a 0 &=
//img->pixel[count] = img->pixel[count] &= flipZero;
img->pixel[count+2] &= flipZero;
}
else
{
//flip bit
//red
//check LSB and FLIP
// img->pixel[count] = img->pixel[count] |= flipOne;
img->pixel[count+2] |= flipOne;
}

//mask--;
//eMsg[count] = img->pixel[count];
//printf("count: %d\n", count);
count = count + 3;
}
// eMsg[j] = byte;
}

//return encMsg = eMsg;
//unsigned char *yes = "sucess";
/*
for(int a = 0; a < messageLen; a++)
{
printf("pixel: %c", img->pixel[a]);
printf("msg: %c\n", eMsg[a]);
// eMsg[a] = img->pixel[a];
}
*/

embCount = count;
return embLen = embCount;
}

int main(int argc, char **argv){

int messageLen;
int i = 0;
PPMImage *img;
int size = 0;
FILE * fp;
int testSize;
fp = fopen(argv[4], "a+");

fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);

unsigned char *buff;
buff = (unsigned char*) malloc(sizeof(unsigned char)*size +1);
memset(buff, '\0', sizeof(unsigned char)*size+1);
fread(buff, 1, size, fp);

fclose(fp);
// printf("text encryption: %s\n", buff);
testSize = strlen(buff);
// printf("Size of text %d\n", testSize);

messageLen = strlen(buff);

img = readPPM(argv[2]);
/*
int testing = strlen(img->pixel);
for (int f=0;f<6;f++)
{
//f2 = 1
//f3 = 6
printf("%c", img->pixel[f]);
}
*/

// printf("%c \n", img->pixel[2]);
// printf("%c \n", img->pixel[5]);
printf("\n");
// unsigned char * encMsg;

int encMsg = 0;
encMsg = embBits(img, messageLen, buff, encMsg);

// printf("cipher msg:%s\n", img->pixel);
printf("message length: %d\n", messageLen);
// printf("cipher msg length: %d\n", encMsg);


writePPM(argv[6], img->pixel, img->x, img->y);

printf("Please press enter to complete\n");
getchar();
}

最佳答案

我不知道您具体对文件做了什么,并将每个位分配给数组中的位置,但您可以打印存储在unsigned int中的二进制序列。尝试使用这样的东西...

#include <stdio.h>

int main() {
unsigned int arr[] = {00110110, 00111100, 10111011};
int i = sizeof(arr)/sizeof(arr[0]);
while(i-->0) {
printf("%c, ", arr[i]);
}
printf("\n");
}

关于c - 如何将 LSB 提取的数据转换为文本或二进制转换为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37039843/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com