I have provided the code below: I just need help making it work. Can somone help
ID: 3768015 • Letter: I
Question
I have provided the code below: I just need help making it work. Can somone help me make it work?
import java.util.Scanner;
import java.io.FileReader;
import java.io.PrintStream;
/* Test class for encoding text using Ciphers */
public class EncryptWord {
/**
* Encodes text using a Cipher object.
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char response;
do {
System.out.println("Do you want to encode or decode? (e/d) ");
response = input.next().toLowerCase().charAt(0);
} while ((response != 'e') && (response != 'd'));
System.out.println("Please enter one line of text");
String text = input.next();
CaesarCipher coder = new CaesarCipher();
for (int i=0; i<text.length(); i++) {
char ch = text.charAt(i);
if (response == 'e') {
System.out.print(coder.encode(ch));
} else {
System.out.print(coder.decode(ch));
}
}
input.close();
}
}
Explanation / Answer
Thid below java class contains both the Driver methods and encrypt and decrypt methods
to perfrom the Caesar Cipher Encrypt and Decrypt algorthim implemenation..See the below code to understand the
what is the implemenation..I have written well comments for to understand the code is easy mannaer.
import java.util.Scanner;
/* Test class for encoding text using Ciphers */
// Defining a class called EncryptWord for Caesar Cipher Encryption and Decryption
public class EncryptWord
{
// Holding the String Pattern for Caesar Cipher Encryption and Decryption
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
// Definition of Encryption method for the given input string
public static String encrypt(String plainText, int shiftKey)
{
plainText = plainText.toLowerCase();
String cipherText = "";
for (int i = 0; i < plainText.length(); i++)
{
int charPosition = ALPHABET.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = ALPHABET.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText;
}
// Definition of Decryption method for the given input string
public static String decrypt(String cipherText, int shiftKey)
{
cipherText = cipherText.toLowerCase();
String plainText = "";
for (int i = 0; i < cipherText.length(); i++)
{
int charPosition = ALPHABET.indexOf(cipherText.charAt(i));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0)
{
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
plainText += replaceVal;
}
return plainText;
}
// Main Method
public static void main(String[] args)
{
// Define Scanner Class to read data from console
Scanner sc = new Scanner(System.in);
// Reading A string
System.out.println("Enter the String for Encryption: ");
String message = new String();
message = sc.next();
System.out.println("Encrypted Cipher Text for the given String :");
// Sending the string to encryption
System.out.println(encrypt(message, 3));
// Sending the string to decryption
System.out.println("Decrypted Cipher Text for the given String :");
System.out.println(decrypt(encrypt(message, 3), 3));
sc.close();
}
}
Output: