我如何...使用LSB算法在C中进行图像隐写?

编程入门 行业动态 更新时间:2024-10-09 15:16:40
本文介绍了我如何...使用LSB算法在C中进行图像隐写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以帮助我使用基于C语言的图像隐写术的项目。我需要使用最低有效位算法将用户数据隐藏到BMP图像中。我知道算法是什么,但我没有得到如何实现它。 请帮我用C语言的LSB技术将数据加密成BMP图像。 LSB算法: 字母'A'的ASCII码为65(十进制), 这是二进制的01000001。 24位图像需要连续三个像素来存储'A': 让我们说插入前的像素是:

Can someone help me with my project based on image steganography in C language only. I need to use Least significant bit algorithm to hide the user data into a BMP image. I know what the algorithm is but i am not getting how to implement it. Please help me to encrypt the data into a BMP image using LSB technique in C language. LSB algo: The letter 'A' has an ASCII code of 65(decimal), which is 01000001 in binary. It will need three consecutive pixels for a 24-bit image to store an 'A': Let's say that the pixels before the insertion are:

R G B P1-10000000.10100100.10110101, P2-10110101.11110011.10110111, P3-11100111.10110011.00110011

然后在插入'A'后它们的值将是:

Then their values after the insertion of an 'A' will be:

10000000.10100101.10110100, 10110100.11110010.10110110, 11100110.10110011.00110011

我尝试了什么:

What I have tried:

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> #include<string.h> long asciiToBinary(int n); long asciiToBinary(int n) { int remainder; long binary = 0, i = 1; while(n != 0) { remainder = n%2; n = n/2; binary= binary + (remainder*i); i = i*10; } return binary; } typedef struct { unsigned int width; unsigned int height; unsigned int size; } BITMAPINFOHEADER; typedef struct { unsigned char blue; unsigned char green; unsigned char red; } PIXEL; int main(){ FILE *image; char fpath[1000],mydata[100]; BITMAPINFOHEADER bih; int i=0,b[8],g[8],r[8]; double asciiTobinary; printf("Enter BMP file path"); scanf("%s",fpath); image=fopen(fpath,"rb"); while(image==NULL){ printf("Error! Enter path again:"); scanf("%s",fpath); } fseek(image,2,SEEK_SET); //reading the height and width fread(&bih.size,4,1,image); printf("\n \n Size of the image=%d\n",bih.size); fseek(image,18,SEEK_SET); fread(&bih.width,4,1,image); fseek(image,22,SEEK_SET); fread(&bih.height,4,1,image); printf("\n \n Width of the image =%d \n Height of the image =%d \n pixel = b | g | r \n \n",bih.width,bih.height); PIXEL pic[bih.width*bih.height*2],p; while(!feof(image)){ //reading the pixels rgb values fread(&p.blue,sizeof(p.blue),1,image); fread(&p.green,sizeof(p.green),1,image); fread(&p.red,sizeof(p.red),1,image); pic[i]=p; printf(" %d= %u | %u | %u ",i+54,pic[i].blue,pic[i].green,pic[i].red); i++; } fclose(image); return 0; }

推荐答案

首先,使用空格将路径与数据分开是个坏主意:如果路径以C:\ Users \ MyName \ Myy Documents \ ...开头? 在两条不同的行上获取路径和数据,并为每个行使用单独的提示(和在你要求数据之前验证路径是否正确。 这是你的作业,而不是我的 - 所以我不会给你任何代码! 但是......但是你这样做,你需要两个文件:一个输入,一个输出 - 所以为目的地添加第二个提示,然后打开一个可写文件。当您读取输入文件时,您需要将数据复制到输出文件,仅修改像素数据 - 这很重要,因为没有标题信息,您的新文件将无法正常工作...... 然后你需要开始修改数据:这意味着你需要传递隐藏数据,一次提取一点,并替换每个颜色值的最低有效位与隐藏的一个。当你用完时,将文件的其余部分复制到输出。 我?我读取了块,理想情况下使用的块大小是隐藏数据字节数的八倍。这样,您可以在一个数组中处理整个数据,并可以使用一条指令读取和写入所有数据。文件的其余部分只是一个读写循环,直到你用完数据。 试一试:这很简单! First off, it's a bad idea to use a space to separate the path from the data: what if the path starts with "C:\Users\MyName\My Documents\..."? Fetch path and the data on two separate lines, and use separate prompts for each (and verify the path is correct before you ask for the data). This is your homework, not mine - so I'll give you no code! But...however you do it, you need two files: one input, one output - so add a second prompt for the destination, and open a writeable file for that. As you read the input file, you need to copy the data to the output file, modifying only the pixel data - this is important, because without the header info, your new file won't work... Then you need to start modifying the data: this means you need to pass though the "hidden data", extracting it a bit at a time, and replacing the least significant bit of each colour value with the hidden one. When you run out, copy the rest of the file to the output. Me? I'd read in chunks, ideally a using chunk size eight times the size of the "hidden data" byte count. That way, you process the whole data in one chunk in an array and can read and write it all with one instruction. The rest of the file is just a read-write loop until you run out of data. Give it a try: it's pretty simple stuff!

Quote:

typedef struct { unsigned char blue; unsigned char green; unsigned char red; unsigned char bluearray [8]; unsigned char greenarray [8]; unsigned char redarray [8]; } PIXEL;

typedef struct { unsigned char blue; unsigned char green; unsigned char red; unsigned char bluearray[8]; unsigned char greenarray[8]; unsigned char redarray[8]; } PIXEL;

bluearray,greenarray的目的是什么? redarray 会员?我想你不需要它们。 你还必须确保输入图像每像素使用24位并正确处理步幅(参见 BMP文件格式 - 维基百科 [ ^ ]。 一旦你有了图像字节,隐写术过程很简单,从第一个图像字节开始,比如 b [ n] , n = 0 ,以及要存储的值,例如 v

What's the purpose of the bluearray, greenarray, redarray members? I guess you don't need them. You also have to make sure the input image uses 24 bits per pixel and handle properly the stride (see BMP file format - Wikipedia[^]). Once you have the image bytes the steganography process is simple, starting from first image byte, say b[n] with n=0, and the value to store, say v

  • 如果(v& 0x80)则b [n] | = 0x01否则b [n]& = 0xF7
  • v<< = 1 ; ++ n
  • if(n< 0)then goto 1
  • comlpeted
  • 更多推荐

    我如何...使用LSB算法在C中进行图像隐写?

    本文发布于:2023-11-28 21:35:05,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1644025.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:算法   图像   LSB

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!