Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I'm working on implementing an encrypting/decrypting cipher called the Transposition Cipher. Basically, the input has an operation ('E' for encrypt, 'D' for decrypt), a

Hello,

I'm working on implementing an encrypting/decrypting cipher called the Transposition Cipher. Basically, the input has an operation ('E' for encrypt, 'D' for decrypt), a key, and the message to be filled in a 2d array based on the alphabetical ordering of the key. The output is the encrypted/decrypted String. The empty spaces in the grid must be filled by '@' (this is where I'm having the most trouble as I'm not getting the desired output).

It works for the regular cases, but the '@'s aren't filling up the spaces properly when:

  • the key has repeating characters and/or is longer than the plaintext (it's not getting encrypted/decrypted in the right way)

ex input: E COMPUTERSCIENCE HELLO WORLD

expected output: HD@O@@@L@ELRLWO

my output: HLOWORL@@E@D@L@

  • the key is the same length as the plaintext

ex input: D COMPUTERSCIENCE HD@O@@@L@ELRLWO

expected output: HELLOWORLD@@@@@

my output: HEWD@O@@@ROL@LL

I've attached my code below.

Thank you so much!

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class TranspositionCipher { private String line; private String[] arr; private String oper; private String text; private String key; public TranspositionCipher(String line, String[] array, String oper, String text, String key) { this.line = line; this.arr = array; this.oper = oper; this.text = text; this.key = key; } public static String encryptCT(String key, String text) { int[] arrange = arrangeKey(key); int lenkey = arrange.length; int lentext = text.length(); int numRows; if (lenkey > lentext) { numRows = 1; } else { numRows = (int) Math.ceil((double) lentext / lenkey); } char[][] grid = new char[numRows][lenkey]; int z = 0; for (int x = 0; x < numRows; x++) { for (int y = 0; y < lenkey; y++) { if (z < lentext) { grid[x][y] = text.charAt(z); z++; } else { grid[x][y] = FillEmptySpace(); } } } String enc = ""; for (int x = 0; x < lenkey; x++) { for (int y = 0; y < lenkey; y++) { if (x == arrange[y]) { for (int a = 0; a < numRows; a++) { enc = enc + grid[a][y]; } } } } return enc; } public static String decryptCT(String key, String text) { int[] arrange = arrangeKey(key); int lenkey = arrange.length; int lentext = text.length(); int numRows; if (lenkey > lentext) { numRows = 1; } else { numRows = (int) Math.ceil((double) lentext / lenkey); } String regex = "(?<=\\G.{" + numRows + "})"; String[] get = text.split(regex); char[][] grid = new char[numRows][lenkey]; for (int x = 0; x < lenkey; x++) { for (int y = 0; y < lenkey; y++) { if (arrange[x] == y) { for (int z = 0; z < numRows; z++) { grid[z][y] = get[arrange[y]].charAt(z); } } } } String dec = ""; for (int x = 0; x < numRows; x++) { for (int y = 0; y < lenkey; y++) { dec = dec + grid[x][y]; } } return dec; } public static char FillEmptySpace() { return '@'; } public static int[] arrangeKey(String key) { String[] keys = key.split(""); Arrays.sort(keys); int[] num = new int[key.length()]; int count = 0; for (int i = 0; i < key.length(); i++) { for (int j = i + 1; j < key.length(); j++) { if (key.substring(i, i + 1).equals(key.substring(j, j + 1))) { count++; } } } if (count > 0) { ArrayList numList = new ArrayList<>(); for (int k = 0; k < key.length(); k++) { char c = key.charAt(k); for (int l = 0; l < key.length(); l++) { if (!numList.contains(l)) { if (key.charAt(l) == c) { numList.add(l); } } } } for (Integer i : numList) { num[i] = numList.get(i); } } else { for (int x = 0; x < keys.length; x++) { for (int y = 0; y < key.length(); y++) { if (keys[x].equals(key.charAt(y) + "")) { num[y] = x; break; } } } } return num; } public static void main(String[] args) throws FileNotFoundException { Scanner fileIn = new Scanner(new File("input.txt")); while (fileIn.hasNextLine()) { String line = fileIn.nextLine(); String[] arr = line.split(" "); String oper = arr[0]; String text = ""; for (int i = 2; i < arr.length; i++) { text += arr[i]; } String key = arr[1]; text = text.replaceAll("'", ""); TranspositionCipher crypt = new TranspositionCipher(line, arr, oper, text, key); if (crypt.oper.equals("E")) { System.out.println(encryptCT(key, text).toUpperCase()); } if (crypt.oper.equals("D")) { System.out.println(decryptCT(key, text).toUpperCase()); } } } }

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Here is the original problem, but it only discusses the regular cases (which already work for me). My main problem is incorporating the '@'s into the empty spaces in the 2d grid so that they show up in the output String.

Part 3 As you can see creating and breaking ciphers is intriguing, so much so that we have ask you to encode and decode various messages. For this cryptology endeavor we will use Simple Transposition Cipher. Please refer back to the previous handout if need be. Input: All data will be read from a file. The first item to be read is a letter representing E for encrypt or D for decrypt, followed by a String representing the keyword, then another String that is to be either encrypted or decrypted. There will be an undisclosed number of items to be either encrypted or decrypted. Output: The encrypted or decrypted message Test data E MATH MY MOTHER IS PRETTY D BREAD KUTOOTYDETENHNRESRROHIEFD E FOLKS DEAD PEOPLE CAN'T TALK TO FOLKS D CRY CLOAUOLSN Test Output YHSTORRYMTIEMEPT THEKEYISUNDERTHEFRONTDOOR DECAFDLTTKAPNKLEOALOPETOS CALLUSOON

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2022 Workshops 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 In Computer And Information Science 33

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Alfred Taudes ,Atif Mashkoor ,Johannes Sametinger ,Jorge Martinez-Gil ,Florian Sobieczky ,Lukas Fischer ,Rudolf Ramler ,Maqbool Khan ,Gerald Czech

1st Edition

3031143426, 978-3031143427

More Books

Students also viewed these Databases questions

Question

what is pca used for?

Answered: 1 week ago