Question
in java please One of the earliest known forms of encryption is called a Caesar Cipher. Used by Julius Caesar in around 100BC to encrypt
in java please
One of the earliest known forms of encryption is called a Caesar Cipher. Used by Julius Caesar in around 100BC to encrypt messages to his armies, its a very simple form of encryption. Take each letter of the alphabet and shift it three letters to the right. Youd encrypt the letter a to the letter d (because d is three letters to the right of a in the alphabet). b would become e, c would become f... w would become z, x wraps around to a, y wraps around to b, and z wraps around to c.
As an example, if you took the plaintext: class, it would encrypt as: c -> f l -> p a -> d
s -> v s -> v
So the ciphertext would be fpdvv.
Your task:
Begin with the following class definition:
C# | Java |
using System.Collections.Generic; class Caesar { List public Caesar() { for(char letter='a';letter<='z';letter++) { alphabet.Add(letter); } } } | import java.util.ArrayList; class Caesar { ArrayList alphabet = new ArrayList(); public Caesar() { for(char letter='a';letter<='z';letter++) { alphabet.add(letter); } } } |
This creates an arraylist which has one letter in each cell. From here youll be adding methods:
-
1) Add a method position_of_char which takes in a character and returns the index of that character in the arraylist. i.e a would return 0, b should return 1.
-
2) Add a method letter_at_pos which takes in an integer and returns the character in that cell. i.e. 0 should return a, 1 should return b.
-
3) Add a method encrypt_char which takes in a character and returns the encrypted version of that character. The pseudocode for this method is:
-
a) Convert the letter that was passed in as a parameter into a number. Do this by calling the position_of_char above.
-
b) Calculate the new index. Add 3 to the number you got in step a. If the result is 26 or greater, subtract 26.
The reason for this subtraction is that x => 23. 23+3 = 26, but there is no index 26 in the alphabet array, only 0-25. When we subtract 26, we get 0, which is a. Thus x encrypts to a.
-
c) Lookup the encrypted letter by passing the new index you calculated in step b to letter_at_pos
-
d) Return the encrypted letter.
-
-
4) Add a method decrypt_char which takes in a character and returns the decrypted
version of that character. The pseudocode for this method is:
-
a) Convert the letter that was passed in as a parameter into a number. Do
this by calling the position_of_char above.
-
b) Calculate the new index. Subtract 3 from the number you got in step a. If
the result is less than 0, add 26 to it.
The reason for this addition is that a => 0. 0-3 = -3, but there is no index -3 in the alphabet array, only 0-25. When we add 26, we get 23, which is x. Thus a decrypts to x.
-
c) Lookup the decrypted letter by passing the new index calculated in step b to letter_at_pos
-
d) Return the decrypted letter.
-
-
5) Add a method encrypt, which takes in a string and returns the encrypted version
of that string. Do the following steps a) Create an empty string called ciphertext. Initialize it to an empty string.
b) Write a for loop, which goes from 0 to the length of the string which was passed in. You can find the length of a string in Java with the .length() method, while in C# youll access the .Length attribute.
-
i) Extract one letter from the string at the position of the loop index. I.e. on the first iteration of the loop, youll extract the first character from the string. In Java you might need the .charAt(i) method, while in C# you can access the string as if its an array like this: string[i].
-
ii) Use the encrypt_char method to change that one character from plaintext to cipher text.
-
iii) Add the new letter that was returned from encrypt_char to your ciphertext string.
c) Return the Ciphertext string. 6) Add a method decrypt, which takes in a string of encrypted text and returns the
plaintext. Youll follow the same steps as encrypt, but this time call decrypt_char instead of encrypt_char.
Driver Program:
Create an object of the class Caesar.
Prompt the user with a menu as follows:
1 Encrypt a message 2 Decrypt a message 3 Quit
If the user chooses 1, allow them to enter a message, convert the message to lowercase (hint: Java: .toLowerCase(), C#: ToLower()), pass the result to the encrypt method, and then print the encrypted message.
If they choose 2, allow them to enter an encrypted message, convert it to lowercase, then pass it to the decrypt method and print the plaintext.
Note: This program cannot encrypt numbers, spaces or punctuation. If you enter anything other than a-z, youll likely get odd output or it may crash. You dont have to solve that problem for this assignment.
Sample Output:
-
1 Encrypt a message
-
2 Decrypt a message
-
3 Quit
1
What is the message to encrypt?
ScrappyTheOwl
Encrypted: vfudssbwkhrzo
-
1 Encrypt a message
-
2 Decrypt a message
-
3 Quit
2
What is the encrypted message?
vfudssbwkhrzo
Plaintext: scrappytheowl
-
1 Encrypt a message
-
2 Decrypt a message
-
3 Quit
3
If the user chooses 1, allow them to enter a message, convert the message to lowercase (hint: Java: .toLowerCase(), C#: ToLower()), pass the result to the encrypt method, and then print the encrypted message.
If they choose 2, allow them to enter an encrypted message, convert it to lowercase, then pass it to the decrypt method and print the plaintext.
Note: This program cannot encrypt numbers, spaces or punctuation. If you enter anything other than a-z, youll likely get odd output or it may crash. You dont have to solve that problem for this assignment.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started