Question: The Caesar cipher is an ancient method of encrypting text, i.e. to transform text into a format that is unreadable for anyone without a secret

The Caesar cipher is an ancient method of encrypting text, i.e. to transform text into a format that is unreadable for anyone without a secret key. It is believed that Julius Caesar actually used such a cipher for his correspondence. Unfortunately for him this type of cipher is easily broken using a frequency analysis method which is outlined below. Your assignment is to implement this in Java.

Part 1: Encrypting and Decrypting

The Caesar cipher is a rotation cipher and operates by translating each letter into the one that is shifted along the alphabet by a fixed distance. This distance is called the shift. It is the same for all letters in the alphabet and therefore can be seen as the secret key to encrypt and decrypt: To encrypt your text using a given shift, you translate letters by that many places later in the alphabet. A Caesar cipher with shift The Caesar cipher is an ancient method of encrypting text, i.e. to can be illustrated as follows.

For example, if your text to encrypt is Meet me at midnight under the bridge and your shift is transform text into a format that is unreadable for anyone without a, the encrypted text is Phhw ph dw plgqljkw xqghu wkh eulgjh because the letter M gets translated into a P and e gets translated into h and so on. We wrap around at the end of the alphabet so that z gets changed to a c given a shift of secret key. It is believed that Julius Caesar actually used such a. We can interpret a negative value for the shift as translating letters backwards (e.g. an f gets encrypted as the letter b if the shift is cipher for his correspondence. Unfortunately for him this type of cipher is).

Requirements

In a file called Caesar.java, implement the following (public static) methods.

a method called rotate that rotates a single character. It should have two arguments: the first is an integer (int) shift and the second a char to rotate, and return the character rotated by the given shift, as a char.

Lower-case characters should be translated into lower-case characters, capitalised ones into capitalised ones, and all other characters should remain untouched.

another method called rotate that rotates a whole string. It should again take two arguments: the first is an integer (int) shift and the second a String to rotate. It should return the string rotated by the given shift, as a String. Of course you may use your character rotation method here.

a main method, that allows to encode/decode text.

This should read both the shift as well as the plain text from command line arguments. The first argument is interpreted as the shift and the second one as a (string) message to be rotated. Your method should check if it was called with exactly two arguments and complain otherwise. The onlyoutput printed by the program should be the rotated string output. See below for example outputs. Your program needs to print the exact same output to be considered correct.

$> java Caesar 3 "The ships hung in the sky in much the same way that bricks don't." Wkh vklsv kxqj lq wkh vnb lq pxfk wkh vdph zdb wkdw eulfnv grq'w. $> java Caesar -13 "The ships hung in the sky in much the same way that bricks don't." Gur fuvcf uhat va gur fxl va zhpu gur fnzr jnl gung oevpxf qba'g. $> java Caesar 13 The ships hung in the sky in much the same way that bricks don't. Too many parameters! Usage: java Caesar n "cipher text" $> java Caesar 13 Too few parameters! Usage: java Caesar n "cipher text"

Part 2: Cracking the Caesar cipher

Suppose we are given a cipher text, i.e. text that has already been encrypted with some (unknown) shift, and we want to determine the original unencrypted text (typically referred to as the plaintext). To reconstruct the original text we could decode with each of the 26 possible shifts, and take the result that looks closest to an English sentence.

How do we measure closeness? This is where letter frequencies and a small statistical trick comes in. First of all, we know how often each letter occurs on average in English text. For instance, e is the most common letter, then t, and so on. To decode our cipher text, we can compute the frequencies of each letter as they appear in the text. To measure how close these are to the known English letter frequencies, we use the easily broken using a frequency analysis method which is outlined below. Your-score (that is the Greek letter chi, so its the chi-squared score). This score is defined as

assignment is to implement this in Java. Part 1: Encrypting and Decrypting

where The Caesar cipher is a rotation cipher and operates by translating each denotes the frequency of a letter letter into the one that is shifted along the alphabet by a (the number of occurrences divided by the total number of letters in the text), and fixed distance. This distance is called the shift. It is the same is the corresponding English letter frequency. In other words, we sum the fraction over all for all letters in the alphabet and therefore can be seen as possible letters to determine this score, which is a single number.

The the secret key to encrypt and decrypt: To encrypt your text using score will be lower when the frequencies are closer to English. Note that we are ignoring the case of letters (we treat upper and lower case as equal) when computing the a given shift, you translate letters by that many places later in score.

You are provided with a file called Download provided with a file called Brutus.java Download Brutus.javathat already defines letter frequencies in English texts as an array of doubles, in alphabetical order:

public static final double[] english = { 0.0855, 0.0160, 0.0316, 0.0387, 0.1210, 0.0218, 0.0209, 0.0496, 0.0733, 0.0022, 0.0081, 0.0421, 0.0253, 0.0717, 0.0747, 0.0207, 0.0010, 0.0633, 0.0673, 0.0894, 0.0268, 0.0106, 0.0183, 0.0019, 0.0172, 0.0011 };

Accordingly, the frequency of the letter a is the alphabet. A Caesar cipher with shift can be illustrated as follows. and can be accessed as english[0]. Similarly, For example, if your text to encrypt is Meet me at midnight and so on.

Requirements

In Brutus.java, write (public static) methods as follows.

a method called count that takes a single String parameter and returns a length-26 integer array (int[]) whose values reflect how often each character occurred. You should not make a difference between upper and lower case letters and the returned array should be in alphabetical order. This way, if letterCounts is an array resulting from your method then letterCounts[25] is the number of times the letter z or Z occurs.

a method called frequency that takes a single String and returns a length-26 array of doubles whose values correspond, in alphabetical order, to the frequency of the letter. This way, if letterFreq is an array resulting from this method then letterFreq[24] is the number of times the letter x or X occurs, divided by the size of the string input.[5]

a method called chiSquared, which returns the under the bridge and your shift is , the encrypted text is-score (a double) for two given sets of frequencies. That is, it should take two parameters, both of type double[], and return a single double value that tells us how close these two arrays are. You may assume that the two inputs are always of length 26 and that the second input contains no zeros (we will use the given array for English letter frequencies in tests here).

a main method that can be used to decipher Caesar-encoded English cryptotext without the key. Of course, you should be using your chiSquaredmethod as well as the given English letter frequencies.

The string that is to be deciphered should be read from the first command line argument and your program should ensure that it gets exactly this one argument and complain otherwise. Sample output below.

$> java Brutus "Vg vf n zvfgnxr gb guvax lbh pna fbyir nal znwbe ceboyrzf whfg jvgu cbgngbrf." It is a mistake to think you can solve any major problems just with potatoes. $> java Brutus Too few parameters! Usage: java Brutus "cipher text" $> java Brutus Too Many Parameters Too many parameters! Usage: java Brutus "cipher text"

Hints and Comments

In Java, char and int variables are (more or less) interchangeable. A Java statement like

int diff = 'e' - 'b';

is perfectly legal, i.e. Java can interpret the difference of two letters with no problem, and this will give an integer value. If the two letters are of the same case, then this will give a value between Phhw ph dw plgqljkw xqghu wkh eulgjh because the letter M gets and translated into a P and e gets translated into h and so. In particular, if ch is a lower case (char) letter, then

int diff = ch - 'a';

tells you how many places after a that letter is in the alphabet. (The value of diff will be between on. We wrap around at the end of the alphabet so that and z gets changed to a c given a shift of . We inclusive).

When translating letters, notice that if ch-'a' is between can interpret a negative value for the shift as translating letters backwards and (e.g. an f gets encrypted as the letter b if the shift (inclusive), then ch is a lower case letter, and we encrypt as above. Alternatively, if ch-'A' is between is ). Requirements In a file called Caesar.java, implement the following (public and static) methods. a method called rotate that rotates a single character. It (inclusive), we encrypt ch similarly to get a new upper case letter. You may also use helper methods isLetter, isLowerCase etc from the Character class.

In order to translate whole strings, recall that you can access the individual characters in a string using the charAt method: if str is a String then str.charAt(i) gives you the char at index i in str.

To interpret a string as an integer you can use Integer.parseIntLinks to an external site..

When counting letter frequencies remember that for this exercise, we consider upper and lower case to be the same and do not consider spaces and punctuation characters. For example, in the string "Mississippi moon!", the frequency of the letter m is should have two arguments: the first is an integer (int) shift and, while the frequency of the letter s is the second a char to rotate, and return the character rotated by.

Make sure you that your code is readable and appropriately documented. There will be points for javadoc comments that explain what each method does and how its parameters are interpreted.

Make sure that all your .java files are in one directory, and are not structured into packages (as some IDEs recommend). The command java *.java must not error.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!