Answered step by step
Verified Expert Solution
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 az
public static final String ALPHABET "abcdefghijklmnopqrstuvwxyz";
create encryptData method for encrypting user input string with given shift key
public static String encryptDataString 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 ; i inputStr.length; i
get position of each character of inputStr in ALPHABET
int pos ALPHABET.indexOfinputStrcharAti;
get encrypted char for each char of inputStr
int encryptPos shiftKey pos;
char encryptChar ALPHABET.charAtencryptPos;
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 decryptDataString 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 ; i inputStr.length; i
get position of each character of inputStr in ALPHABET
int pos ALPHABET.indexOfinputStrcharAti;
get decrypted char for each char of inputStr
int decryptPos pos shiftKey;
if decryptPos is negative
if decryptPos
decryptPos ALPHABET.length decryptPos;
char decryptChar ALPHABET.charAtdecryptPos;
add decrypted char to decrypted string
decryptStr decryptChar;
return decrypted string
return decryptStr;
main method start
public static void mainString args
create an instance of Scanner class
Scanner sc new ScannerSystemin;
take input from the user
System.out.printlnEnter a string for encryption using Caesar Cipher: ;
String inputStr scnextLine;
System.out.printlnEnter the value by which each character in the plaintext message gets shifted: ;
int shiftKey Integer.valueOfscnextLine;
System.out.printlnEncrypted Data encryptDatainputStr shiftKey;
System.out.printlnDecrypted Data decryptDataencryptDatainputStr shiftKey shiftKey;
close Scanner class object
scclose;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started