Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

explain the code: / / import required classes and package, if any import java.util.Scanner; / / create class CaesarCipherExample for encryption and decryption public class

explain the code: // import required classes and package, if any
import java.util.Scanner;
// create class CaesarCipherExample for encryption and decryption
public class CaesarCipherExample
{
// ALPHABET string denotes alphabet from a-z
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
// create encryptData() method for encrypting user input string with given shift key
public static String encryptData(String inputStr, int shiftKey)
{
// convert inputStr into lower case
inputStr = inputStr.toLowerCase();
// encryptStr to store encrypted data
String encryptStr ="";
// use for loop for traversing each character of the input string
for (int i =0; i < inputStr.length(); i++)
{
// get position of each character of inputStr in ALPHABET
int pos = ALPHABET.indexOf(inputStr.charAt(i));
// get encrypted char for each char of inputStr
int encryptPos =(shiftKey + pos)%26;
char encryptChar = ALPHABET.charAt(encryptPos);
// add encrypted char to encrypted string
encryptStr += encryptChar;
}
// return encrypted string
return encryptStr;
}
// create decryptData() method for decrypting user input string with given shift key
public static String decryptData(String inputStr, int shiftKey)
{
// convert inputStr into lower case
inputStr = inputStr.toLowerCase();
// decryptStr to store decrypted data
String decryptStr ="";
// use for loop for traversing each character of the input string
for (int i =0; i < inputStr.length(); i++)
{
// get position of each character of inputStr in ALPHABET
int pos = ALPHABET.indexOf(inputStr.charAt(i));
// get decrypted char for each char of inputStr
int decryptPos =(pos - shiftKey)%26;
// if decryptPos is negative
if (decryptPos <0){
decryptPos = ALPHABET.length()+ decryptPos;
}
char decryptChar = ALPHABET.charAt(decryptPos);
// add decrypted char to decrypted string
decryptStr += decryptChar;
}
// return decrypted string
return decryptStr;
}
// main() method start
public static void main(String[] args)
{
// create an instance of Scanner class
Scanner sc = new Scanner(System.in);
// take input from the user
System.out.println("Enter a string for encryption using Caesar Cipher: ");
String inputStr = sc.nextLine();
System.out.println("Enter the value by which each character in the plaintext message gets shifted: ");
int shiftKey = Integer.valueOf(sc.nextLine());
System.out.println("Encrypted Data ===>"+encryptData(inputStr, shiftKey));
System.out.println("Decrypted Data ===>"+decryptData(encryptData(inputStr, shiftKey), shiftKey));
// close Scanner class object
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

Oracle PL/SQL Programming Database Management Systems

Authors: Steven Feuerstein

1st Edition

978-1565921429

More Books

Students also viewed these Databases questions

Question

1. Explain why evaluation is important.

Answered: 1 week ago