Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have below question and I have my code as well. My program asks for all the command line user inputs. How can I fix

I have below question and I have my code as well. My program asks for all the command line user inputs. How can I fix my code to have a user input keyword, take an input plaintext file and output a ciphertext as file?

image text in transcribed

My Java program which asks command line inputs:

import java.util.*;

public class SubstitutionCipher { private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter a keyword: "); String keyword = scan.nextLine(); //scan.close(); String key = generateKey(keyword); System.out.println("Key: " + key); System.out.print("Enter a message to encrypt: "); scan = new Scanner(System.in); String message = scan.nextLine(); //scan.close(); String encryptedMessage = encrypt(message, key); System.out.println("Encrypted message: " + encryptedMessage); String decryptedMessage = decrypt(encryptedMessage, key); System.out.println("Decrypted message: " + decryptedMessage); } private static String generateKey(String keyword) { StringBuilder sb = new StringBuilder(); Set used = new HashSet(); for (char c : keyword.toCharArray()) { if (!used.contains(c)) { sb.append(c); used.add(c); } } for (char c = 'Z'; c >= 'A'; c--) { if (!used.contains(c)) { sb.append(c); } } return sb.toString(); } private static String encrypt(String message, String key) { message = message.toUpperCase(); StringBuilder sb = new StringBuilder(); for (char c : message.toCharArray()) { int index = ALPHABET.indexOf(c); if (index != -1) { sb.append(key.charAt(index)); } else { sb.append(c); } } return sb.toString(); } private static String decrypt(String message, String key) { message = message.toUpperCase(); StringBuilder sb = new StringBuilder(); for (char c : message.toCharArray()) { int index = key.indexOf(c); if (index != -1) { sb.append(ALPHABET.charAt(index)); } else { sb.append(c); } } return sb.toString(); } }

2 Task Two: The Substitution cipher (2 marks) A substitution cipher by a keyword works as follows. If a keyword is "STRAWBERRY", we remove the repeating characters in it to get "STRAWBEY". Then, we append the rest of the alphabet characters in reverse order (from ' Z ' to ' A ') to the keyword to construct a key i.e., "STRAWBEYZXVUQPONMLKJIHGFDC" to form a complete substitution key. Finally, we encrypt amessage by substituting its characters with the characters in the key as follows: Your task is to implement the substitution cipher. In particular, your program should - be a command-line program; - take a keyword, which is typed through command-line interface, as input; 2 - take a plaintext (message) file as input; - output a ciphertext as a file; - take a ciphertext file as input; - output a plaintext as a file; - handle any possible errors; - Write in C/C++ or Java (or else, you need to consult Mr Sionggo); - be submitted with a clear readme.txt file. NB: Ignore case. That is, you can choose whether you will input and output uppercase or lowercase characters. Also, keep (do not encrypt) special character such as full stops and commas

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

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions