15-123 Effective Programming in C and UNIX
Lab 5 – Image Encryption with BMP Images
Due Date: Saturday March 28th, 2009 by 11:59pm
Background:
Encryption is a method used to protect data from intruders during transmission. The idea of data
encryption is to scramble data using a specific key, so attackers would need to know the key to
decrypt the data. The reverse of data encryption is called decryption. Decryption algorithm will
recreate the original data file. For example, a naïve encryption of an ASCII file is to encrypt each
character just by adding 1 to each character. That is replacing each character by its successor in
the ASCII table. So ‘a’→‘b’ , ‘b’→‘c’, …, ‘t’→’u’. So the word “bat” would be encrypted as
“cbu”. Now applying the decryption algorithm (or just subtracting one from each of the
encrypted characters “cbu” or by replacing with its predecessor will produce the original word
“bat”. Although there are number of research results available on image encryption, our goal in
this assignment is not to use one of the more advanced algorithms, but try out number of simple
algorithms, some of your choosing, that would allow us to work with binary files, do some bit
manipulations, encode and decode image files. For simplicity we will consider only the
uncompressed image formats like BMP (jpeg and gif are compressed forms of images). Here is
some background information about BMP’s.
The BMP file format:
The images we are planning to encrypt are in the BMP file format, which is one of the most
popular uncompressed file formats for images.
Note that you will need to find a way to view the images for this assignment. I recommend
writing and testing the program on Andrew Unix and using SSH File Transfer Client to view the
files on the local machine using MS Paint to see if the encryption is working. If you are working
on a Linux cluster machine, you can also use the command “display” ($ display
image.bmp) to view the image assuming x terminal is installed.
Your program should be able to encrypt any bitmap file of size n by m pixels. The actual size of
a BMP image of n by m pixels is given by (3·n·m + 54+ some extra bytes) bytes. Each BMP file
allocates 54 bytes for header information, and each pixel takes up 3 bytes for Red, Green and
Blue (RGB). Extra bytes may be used for padding purposes. We should not encrypt the BMP
header or the extra bytes. You should just read the first 54 bytes into an array, and when you
write it out again, you should just write the header bytes unchanged. The pixels of a color image
are stored in RGB format, which means that each pixel is represented by three bytes, one for red,
one for green, and one for blue. One byte can store numbers in the range 0-255. So the color red
is (255, 0, 0) and green is (0, 255, 0). And (128, 0, 0) make dark red, and (255, 0, 255) makes
purple since purple is red + blue. It is important to note that the pixels are stored from left to
right and from bottom to top (upside down!). So the first m groups of three bytes (3·m bytes) in
the file (after the 54-byte header) correspond to the bottom row of the image, and the last m
groups of three bytes correspond to the top row. Also make sure that you figure out whether
image has extra bytes or not.