Java代写:COMP10062 Encrypted Sentences

代写Caesar Cipher算法,对文本进行加解密操作。

Requirement

In this assignment you will implement a simple encryption method (the Caesar Cipher) based on an encryption key.

How to Encrypt

A different key is computed for each sentence in the message.

Computing the Key for a Sentence

  1. If the message is empty, return 0. Otherwise
  2. Count the number of words in the sentence. Words are separated by one or more spaces.
  3. Count the number of non-letter characters in the sentence.
  4. Count the number of occurrences of the first character in the sentence (case-insensitive).
  5. Add the above three numbers to get the key.
  6. If the key turns out to be a multiple of 26, add 1 to it.

Encrypting the Message

To encrypt a message, proceed one sentence at a time. For each sentence, compute the key, then go through the sentence one character at a time and use the key to shift each letter or digit character (a-z, A-Z and 0-9), wrapping around for each range when you get to the end. Non-alphanumeric characters should not be touched.

For example, if the key is 3:

  • ‘a’ becomes ‘d’
  • ‘D’ becomes ‘G’
  • ‘7’ becomes ‘0’ (it wraps around)
  • ‘Y’ becomes ‘B’ (wraps around)
  • Etc.

Example Sentence: “Computer class (COMP10062) is cool.”

  1. Compute the key: 5+12+4 = 21
  2. Encrypt the sentence: “Xjhkpozm xgvnn (XJHK21173) dn xjjg.”

How to Decrypt

If you have implemented the encryption properly, each encoded sentence should have the same key as it did before it was encoded. Decryption involves computing the key for the sentence and then rolling the letters and digits backwards instead of forwards.

The Assignment

For this assignment, you will create a FileConverter class that can read a file, separate it into sentences (with the assumption that a ‘.’ character always terminates a sentence), store it as an array of Sentence objects, and write its encrypted (or decrypted) version into a new file.

The class diagram for this assignment is shown below. Only public methods are shown - you are free to add whatever private methods you feel you need, but you may not add any public methods or change the interface of the public methods shown here.

The arrow in the diagram above is an association arrow. The * symbol on the arrow means that 0 or more Sentence objects are associated with a FileConverter object. These sentence objects must be stored in a private Array variable inside the FileConverter object.

Each class is discussed separately below.

The Sentence Class

This class holds a single sentence, represented as a String terminated with a ‘.’ Character. It has a key field which holds the key computed for this sentence (see above for the details) and an encrypted field which is true if the sentence is currently encrypted.

  • There are two constructors - the first takes only the sentence as a parameter (and assumes it is not encrypted). The constructor should also compute the key.
  • The toggleEncryption() method will either encrypt or decrypt the sentence (depending on the state of the encrypted field) and then update sentence and encrypted.
  • The toString() method should just return the sentence field (in its current form, either encrypted or decrypted).
  • The getKey() and isEncrypted() method are standard getter methods for their corresponding fields.
  • You can add private methods as you see fit.

You should write this class first, and then test it. Here’s a sample main method you could use to test it:

1
2
3
4
5
6
7
8
9
public static void main(String[] args) {
Sentence s = new Sentence("Computer class (COMP10062) is cool.");
System.out.println(s.getKey());
System.out.println(s);
s.toggleEncryption();
System.out.println(s);
s.toggleEncryption();
System.out.println(s);
}

The output of this method should be:

21
Computer class (COMP10062) is cool.
Xjhkpozm xgvnn (XJHK21173) dn xjjg.
Computer class (COMP10062) is cool.

But you should try other test cases as well.

The FileConverter Class

The constructor of the FileConverter class receives the name of an input and output file along with a boolean flag indicating whether or not the input file is already encrypted.

  • The readInput() method should open the input file named in the constructor call, read its contents, separate it into sentences (assuming that every ‘.’ Character ends a sentence) and store the result in an array of Sentence objects. Exceptions should be caught. Print an error message and return false if the method is not successful. Otherwise return true.
  • The writeOutput() method should encrypt or decrypt the sentences (depending on the inputEncrypted variable) and then write them into the output file named in the constructor call. Put each sentence on a separate line. Exceptions should be caught. Print an error message and return false if the method is not successful. Otherwise return true.
  • The getKeys() method returns an array of key values - one for each sentence stored in the array of Sentence objects. It should check for error conditions and return an empty array (new int[0]) if the file has not been read yet or if there are no sentences in memory.

The main Method

Finally, you should write a test program with a main method. This program should do the following:

  1. Create a FileConverter object to read an input file, encrypt it and write it to an output file.
  2. Create another FileConverter object to read the output from the last step, decrypt it, and write it to a different output file.
  3. Print the keys returned from each FileConverter object’s getKeys() method.

If run from the command line, the user should be able to specify the file names to read from and write to, like this:

java FileConverterMain unenc.txt enc.txt unenc2.txt

If command line arguments are not supplied, use the following default values:

input.txt, output.txt, output2.txt

Here’s an example input file:

The quick brown fox
jumps over the lazy dogs. Now is the
time for all good men to come to the
aid of their country. All your base are
belong to us.

If done correctly, the first output file should look like this

Nby kocwe vliqh zir dogjm ipyl nby futs xiam.
Wxf rb cqn crvn oxa juu pxxm vnw cx lxvn cx cqn jrm xo cqnra
lxdwcah.
Sdd qgmj tskw sjw twdgfy lg mk.

and the second should look like this

The quick brown fox jumps over the lazy dogs.
Now is the time for all good men to come to the aid of their
country.
All your base are belong to us.

The output from the main method should look like this:

20 35 17
20 35 17

Be sure to test your code on bigger files to shake out any bugs.

Handing In

See the drop box on eLearn for the assignment due date. Hand in your work, including sample input and output files in a single zip file.