Reciprocal Cipher

Create your GitHub repo for this lab here: https://classroom.github.com/a/hWcjSqtY

Everyone knows that computer security is so hot right now, so you want in on the action. You decide to create your own encryption algorithm. The only problem is, you don’t actually know much about encryption… Standard advice in the crypto community is that you should always write your own encryption libraries, never using off-the-shelf solutions vetted by experts. What do they know anyway?! cough cough

You find an article on ROT-13 and inspiration takes hold: you’ll use a reciprocal letter substitution cipher to encrypt your data!

Learning Objectives and Hints

In all seriousness, the goal of this lab is to get more familiar with pointers. You’ll create a function called rotate that looks like this:

int rotate(int *ch, int start, int end);

Its parameters represent a pointer to a single character to operate on, as well as a start and end of the range of characters to rotate through. Check out an ASCII table to get an idea of the numeric representation of each letter. That’s how the characters are represented in memory. ‘a’, for instance, is 97 in ASCII, and ‘z’ is 122. This means that if you call rotate(&c, 'a', 'z');, it is equivalent to rotate(&c, 97, 122);.

Let’s work a small example. Say the character in question we want to rotate is f. If our ASCII range is 97 through 122 (inclusive), the total number of letters we have is 26: ((122 + 1) - 97). To rotate half way through the range, we’d move 13 characters: 26 / 2. Since f is 102 in ASCII, we’d get 115: 102 + 13, which corresponds to the ASCII letter s.

After rotating, the neat thing about a reciprocal cipher is that we can rotate it again to get our original string back. 115 + 13 = 128, which is outside our character range. However, if we use the modulus operator (%) we can “wrap back” to the beginning again: (115 + 13) % (122 + 1) + 97 = 102, or f.

Getting Started

cipher.c provides more detail on your encryption scheme. The starter code also provides a basic rotation example, so it’s your job to enhance it to support different ranges of characters.

You can use make to compile the program. Here are a few examples on how to run it:

# Encrypts 'file.txt':
./cipher < file.txt

# Encrypts the string 'hello world':
echo "hello world" | ./cipher

# Encrypts and saves the output to a file:
./cipher < file.txt > encrypted.txt

# Decrypts a file (same as encrypting):
./cipher < encrypted.txt

Testing Your Code

You can run make test to run all the test cases, or make test run=X to run a specific test case, where X is the test case number.

If the test cases are updated, you can pull in the changes with make testupdate.

Submission

Check your code into your Github repository.