java代写: INFO1103 Task 3: Cryptography

Task 3: Cryptography

Submission deadline: 5pm on Saturday, 15th October (Week 11)
Submission: Submit a zip file containing your Encrypter.java, SecretMessages.java and FrequencyDecryption.java files via the online system PASTA.
Weight: 7 marks = 7% of your final mark.
This assessment must be completed individually.

Introduction

You, being a studious student, open this task description, ready for whatever challenge you may need to face. However, you are suddenly faced with a big problem! Your instructors have literally encrypted a large section of the task description!

What!

Uq hyygd hugk wyr’sk ckbuzakjkc qaud ikddmok. Byxojmq – rhmquyxd!
Lk jkmhhw ayzk wyr’sk kxfywkc cyuxo qaud mdduoxikxq.
(Lk bkjqmuxhw kxfywkc ljuquxo uq!)
Lk qayroaq uq lyrhc tk xubk qy amsk dyik zjuekd nyj qak nujdq qajkk zkyzhk qy nuxuda qak mdduoxikxq.

Cy wyr qauxg wyr byrhc tk yxk yn qaydk zkyzhk?

Qy tk khuouthk, wyr irdq amsk zmddkc mhh yn qak xyx-aucckx qkdqd yx ZMDQM.
Un wyr amsk zmddkc qakdk qkdqd, qakx prubghw bakbg zumeem. Amd mxwyxk imck m zydq qamq dmwd…

‘Au kskjwyxk. Qaud iuoaq tk m tuq jmxcyi, trq prujgw pryggmd mjk p- rjuyrdhw p-rqk!!!’

Un xyq, wyr iuoaq tk qak nujdq qy nuxuda!

Qy bhmui wyrj zjuek, imgk drjk wyr’sk zmddkc mhh qak ZMDQM qkdqd.
Un wyr amsk, qakx imgk m zydq qamq dmwd kvmbqhw qak dmik qauxo md
mtysk.
Qamq ud, imgk m zydq qamq dmwd:
‘Au kskjwyxk. Qaud iuoaq tk m tuq jmxcyi, trq prujgw pryggmd mjk p-rjuyrdhw p-rqk!!!’
XYQK: Nkkh njkk qy zydq mxyxwiyrdhw, trq imgk drjk qaud zydq ud zrthub, dy yqakjd gxyl wyr’sk nuxudakc.

Qak dkbjkq ikddmok nyj dkbyxc zhmbk ud: ‘Pryggmd pryqk pryggmd pruqk prubghw.’.
Zhkmdk zydq qaud ux nyhhyl-rz cudbrdduyx dkbquyx yn qak nujdq zydq un wyr mjk dkbyxc! 🙂
Nuxmhhw, qak dkbjkq ikddmok nyj qaujc zhmbk ud: ‘Zkcmxqub zjyojmiikjd zkjnyji zjmbqumhhw zkjnkbqhw… zkjamzd’.

Momux, zhkmdk zydq qaud ux nyhhyl-rz cudbrdduyx dkbquyx yn qak nujdq zydq un wyr mjk qaujc! 🙂
Xyq nujdq, dkbyxc yj qaujc, trq dquhh lmxq qy fyux ux yx qak nrx? Imgk rz wyrj ylx qmrqyojmi mxc zydq uq ux qak
nyhhyl-rz cudbrdduyx dkbquyx.

INFO1103 Introduction to Programming, Semester 2, 2016

1

Nuxmhhw, qakjk mjk frdq m nkl qauxod lk lmxqkc qy dmw.
Dyikquikd, wyr iuoaq nuxc qamq qak yjckjkc hudq yn iydq byiiyx hkqqkjd ux yxk qkvq
ud dhuoaqhw cunnkjkxq njyi qak yqakj.
Qamq’d ygmw qayroa! Wyr bmx dquhh rdk qaud ikqayc, trq luhh mhdy xkkc qy rdk dyik frcokikxq qy nuv mxw kjjyjd.

Mhh juoaq, qamq’d kskjwqauxo!
Lk ayzk wyr amc nrx, mxc luda wyr oyyc hrbg nyj qak jkdq yn qak byrjdk! 🙂

Nooooo!!!

How can this be? How can you do your assignment when half of it is gibberish!? You didn’t ask for a task descryption! As you are thinking this, something suddenly occurs to you. You are a Java programmer now! You have all the skills you need to crack this silly cipher, and you’re going to do it!

Before You Begin

Remember that this task is here to help you improve your programming skills. When doing this task, you might find some parts tricky, but this is normal and also a good thing (if we were never challenged, we would never learn anything!). The most important things are to start this task as soon as you can, to ask questions when you need help and to be persistent! It’s often good to work on one part of the assignment at a time, then submit to PASTA to see how you went with it. Try to be creative when approaching problems – usually there are many ways to do something. We hope you enjoy this cryptography-themed task! 🙂

Part 1 – Your Own Secret Messages

In the first part of this assignment, your task is to write a program to encrypt and decrypt your very own secret messages using a substitution cipher! This substitution cipher will involve swapping each letter of the alphabet with a different letter and sometimes also removing spaces. For example, maybe “a” might be replaced with “e”, “b” might be replaced with “a”, “c” might be replaced with “f” and so on.

In order to encrypt and decrypt these kinds of messages, the sender and receiver must both know a secret key. A key is a set of rules that tell you which letters to swap with which. One way that you can represent this key is with a string that contains all of the letters from a to z exactly once. The first letter in the string should be the letter that replaces “a” when encrypting, the second letter in the string should be the letter that replaces “b”, the third letter should be the one that replaces “c” etc.

For example, if the string is “bzcdefghijklmnopqrstuvwxya”, then “a” would be replaced with “b”, “b” would be replaced with “z”, “z” would be replaced with “a” and everything else would stay the same. So, a word like “zebra” would become “aezrb”.

When decrypting, the reverse would be done: “b” would be replaced with “a”, “z” would be replaced with “b”, “a” would be replaced with “z” and everything else would stay the same. So, “aezrb” would become “zebra”.

All right, with that in mind, are you ready to make your own secret messages?

INFO1103 Introduction to Programming, Semester 2, 2016

2

Step 1 ‐ Getting the skeleton code

In this task description, there is a section called Setting Up. Please read this section carefully for information about the skeleton code and follow the instructions to set up your workspace.

Step 2 – Understanding the Encrypter class

For this part of the assignment (i.e. Part 1), we will be focusing on only two of the three skeleton code classes: Encrypter and SecretMessages. You could think of the Encrypter class as a set of instructions for making encrypters. That is, things that can encrypt and decrypt secret messages. Each encrypter has its own special key for encrypting and decrypting messages, and also a setting for removing whitespace. (Recall that in programming, all characters that are not visible when displayed are called collectively whitespace. Thus, the whitespace includes the normal space “ ” but also the tab “\t”, new line “\n” and some other characters.)

This option to remove whitespace is useful, because messages without spaces are harder to guess. For example, it might be easy to guess that something like “T od Dto” by looking at word length, but “TodDto” would be harder!

Sometimes you might not want to remove whitespace, though, because then it’s more difficult to read the message after decrypting it. For example, “IamMia” is more difficult to read than “I am Mia”. Perhaps when testing your code, it would be nicer if the decryption was very easy to read. So, in some cases, not removing whitespace could be helpful.

This is why each encrypter has a removeSpaces setting. If removeSpaces is set to true, then the encrypter will remove all whitespace when encrypting. If it is set to false, then the encrypter will not remove whitespace.

Note that the encrypter should never remove whitespace when decrypting, as this just makes the message unnecessarily difficult to read.

Step 3 – Writing the Encrypter Class

You are welcome to complete this task in any order you like, but if you’re not sure where to begin, try starting with the getKey and isRemovingSpaces methods in the Encrypter class.

The blue comments above each method tell you exactly what the method should do, and give you information about input and output. Please read this carefully!

After you have written a method, it’s a good idea to check that it works correctly. You can do this by submitting your code to PASTA. Even if you have only written one method, you can still submit your code. Remember that you are allowed as many attempts as you like, so there really is no harm in doing this. To submit your code, first make sure your code compiles (i.e. no red errors in Eclipse), then follow the submission instructions at the end of this task description.

If anything seems unfamiliar, try looking at the lecture slides, tutorial questions and textbook sections on object oriented design. Also, here are a few hints that you might find useful when completing these methods!

INFO1103 Introduction to Programming, Semester 2, 2016

3

INFO1103 Introduction to Programming, Semester 2, 2016

Hint 1

If you have a character and want to check if it is a letter or whitespace, you can use the Character methods isWhitespace and isLetter.

Here’s an example of how you could check if the character ‘T’ is a letter:

if (Character.isLetter(‘T’)) System.out.println(“This is a letter”);

Hint 2

You can easily check if a letter character is lowercase or uppercase using the following Character methods: isUpperCase, isLowerCase.

Here is an example of how you could check if the character ‘B’ is uppercase:

if (Character.isUpperCase(‘B’))
System.out.println(“This character is an uppercase letter!”);

Hint 3

Want to get the lowercase equivalent of an uppercase letter, or the uppercase equivalent of a lowercase letter? Well then, these Character methods are just what you need: toUpperCase, toLowerCase

Here’s an example of how you could get ‘B’ in lowercase:

char lowercaseB = Character.toLowerCase(‘B’); //lowercaseB will be ‘b’

Hint 4

Say you have a character which is a lowercase letter, and you want to know its position in the alphabet. For example, ‘a’ is in position 0, ‘b’ is in position 1, ‘c’ is in position 2 etc.

Here’s a simple way you can do it (note that charName is the name of the character you’re interested in).

int position = charName – ‘a’;

How does this work? Well, charName and ‘a’ are converted to certain integers. ‘a’ is converted to 97, ‘b’ is converted to 98, ‘c’ to 99 etc. (see this table for reference https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html ). Then, these integers are subtracted.

So, something like ‘c’ – ‘a’ becomes 99 – 97, which is 2. Ah! And 2 is the position of ‘c’ in the alphabet. :D.

To find the position of an uppercase letter, just convert it to lowercase first and follow the same process!

Hint 5

What about going backwards? What if you know the position of a letter in the alphabet and you want to know what character it is? Well, we can do something similar to before!

Here’s how we could find the character in position 4 of the alphabet:

int character = (char) (4 + ‘a’); //character will be e

Why is this e? Well, ‘a’ is converted to 97, so we have 4+97, which is 101. Then, we convert 101 to a character and get ‘e’.

4

Step 4 – SecretMessages Class

Now that we’ve made our Encrypter class, it’s time to use it!
Write a program in the class SecretMessages that creates an encrypter and allows the user to do various

things with this encrypter.

This encrypter should initially:

  •  use the key “zabcdefghijklmnopqrstuvwxy” to encrypt and decrypt messages
  •  be set to not remove spaces

    The program should give the user 5 different choices of things to do with the encrypter:

  1. encrypt a message
  2. decrypt a message
  3. check the key
  4. change the key
  5. change the space-removal mode.

After the user has made a choice, the program should use the encrypter to perform their choice. Then, it should allow them to make another choice and it should perform that action. It should keep allowing the user to make choices until they decide to quit. See the next section for some examples.

Example Input/Output

The following are some examples of input/output. Note that the black text is output, and the green text is input.

Example 1:

What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key
4 ‐ change the key
5 ‐ change space mode
other ‐ quit
1
Enter a message to encrypt
Quokkas are amazing! :D
The encrypted message is: Ptnjjzr zqd
zlzyhmf! :C
What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key
4 ‐ change the key
5 ‐ change space mode
other ‐ quit
2
Enter a message to decrypt
Ptnjjzr zqd zlzyhmf! :C
The decrypted message is: Quokkas are
amazing! :D
What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key
4 ‐ change the key
5 ‐ change space mode
other ‐ quit
q
Quitting. Goodbye.

INFO1103 Introduction to Programming, Semester 2, 2016

5

Example 2:

Example 3:

What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key
4 ‐ change the key
5 ‐ change space mode
other ‐ quit
This is the key: zabcdefghijklmnopqrstuvwxy
What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key
4 ‐ change the key
5 ‐ change space mode
other ‐ quit
4
Enter a new key
quokka
The new key was invalid so the key couldn't
be changed
What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key
What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key

4 ‐ change the key
5 ‐ change space mode
other ‐ quit 13

Enter a message to encrypt
Do you like quokkas?
The encrypted message is: Cn xnt khjd
ptnjjzr?
What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key
4 ‐ change the key
5 ‐ change space mode
other ‐ quit
5
Changing mode
What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key

4 ‐ change the key
5 ‐ change space mode
other ‐ quit 14

Enter a message to encrypt
Do you like quokkas?
The encrypted message is: Cnxntkhjdptnjjzr?
What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key
4 ‐ change the key
5 ‐ change space mode
other ‐ quit
Enter a new key
abcedfghijrstuvwxyzklmnopq
The key was successfully changed
What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key

4 ‐ change the key
5 ‐ change space mode
other ‐ quit q3

Quitting. Goodbye.
4 ‐ change the key
5 ‐ change space mode
other ‐ quit
This is the key: abcedfghijrstuvwxyzklmnopq
What would you like to do?
1 ‐ encrypt a message
2 ‐ decrypt a message
3 ‐ check the key
4 ‐ change the key
5 ‐ change space mode
other ‐ quit
q
Quitting. Goodbye.

INFO1103 Introduction to Programming, Semester 2, 2016

6

Part 2 – Descryption Decryption

In the last section, you made your own encrypter to encrypt and decrypt your own secret messages. This worked by swapping letters around. You also saw that you could remove spaces to make those messages more difficult to decrypt. Of course, if you wanted to go a step further, you could also remove capitalisation and punctuation. This would leave just a big block of letters.

If someone handed you a long block of letters like that and you had to decrypt the message without the key, what would you do? It might seem like a difficult task, but we can use a special trick!

Say you read a text in plain English, and found that the most common letter was “e”. Then, you read an encrypted message and found that the most common letter was “z”. Would you be able to guess what that “z” actually was? Probably! If the most common letter in English was ‘e’, then there would be good evidence to think that that “z” was actually an “e”!

You could do something similar with the rest of the letters! You could get a list of all the letters in English ranked based on how frequently they occur and a list of all letters in the encrypted message ranked in the same way. Then you could try to match up the letters in the lists to guess the key!

Of course, this might not always work out exactly. For example, it might be tricky trying to decrypt a novel with no e’s in it! (https://en.wikipedia.org/wiki/Gadsby_(novel) ). However, it can be useful in giving you a general idea of which letter could be which, and you could try out different combinations! Generally, the longer the encrypted message, the better this method will work.

With all of this in mind, it’s time to finally discover what the encrypted part of the task description says!

The encrypted message is quite short, which means that the distribution of letters not exactly the same as general English. So, to make things a little simpler, we are providing a reference text to use. If you count the number of times each letter appears in the reference text, and sort the letters based on how often they appear, the list will be the same as the encrypted text.

A reference text of standard English

This is some sample text to use as a reference. Have you heard of quokkas? They’re really cute Australian animals that seem like little roos with big puffy cheeks! If you want to see one, the best way is to fly to Perth, then catch a ferry over to Rottness* island. There are apparently about 10,000 on Rottness* island. Wow! Sometimes, if you have superb selfie skills, you can take a selfie with them! They often look like they’re smiling, which makes them very photogenic! Tortoises are also very q-ute. They have shells, you see. Tortoises might eat juicy fish. Did you hear fish is ‘poisson’ in French? Tortoises are better than fish. Juice is tastier than wine. Is juice a good prize? Nnnnnooooooooooooo! This sentence is still strange. quip, quid, quiz, quote, quit. Good dogs/ pups meet bugs under beds. Ponds. Pepper. Mind mood.

*Actually spelt “Rottnest”, but we needed less t’s….
Your task now is to finish the class FrequencyDecryption. We have written many of the methods for you

– you only need to write getKey and printTaskDescription. What could the secret message say?

INFO1103 Introduction to Programming, Semester 2, 2016

7

Example Output

The secret message is:
Well, we can’t tell you what the secret message is in the example output, but
it should be in this form at least. Blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah etc.

Setting Up

Setting up your workspace

In Eclipse, create a project “info1103_task3”. In this project, create a package “cryptography”. Then, create three classes within this package: Encrypter, SecretMessages and FrequencyDecryption.

Downloading the skeleton code

Download the skeleton code from PASTA for these three classes. Then, you can either import these classes, or copy and paste the skeleton code into each of the classes you made.

Methods to complete

Take a quick look at the skeleton code. You’ll notice that, in some methods, there is one of the following comments:

//TODO complete this method

//TODO complete this constructor

These are the methods that you will need to write for this assignment. You don’t need to modify any of the methods that don’t have one of these comments – we have already done these for you!

NOTE: we recommend that you read the rest of the task description before you start writing any of the methods!!

INFO1103 Introduction to Programming, Semester 2, 2016

8

Methods already return values!

You might notice that, in some of the methods you need to complete, we have added a return statement to the skeleton code. For example,

public String getKey()

{
return “”; //TODO delete this when you write this method

}

This has been done to make your code compile. That way, you can submit to PASTA even if you haven’t written some of the methods.

When you start writing a method, delete this return statement and replace it with your own code.

Method headers

Each method has a header, such as:

public boolean setKey(String newKey) Please do not modify these headers at all because we use them to test your code.

Additional methods

You are welcome to add any additional methods to the skeleton code if you would like to.

Method Comments

Finally, you’ll notice there are blue comments above each method, which describe what each method does, what the parameters should be, and what the method will return depending on the input. Before writing any methods, it’s very important to read these comments carefully, so you can make sure your method does exactly what it should!

Submission Instructions

Submitting in PASTA

  1. Before submitting, you should zip your source code files. Open the folder containing the classes Encrypter.java, SecretMessages.java and FrequencyDecryption.java. If you have trouble finding the src folder, first find and open your workspace folder. Then, open the folder “info1103_task3”. The src folder will be inside. Navigate into the src folder, right click on the “cryptography” folder and choose “Send to -> Compressed (Zipped) folder”.
  2. Log in to PASTA (http://info1103.it.usyd.edu.au/) with your Unikey login and password.

    Note: If you are off-campus, you will need to establish a VPN connection to access PASTA. This means installing a program called “VPN client” on your computer and running it every time when you use PASTA off-campus. Instructions how to do this are available here:

INFO1103 Introduction to Programming, Semester 2, 2016

9

http://staff.ask.sydney.edu.au/app/answers/detail/a_id/519/~/how-do-i-download-and-install-the-vpn-client-to- access-the-university-network%3F

If you have problems with VPN, try synchronising your password:

https://www.auth.usyd.edu.au/extro/pwresync/

If this doesn’t work, contact the ICT helpdesk: http://sydney.edu.au/ict/student/contact/

  1. Find Task 3 and press “Submit”. Then press “Choose File” and attach the zipped source files, then

    press “I accept” (after reading the University policy on academic honesty).

  2. If you see a message indicating that your code is queued for testing under Task 3, this means that your code has been uploaded successfully.
  3. If you see a red error box, you need to fix any problems indicated before your submission will be accepted.
  4. Once your program has been tested, the page will tell you to refresh for results, so refresh the page. You should see green, red and grey boxes. Each box corresponds to a test; a red box indicates that your program has failed the respective test, a green box indicates that your program has passed the test, and a grey box indicates that the test is hidden (you will not see the result of the test until after the due date).
  5. If you have red boxes, you can see which tests were not passed by clicking on the task name. Correct the errors (go to Eclipse, re-write your code and test it carefully) and then submit again in PASTA.
  6. For further help on using PASTA, check the INFO1103 User Manual, or post a question on Piazza.

You can submit as many times as you want. Please make sure that your program compiles and runs before you submit it in PASTA. Your last submission will be the one that will be marked.

Marking

5 marks will be allocated for the Encrypter and SecretMessages classes (core part of the task) and 2 marks for the FrequencyDecryption (extension part of the task).

Your mark is the percentage of the tests that you have passed. For example, if you pass half of the tests for the core part of the task and all of the tests for the extension, you will receive 50% of 5 marks + 100% of 2 marks = 4.5 marks total. If you do not attempt the extension part of the test, the maximum mark you can get is 5/7.

INFO1103 Introduction to Programming, Semester 2, 2016

10