Question: Write this program in Java: In cryptography, plaintext is the original message and ciphertext is the encrypted message. A cipher is an algorithm that converts
Write this program in Java:





In cryptography, plaintext is the original message and ciphertext is the encrypted message. A cipher is an algorithm that converts plaintext into ciphertext. The Playfair cipher is an encryption method that uses a matrix to convert pairs of plaintext characters into pairs of ciphertext characters. This technique involves a key that is used to construct a 5x5 matrix which is then used for encryption/decryption. The key is a sequence of alphabetical characters (for example "sequoiatree"). We construct our 5x5 matrix by first filling in the unique letters from the key: S, E, Q, U, O, I, A, T, R. (Notice that we do not fill in repeated characters from the tree.) We then fill in the matrix with the remaining characters from the alphabet, without repeating any characters that appeared in the key. To make room in a 5x5 matrix, I and J are treated as the same character. Our matrix with the tree "sequoiatree" is: C D F GH K L MINIP Encryption Suppose we want to encrypt the string: "HELLO WORLD". The first thing we do is divide the message up into groups of two: HE LL OW OR LD. If an pair has duplicate characters (like LL), we add an "X" in between them and redistribute the pairs: HE LX LO WO RL D. If we are left with a single character at the end with no pair (D, in this case), we add an X at the end. Our final sequence is: HE LX LO WO RL DX We will encrypt each pair separately. First, we find the location of each letter in the pair in the 5x5 matrix. There are three possibilities 1) The two letters are in the same row. If so, your ciphertext for that pair will be the letter directly right of the first letter and the letter directly right of the second letter. (If there is no letter to the right, wrap around to get the letter at the beginning of that row.) For example, if you were encrypting the pair: "EO'. you would first notice that they are both in the same row. You would find the letter directly right of "E (which is "Q) and the letter directly right of "O" (there isn't one, so you'd wrap back to the beginning of that row and choose "S"). So the pair "EO" would be encrypted as "QS. 2) The two letters are in the same column. If so, your ciphertext for that pair will be the letter directly below the first letter and the letter directly below the second letter. (If there is no letter below, wrap around to get the letter at the top of that column.) For example, if you were encrypting the pair: "TX", you would first notice that they are both in the same column. The letter directly below "T" is "F", and there is no letter directly below "X" -- so we would wrap back to to the top of that column to get "Q". So the pair "TX" would be encrypted as "FQ 3) The two letters are not in the same row or column. We first find the row and column of the first character in the pair, and call those row1 and col1. We then find the row and column of the second character in the pair, and call those row2 and col2. The first encrypted letter in the pair will be at (row1,col2) and the second encrypted letter from the pair will be at (row2,col1). For example, when we encrypt the pair "HE", we notice that those letters are not in the same row or column. "H" is at (2,4), so row1-2 and col1-4. "E" is at (0,1), so row2-0 and col2-1. Our first encrypted character is at position (row1.col2) = (2,1), which is a "D". Our second encrypted character is at position (row2.col1) = (04) which is an "O". So the pair "HE" is encrypted as "DO
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
