Click here to Skip to main content
16,015,274 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

Then their values after the insertion of an 'A' will be:
10000000.10100101.10110100,
10110100.11110010.10110110, 
11100110.10110011.00110011


What I have tried:

C
#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;
}
Posted
Updated 5-Feb-21 22:12pm
v4
Comments
Member 12835561 18-Apr-18 6:03am    
answer for the above given code

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!
 
Share this answer
 
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;

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
  1. if (v & 0x80) then b[n] |= 0x01 else b[n] &=0xF7
  2. v <<= 1; ++n
  3. if (n<0) then goto 1
  4. comlpeted
 
Share this answer
 
Not sure if this helps, but i saw a link which did the same but with some other meyhod i think.

Link: here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900