Assignment 6: Secret messages

Friday, March 4, before midnight

The goals for this assignment are:

  • Understand binary representations of data types

  • Directly manipulate basic types at the bit level

  • Work with bitwise operators: |, &, ~, ^

Background: Steganography

Steganography is the practice of hiding messages in plain sight. In this question, we will hide text messages using the least significant bits of an image.

Background

Credit: Chris Trailie (Original)

To understand how we will hide messages in the least significant bits of an image, let’s look at the following write-up by Chris Trailie, starting with the two pictures below.

Ordinary image

Hidden message

The picture on the right contains 12 paragraphs of text on the Ursinus 150 strategic plan. Can you see the difference? No? Well great, that's the point!

So how do we do this? The idea is beautifully simple, and is best understood with an example. Consider the following 3-pixel image

[254, 119, 50] [2, 141, 254] [91, 159, 64]

We're going to extract a binary signal by looking at the least significant bit (the 1's place in binary) of each color channel in each pixel from left to right from red, to green, to blue, and put them together into one binary string. In other words, for a particular pixel and a particular color channel, we'll extract a 0 if it's an even number and a 1 if it's an odd number. Let's look at the first 8 bits in the above image. We have

2540
1191
500
20
1411
2540
911
1591

All together, this is the binary string 01001011, which is the character 'K' in ASCII. What if we wanted to change it to some other character though? Perhaps the character 'z', which is 0x7A hex, or 01111010 in binary. Then we can just tweak the 1's place of the pixel values as follows, where I've bolded the ones that have changed:

2540
1191
511
31
1411
2540
911
1580

Here's what these updated values look like

[254, 119, 51] [3, 141, 254] [91, 158, 64]

If you were just looking at it and comparing it to what we started with, you would never notice the difference! So we have freedom to tweak the least significant bit of every color channel of every pixel at will to encode text, and this is exactly what you will be doing in this assignment!. In a 500x500 image, for example, this means we can store 250,000 bits. Since each ASCII character is 8 bits, this is 31,250 characters total, or roughly about 6000 words.

1. Decode

In the file, decode.c, write a program that reads in a PPM file (raw, or binary, format) and then outputs any message that might be stored in the least significant bits of each color. Your program should read bits from each of the red, green, and blue colors — top to bottom, left to right. You should keep decoding until you reach the empty character \0.

For example, consider the example file tiny_encoded.ppm. Decoding should give you the bits 001100010011011100110100001000010000101000000000.

If we group them up into ASCII char variables, we see this is the character string 174!\n.

00110001'1'
00110111'7'
00110100'4'
00100001'!'
00001010'\n'
00000000'\0'

Your program should perform as follows.

$ ./decode tiny_encoded.ppm
Reading tiny_encoded.ppm with width 4 and height 4
Max number of characters in the image: 6
174!

Requirements/Hints:

  • You should read the PPM filename as a command line argument

  • You should report the usage if no file is given to the program

  • You should report an error if the file cannot be read

  • Re-use your implementation of read_ppm from Assignment 05.

  • Output the size of the image along with the maximum number of characters it can store

  • For debugging, try printing out values in hex format, e.g. printf("%02X", c);

  • ppm_pixel (defined in read_ppm.h) allows you to access each color either by name, or by array. You may find it simpler to use the array representation for this question.

2. Encode

In the file, encode.c, write a program that reads in a PPM file (raw, or binary, format) and asks the user for a message to embed within it.

$ make encode
gcc -g -Wall -Wvla -Werror encode.c read_ppm.c -o encode
$ ./encode feep-raw.ppm
Reading feep-raw.ppm with width 4 and height 4
Max number of characters in the image: 5
Enter a phrase: lol
Writing file feep-raw-encoded.ppm

Requirements/Hints:

  • You should read the PPM filename as a command line argument

  • You should report the usage if no file is given to the program

  • You should report an error if the file cannot be read

  • You should output a new file based on the input name. For example, if the input is feep-raw.ppm, the new file with the encoded message should be feep-raw-encoded.ppm.

  • Re-use your implementation of read_ppm from Assignment 05.

  • Output the size of the image along with the maximum number of characters it can store

  • For debugging, try printing out values in hex format, e.g. printf("%02X", c);

  • ppm_pixel (defined in read_ppm.h) allows you to access each color either by name, or by array. You may find it simpler to use the array representation for this question.

3. Submit your work

Push you work to github to submit your work.

$ cd A01
$ git status
$ git add *.c
$ git status
$ git commit -m "assignment complete"
$ git status
$ git push
$ git status