Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Caesar Cipher The following program was written by a beginner programmer to prov

ID: 675008 • Letter: C

Question

Caesar Cipher The following program was written by a beginner programmer to provide Caesar Cipher encryption and decryption tool (the actual methods that perform these tasks are not provided). The programmer noticed that the user is never given a chance to enter all the information that they need to enter. Figure out what is wrong, explain why it is wrong and propose a fix. public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.printIn("What would you like to do? (E)ncrypt or (D)ecrypt?"); String tool = input.next(); tool = tool.toUpperCase(); if (tool.equals("E") ||tool.equals("D")) { System.out.println("Enter your encryption key: "); int shiftKey = input.nextInt(); System.out.println("Enter your message: "); String userMessage = input.nextLine(); if (tool.equals("E")) System.out.println(encrypt(userMessage,shiftKey)); if (tool.equals("D")) System.out.println(decrypt(userMessage,shiftKey)); } else { System.out.println("ERROR: Invalid choice!"); } input.close();

Explanation / Answer

Import the package and class into our program. using import statement.

ortherwise follow the below process:

We need to implement caesar cipher encryption and decryption methods in separeate class or within the same class.

Method implemented outside the class:

class CaesarCipher {

     private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

     public 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 = this.ALPHABET.charAt(keyVal);

                cipherText += replaceVal;

           }

           return cipherText;

     }

     public String decrypt(String cipherText, int shiftKey)

     {

           cipherText = cipherText.toLowerCase();

           String plainText="";

           for(int i=0;i<cipherText.length();i++)

           {

                int charPosition = this.ALPHABET.indexOf(cipherText.charAt(i));

                int keyVal = (charPosition-shiftKey)%26;

                if(keyVal<0)

                {

                     keyVal = this.ALPHABET.length() + keyVal;

                }

                char replaceVal = this.ALPHABET.charAt(keyVal);

                plainText += replaceVal;

           }

           return plainText;

     }

}

class CaesarDemo {

     public static void main(String args[])

     {

           String plainText = "studentitzone";

           int shiftKey=4;

          

           CaesarCipher cc = new CaesarCipher();

          

           String cipherText = cc.encrypt(plainText,shiftKey);

           System.out.println("Your Plain Text :" + plainText);

           System.out.println("Your Cipher Text :" + cipherText);

          

           String cPlainText = cc.decrypt(cipherText,shiftKey);

           System.out.println("Your Plain Text :" + cPlainText);

     }

}

Inside the class:

public class CaesarCipher

{
public static void main(String[] args)
{
//gets a string to encrypt
String str = (JOptionPane.showInputDialog("Input Data to encypt:"));
//gets a key
String key = (JOptionPane.showInputDialog("Input the key:"));
int keyLength=str.length(key);
//prints encryption
String encrypted = encrypt(str, keyLength);
System.out.println("Encrypted:" + encrypted);
//prints decryption
String decrypted = decrypt(encrypted, keyLength);
System.out.println("Decrypted:" + decrypted);
}
  
public static String encrypt(String str, int keyLength)
{
String encrypted = "";
for(int i = 0; i < str.length(); i++)
{
int c = str.charAt(i);
if (Character.isUpperCase(c))
{
//26 letters of the alphabet so mod by 26
c = c + (key % 26);
if (c > 'Z')
c = c - 26;
}
else if (Character.isLowerCase(c))
{
c = c + (key % 26);
if (c > 'z')
c = c - 26;
}
encrypted += (char) c;
}
return encrypted;
}

public static String decrypt(String str, int key)
{
String decrypted = "";
for(int i = 0; i < str.length(); i++)
{
int c = str.charAt(i);
if (Character.isUpperCase(c))
{
c = c - (key % 26);
if (c < 'A')
c = c + 26;
` }
else if (Character.isLowerCase(c))
{
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}
decrypted += (char) c;
}
return decrypted;
}
}