Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Imagine you are an outsider (perhaps an adversary) who would like to know the content of the encrypted messages. You know that the messages are

Imagine you are an outsider (perhaps an adversary) who would like to know the content of the encrypted messages. You know that the messages are encrypted using a Caesar cipher, but do not know the key (shift value). Write a Java program to decrypt the text encrypted with a Caesar cipher (such as the quote below).

One of the oldest (and least complicated) ciphers is known as the Caesar cipher (after Julius Cae- sar). To use the Caesar cipher, the two people must agree upon a shift value. Each letter in the

original message will be shifted by this value. So, if they agree upon 7, the letter a would be- come h.

Here is an example of a quote encrypted with a Caesar cipher: Y jxyda secfkjuh isyudsu, ro qdt bqhwu, yi ijybb ijksa yd jxu Cetuhd qwu.

The final program can include an encrypter and a decrypter or just a decrypter if possible.

The decrypter should be able to find out what the shift value is without the user knowing; so it should work regardless of shift value.

Below is the code I have so far, it works, but I have to manually change what the shift value is in the code for it to be able to decrypt the cipher.

import java.util.Scanner;

public class CaesarCipher { 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.println("Enter the String for Encryption: "); String message = new String(); message = sc.next(); System.out.println(encrypt(message, 3)); System.out.println(decrypt(encrypt(message, 3), 3)); sc.close(); } }

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

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions