CSCE 1030

advertisement
CSCE 1030
LAB 7: Simple Encryptor Machine
In this lab, you will create a simple encryption machine. You will exercise string manipulation in this
work.
Encryption is the process of encoding a clear text (also called plaintext) into ciphertext. The ciphertext
generated is unintelligible. So, given a plaintext in English, the ciphertext generated after encryption
will most probably be a meaningless sequence of letters.
As an example, assume that plaintext=”happyzone”. If we use an encryption algorithm that shifts each
plaintext character 3 symbols, then the ciphertext will be obtained as ciphertext=”kdssbcrqh”. Please
pay attention that we apply a circular shift, i.e. after letter ‘z’, the alphabet rolls over and we return
back to the beginning of the alphabet.
Basically, in order to calculate the index of the ciphertext letter, we get the index of the plaintext letter
in English alphabet, add 3 to it, and then take modulus 26 (since we need to role back to the beginning
of the alphabet). So, the general formula is:
cIndex = ( pIndex + 3 ) % 26
This encryption algorithm is called Caesar Cipher and has been used by Julius Caesar for hiding
military secrets in BC 50s!
In your program, you will read an arbitrary string from standard input, create a Caesar cipher of this
text and display the result.
IMPORTANT: For the sake of simplicity, you only have to accept the 26 lowercase English letters.
While coding this program, you will learn several new structures that are very practical for Java
programming. These structures are highlighted in yellow with following explanation.
Create a class called Encryptor.
You will need to include the following at the top of your program to use the Scanner class. You can use
the JOptionPane class from lab 2 as well.
import java.util.Scanner;
Use the following code to make your Encryptor class, filling in the missing details for yourself.
public class Encryptor {
// necessary declarations or initializations for your program
private String plaintext;
private char plain_char, cipher_char;
// Actual encryption method
public void convert() {
System.out.println("Enter the plaintext. English Alphabet characters ONLY please.");
// Read plaintext from standard input
Scanner reader = new Scanner(System.in);
plaintext = reader.nextLine();
/* Optional. To check if you got the plaintext correctly, you may display it
right here. If you are using a JOptionPane for input, you must display your
input somewhere.
*/
/* Now, you will process each plaintext letter one by one to encrypt and
find the corresponding ciphertext letter. For this, you need to know
the length of your arbitrary plaintext.
*/
int text_length = plaintext.length(); // You can calculate the length of
// an arbitrary string with the
// length() method of String class.
// Start processing the plaintext characters
for (int i = 0; i < text_length; i++) {
plain_char=plaintext.charAt(i); // charAt(position) method returns the character
// at a particular position in a given string.
cipher_char=findCipher(plain_char);
displayCipher(cipher_char);
}
}
private void displayCipher(char ccc) {
// Display your ciphertext letters one by one to the standard output
}
private char findCipher(char pp) {
/* Find the ciphertext letter counterpart of your input plaintext letter
Hint: char is a numerical type. Look up an ASCII character table
Then return the ciphertext letter found.
*/
}
} // end of encryptor class
After
you
have
created
the
Encryptor
class,
create
another
class
called
TestEncryptorYourNameLastName that contains main. (The king’s class would then be named
TestEncryptorElvisPresley).
Inside of main, create an object of class Encryptor by the following:
Encryptor myEncryptor = new Encryptor();
Finally, call the convert() method of your Encryptor class ( you need to use the object that you have
created in the previous step for that, right? So, type the following in your main class:
myEncryptor.convert();
Test your program to make sure it is working correctly. It is really fun to see yourself creating ciphers
of plaintexts!!
Print a screenshot of your output and a printout of your source code. Turn these printouts in to your
lab instructor. Do your own work.
All labs are due at the end of the next lab class (you have one week to complete each lab). After this, you have one
additional week for 50% maximum credit. After this additional week, no late assignments will be accepted.
Please do not store your files on the lab machine. They will be deleted and you will lose your work.
Please include comments in all code, including your name, lab time, lab instructor(s), and the lab number. You are
encouraged to help your classmates with the assignment, but please do not share code. Any duplicate or near
duplicate submissions earn no credit for ALL parties involved. Anti-plagiarism software is in use.
Download