The purpose of this exercise is
- Practice programming and mathematical calculations
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:
- User can only enter 10 characters and must enter all 10 characters.
- User will enter all characters in upper-case letters
(or have your program automatically convert the input to upper case).
- Assume the user entering information does so correctly. Only alphabet letters and spaces are entered.
Treat spaces the same as you do the alphabet letters (it works ok).
- After Z it should wrap back around to A
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).
- Prompt the user for a 10-character sentence.
- Create an empty string for the result.
- Read in the String from the user and store in a String variable.
- Get the first character from the String using charAt.
- Calculate the new value for the character.
You will need to consider the ASCII value and how it relates to the function!
- Append this character to the result string.
- Repeat 9 more times for each character in the string.
- Output to the user the converted string.
Part B : Decryption Algorithm
- Create a new class named Lab5Decrypt (in a file called Lab5Decrypt.java).
- Prompt the user for the encrypted string and decrypt it back
to the original.
You must show your GTA your work to get credit for this lab completion.
© 2008 CSU