Steganography in C

Steganography in C Homework Sample

Steganography is the way to hide data in plain sight. If you encrypt a file to hide it, it is obvious as you can see a file that can’t be read. Instead steganography takes a file, and hides it inside another file, in this case a black and white image inside a color image. We have provided functions to read and write PNG files. You read in the image you want to encode and the image you want to hide. The hidden image should set the bottom bit of the pixels. For more C programming assignments contact us for details.

Solution:

main.c

#include “hw4_provided.h”

void hide_image(
unsigned int ref_r[ROWS][COLS],
unsigned int ref_g[ROWS][COLS],
unsigned int ref_b[ROWS][COLS],
unsigned int hid_r[ROWS][COLS],
unsigned int hid_g[ROWS][COLS],
unsigned int hid_b[ROWS][COLS],
unsigned int res_r[ROWS][COLS],
unsigned int res_g[ROWS][COLS],
unsigned int res_b[ROWS][COLS],
unsigned int height, unsigned int width)
{
for (int r = 0; r < height; r++)
for (int c = 0; c < width; c++)
{
if (hid_r[r][c] >= 128)
{
if (ref_r[r][c] % 2 == 0)
res_r[r][c] = ref_r[r][c] + 1;
else
res_r[r][c] = ref_r[r][c];
}
else
{
if (ref_r[r][c] % 2 == 0)
res_r[r][c] = ref_r[r][c];
else
res_r[r][c] = ref_r[r][c] + 1;
}
res_g[r][c] = ref_g[r][c];
res_b[r][c] = ref_b[r][c];
}
}

void extract_image(
unsigned int res_r[ROWS][COLS],
unsigned int res_g[ROWS][COLS],
unsigned int res_b[ROWS][COLS],
unsigned int hid_r[ROWS][COLS],
unsigned int hid_g[ROWS][COLS],
unsigned int hid_b[ROWS][COLS],
unsigned int height, unsigned int width)
{
for (int r = 0; r < height; r++)
for (int c = 0; c < width; c++)
{
if (res_r[r][c] % 2 == 0)
{
hid_r[r][c] = 0;
hid_g[r][c] = 0;
hid_b[r][c] = 0;
}
else
{
hid_r[r][c] = 255;
hid_g[r][c] = 255;
hid_b[r][c] = 255;
}
}
}

void encode(char *ref_filename, char *hid_filename, char *enc_filename)
{
unsigned int ref_red[ROWS][COLS];
unsigned int ref_green[ROWS][COLS];
unsigned int ref_blue[ROWS][COLS];
int ref_width;
int ref_height;

provided_read_png(ref_filename, ref_red, ref_green, ref_blue, &ref_width, &ref_height);

unsigned int hid_red[ROWS][COLS];
unsigned int hid_green[ROWS][COLS];
unsigned int hid_blue[ROWS][COLS];
int hid_width;
int hid_height;

provided_read_png(hid_filename, hid_red, hid_green, hid_blue, &hid_width, &hid_height);

// chec hid_ and ref_ sizes
if (hid_width == ref_height && hid_height == ref_height)
{
unsigned int enc_red[ROWS][COLS];
unsigned int enc_green[ROWS][COLS];
unsigned int enc_blue[ROWS][COLS];
int enc_width = hid_width;
int enc_height = hid_height;

hide_image(ref_red, ref_green, ref_blue, hid_red, hid_green, hid_blue, enc_red, enc_green, enc_blue, enc_height, enc_width);

provided_write_png(enc_filename, enc_red, enc_green, enc_blue, enc_width, enc_height);
}
}

void decode(char *enc_filename, char *hid_filename)
{
unsigned int enc_red[ROWS][COLS];
unsigned int enc_green[ROWS][COLS];
unsigned int enc_blue[ROWS][COLS];
int enc_width;
int enc_height;

provided_read_png(enc_filename, enc_red, enc_green, enc_blue, &enc_width, &enc_height);

unsigned int hid_red[ROWS][COLS];
unsigned int hid_green[ROWS][COLS];
unsigned int hid_blue[ROWS][COLS];
int hid_width = enc_width;
int hid_height = enc_height;

extract_image(enc_red, enc_green, enc_blue, hid_red, hid_green, hid_blue, hid_width, hid_height);

provided_write_png(hid_filename, hid_red, hid_green, hid_blue, hid_width, hid_height);
}

int main()
{
encode(“happyface.png”, “happyface_hide.png”, “out.png”);
decode(“out.png”, “hide.png”);
return 0;
}