CS240 Lab 4
Text Encryption

Overview

In this lab you will be encrypting text strings into binary digits series.

Background

Encryption is used in Information Security, which means protecting information and information systems from unauthorized access, use, disclosure, disruption, modification or destruction. For example, when you send an E-mail to your friend, you may hope that only he/she can read it. But, by some means, it can be intercepted by others. In that case, how do you protect the content of the email? One of the methods is encryption. You can convert the email from plain English to ciphered text which can only be understood by you and your friend. If you're interested in reading about this more you can view this Wikipedia article.

Now let's see a simple example of doing encryption. In C program, when you use getchar() to get a character from the input, actually you get an integer which corresponds to that ASCII letter. As all of you have already known from previous labs, the character '0' corresponds to the integer 48. For English letters, you can also find the corresponding integers. For example, 'a' corresponds to 97. And it stores in the memory as a series of binary bits. For 'a',the binary bits are '01100001'. So, a straightforward way of encryption is translating the English letters to binary bits series, because they seem meaningless and hard to read. However, if the intercepter is also taking the C programming course, and has the same knowledge as you, then he/she only needs a simple operation to decrypt it. Therefore, you may want to modify the binary series a little to make it harder to be decrypted. For example, you can swap the last four digits of the 8-digit binary bits series (the character only needs 1 bytes, though we use a 4-bytes int to store it) with the first four digits. Then, the binary bits of 'a' becomes '00010110'. After this operation, even if the intercepter can transform the binary bits to an integer and display the ASCII letter, he/she still cannot read it, because it is not an English letter anymore.

Lab Exercise

Write a program that converts the input, a series of characters, to binary bits. You should prompt for the input text with the message:

Please enter the input text:
Then translate the input string into binary bits. You also need to swap the first four digits and the last four digits of each character. You can assume that all input characters are English letters and 'spaces'. Finally, display the binary bits on the screen. To make the output clearer, you need to insert a 'space' after the binary bits series of each character. Note that when transforming the integers to binary bits series, you are not allowed to use the mod (%) operation.

Sample run would look like this:

Please enter the input text: hello world
10000110 01010110 11000110 11000110 11110110 00000010 01110111 11110110 00100111 11000110 01000110

NOTE: You may only use getchar() to retrieve input in this lab. Using any other method will result in an automatic zero. New line character will serve as indication of the end of the input.

Tips and Hints

You can use "%x" insdead of "%d" in printf() to display the hexadecimal number of the intger when debugging. It will help you make sure that you are doing the swapping operation correctly. In addition, since you are not allowed to use the mod (%) operation to obtain the binary bits series, you may consider using bits operations in both translating and swapping tasks. Following are some useful expressions which will come in handy for the lab:

int a, b;
char c;
a = a << 8;         /* shifts 8 bits of integer a to left */
a = a | c;          /* inserts 8 bits of c to last(rightmost) 8 bits of a */
                    /* (a is 32 bits, and the last 8 bits become zeros    */
                    /* after the last step)                               */
b = a & 0xff000000; /* assigns the value of 1st 8 bits from left of a to b */

Turnin

When you are satisfied that your program works correctly (or you run out of time), please do the following.

  1. Create a directory named lab04
  2. Copy all program files (*.c and *.h) and your Makefile to the directory created in step 1.
  3. Goto the parent directory of lab04 and type the following command into the terminal:
    turnin -c cs240=XXXX -p lab04 lab04

    where XXXX represents your lab section number.

    The turnin section is as follows:

    Section Time TA
    0201 Thursday 15:30-17:20 Dan Zhang
    0301 Friday 09:30-11:20 Suli Xi
    0401 Friday 13:30-15:20 Youhan Fang
    0501 Thursday 09:30-11:20 J. C. Chin

    Make sure turnin reports that your project was submitted for grading. You can check the files you have submitted by running the following command:

    turnin -c cs240=XXXX -v -p lab04
  4. Submit often to make sure that you did not try to submit after turnin has been turned off.

Grading Breakdown

2 points A working Makefile is provided.
3 points Program gets user input correctly.
7 points Swap the bits correctly.
8 points Display the binary bits correctly.