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

Java Application According to Wikipedia, a Caesar cipher, also known as Caesar\'

ID: 3763588 • Letter: J

Question

Java Application

According to Wikipedia, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, d would be replaced by a, e would become b, and so on. The method is named after Julius Caesar, who used it in his private correspondence. Problem. Write an application, goCaesar, that will take in a message and a key and output the encrypted message. Your code should be able to take in an entire text file or from the keyboard. Your code should also be able to decrypt the following: "XFRQQEDVNHWEDOOURFNV" using a Key of: 3. Additional Requirement: Use at least one Exception Handling Mechanism.

Explanation / Answer

import java.util.Scanner;

public class GoCaesarC {
   public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

   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;
   }

   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;
   }

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the String for Encryption: ");
       String message = new String();
       message = sc.next();

       System.out.println("After Encryption :" + encrypt(message, 3));
       System.out.println("After Decryption :"
               + decrypt(encrypt(message, 3), 3));
       sc.close();
   }
}

OUTPUT:

Enter the String for Encryption: haithisissrinivas
After Encryption :kdlwklvlvvulqlydv
After Decryption :haithisissrinivas

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote