In C program how to write two short programs one which hides

In C program, how to write two short programs: one which hides a short, secret message in an image file, such as ppm file, and a second program which reads the hidden message back out of the image.

Solution

First program to write secret message

Encrypt.c

#include <stdio.h>
#include \"stego.h\"

void copy_header(FILE *, int, FILE *);
int get_message_length(char[]);
int message_fits(int, int, int);
int count_new_lines(FILE *);
void encode_length(FILE *, FILE *, int);
void encode_message(FILE *, FILE *, int, char *, int, int);

int main(int argc, char **argv) {
if(argc < 3) {
printf(\"requires 2 parameters the message within \\\" \\\" and the PPM file you want to encode the message in.\ Aborting\ .\");
return 1;
}

FILE *fp;
if((fp = fopen(argv[2], \"r+\")) == NULL) {
printf(\"Couldn\'t open file %s.\ Aborting\ \", argv[2]);
return 1;
}
  
if(read_ppm_type(fp)) {
skip_comments(fp);
  
char *myMessage = (char *)argv[1];
int message_length = get_message_length(myMessage);
int w = get_width(fp);
int h = get_height(fp);

if(message_fits(message_length, w, h)) {
if(read_color_depth(fp)) {
   FILE *fp_t = fopen(\"out.ppm\",\"w\");
   int i = count_new_lines(fp);
   copy_header(fp, i, fp_t);
encode_length(fp, fp_t, (message_length - 8) / 8);
   encode_message(fp, fp_t, (message_length - 8), myMessage, w, h);
   printf(\"Encoding Process Complete Take a look at out.ppm\ \");
  
   fclose(fp);
   fclose(fp_t);
} else {
   printf(\"\ Error: Image color depth not valid. Must be 255.\ \");
   return 1;
}
} else {
printf(\"\ Error: Image file is too large to print the message.\ Abort\ \");
return 1;
}
} else {
printf(\"Error: Wrong file format.\ Abort\ \");
return 1;
}

return 0;
}

void copy_header(FILE *fp1, int numLines, FILE *fp2) {
int i;
char temp;

rewind(fp1); //Goes back to the beginning of the file

for(i = 0; i < numLines; i++) {
while((temp = fgetc(fp1)) != EOF && temp != \'\ \') {
fputc(temp, fp2);
}
fputc(\'\ \', fp2);
}

return;
}
int get_message_length(char my_msg[]) {
int i = 0;
while(my_msg[i] != \'\\0\') {
i++;
}
return i * 8 + 8;
}
int message_fits(int length, int w, int h) {
return length < w * h * 3;
}
int count_new_lines(FILE *fp) {
char temp; int count = 0;

rewind(fp);

while((temp = fgetc(fp)) != EOF)
if(temp == \'\ \')
count++;

return count;
}

void encode_length(FILE *in, FILE *out, int length) {
char temp;
int i, l;
for(i = 0; i < 8; i++) {
temp = fgetc(in);
l = length;
l >>= 7 - i;
if((l & 1) == 1) {
   if((temp & 1) == 0) {
   temp++;
   }
} else {
   if((temp & 1) == 1) {
   temp--;
   }
}
fputc(temp, out);
}

return;
}

void encode_message(FILE *in, FILE *out, int num_to_encode, char* my_message, int w, int h) {
int encoded = 0;
unsigned char temp;
int idx = 0, num_coppied = 0;
char current;

int fileSize = (w * h * 3) - 8;
int i;

for(i = 0; i < fileSize; i++) {
temp = fgetc(in);
current = my_message[idx];
  
current >>= 7 - num_coppied;
num_coppied++;

if(encoded <= num_to_encode) {
encoded++;
if((current & 1) == 1) {
   if((temp & 1) == 0) {
   temp++;
   }
} else {
   if((temp & 1) == 1) {
   temp--;
   }
}
if(num_coppied == 8) {
   idx++;
   num_coppied = 0;
}
}
  
fputc(temp, out);
}

return;
}

second program which reads the hidden message

Decrypt.c

#include <stdio.h>
#include \"stego.h\"

int get_msg_length(FILE *);
void decode_message(int, FILE *);

int main(int argc, char **argv) {
FILE *fp;

//Print an error if no args provided
if((fp = fopen(argv[1], \"rb\")) == NULL) {
printf(\"\ Error: Please provide a file to scan\ \ \");
return 1;
}

if(read_ppm_type(fp)) {
skip_comments(fp);
get_width(fp);
get_height(fp);
if(read_color_depth(fp)) {
int length = get_msg_length(fp);

printf(\"\ secret message\ \");
decode_message(length, fp);

fclose(fp);

} else {
printf(\"Error:Invalid Color Depth.\");
return 1;
}
} else {
printf(\"Error:Wrong ppm File Format closing\");
return 1;
}

return 0;
}
//Gets the length of the secret message
int get_msg_length(FILE *fp) {
char temp = 0;
int length = 0;
int i;

for(i=0; i < 8; i++) {
temp = fgetc(fp);
if(i > 0) length <<= 1;
length |= (temp & 1);
}

return length;
}

void decode_message(int length, FILE *fp) {
int readTo = length * 8, numRead = 0, i;
unsigned char charBuffer = 0;
char temp;
char secret_message[length + 1];
int idx = 0;
  
while(numRead <= readTo) {
temp = fgetc(fp);
if(numRead % 8 == 0) {
secret_message[idx] = charBuffer;
idx++;
charBuffer = 0;
} else {
charBuffer <<= 1;
}
charBuffer |= (temp & 1);
numRead++;
}
for(i = 1; i < idx; i++) {
printf(\"%c\", secret_message[i]);
}

printf(\"\ \ \");

return;
}

In C program, how to write two short programs: one which hides a short, secret message in an image file, such as ppm file, and a second program which reads the
In C program, how to write two short programs: one which hides a short, secret message in an image file, such as ppm file, and a second program which reads the
In C program, how to write two short programs: one which hides a short, secret message in an image file, such as ppm file, and a second program which reads the
In C program, how to write two short programs: one which hides a short, secret message in an image file, such as ppm file, and a second program which reads the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site