Colorado State University

CS160: Foundations in Programming

Lab 5

Java programming


The purpose of this exercise is
Instructions: Your GTA will lead you through the following:

Encryption Algorithms

The Caesar cipher is the first and most famous encryption algorithms. Julius Caesar made his messages secret by shifting each letter three letters forward in the alphabet (see Cryptology in Rosen book). We're going to create two programs: one for encryption and one for decryption.

The mathematical function for Caesar's encryption algorithm is:

f(p) = (p+3) mod 26

where the last three letters wrap around to the first three letters.

In order for us to make use of this function, we need to know that each character can be represented as an int in its ASCII value (see Rosen text). So given a variable named letter we can convert it to an int via the following assignment statement:

	int asciiVal = (int) letter;
Now, to keep our program simple since we still have a lot to learn about Java syntax, we're going to require the following things: Note: upper-case letter 'A' has an ASCII value of 65. This is important as it will have an effect on your cipher formula! You can either just memorize that 'A' is 65, or, you can do something like this: int first='A'; and then use first instead of 65. Which will make your program easier to understand?

The next thing we'll need is how to handle the String. You'll need to read in a string from the user. Since we don't know loops yet or if-statements, we're going to have our code process 10 characters every time. You are NOT allowed to use loops or if statements. To get a character out of the string, we can call the method:

	.charAt( index )
on our String variables. Remember, Java starts counting at zero so the first character will be at index 0.

Important! Get ONE letter to work first, then copy-paste and modify for all subsequent letters.


Part A : Encryption Algorithm

Create a new class named Lab5Encrypt (in a file called Lab5Encrypt.java).

Part B : Decryption Algorithm


You must show your GTA your work to get credit for this lab completion.
© 2008 CSU