Question: explain the code part by part import java.util.Scanner; public class monoalphabetic { static final String ALPHABET = abcdefghijklmnopqrstuvwxyz; static final String KEY = qwertyuiopasdfghjklzxcvbnm; static
explain the code part by part
import java.util.Scanner;
public class monoalphabetic { static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; static final String KEY = "qwertyuiopasdfghjklzxcvbnm";
static String encrypt(String plainText) { plainText = plainText.toLowerCase(); String cipherText = ""; for (int i = 0; i < plainText.length(); i++) { int charPosition = ALPHABET.indexOf(plainText.charAt(i)); if (charPosition != -1) { cipherText += KEY.charAt(charPosition); } else { cipherText += plainText.charAt(i); } } return cipherText; }
static String decrypt(String cipherText) { cipherText = cipherText.toLowerCase(); String plainText = ""; for (int i = 0; i < cipherText.length(); i++) { int charPosition = KEY.indexOf(cipherText.charAt(i)); if (charPosition != -1) { plainText += ALPHABET.charAt(charPosition); } else { plainText += cipherText.charAt(i); } } return plainText; }
public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter the message: "); String message = scan.nextLine(); System.out.println("Encrypted message: " + encrypt(message)); System.out.println("Decrypted message: " + decrypt(encrypt(message))); scan.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
