Question: Please add detailed comments in the following both codes and submit the new code. Code 1 class CaesarCipher { public static void main(String args[]) {

Please add detailed comments in the following both codes and submit the new code.

Code 1

class CaesarCipher { public static void main(String args[]) { try{ String plaintext = args[0]; if(args.length>1) { System.out.println("To many arguments !"); System.exit(0); } int shift = 3; String ciphertext = ""; char alphabet; for(int i=0; i < plaintext.length();i++) { alphabet = plaintext.charAt(i); if(alphabet >= 'a' && alphabet <= 'z') { alphabet = (char) (alphabet + shift); if(alphabet > 'z') { alphabet = (char) (alphabet+'a'-'z'-1); } ciphertext = ciphertext + alphabet; }else if(alphabet >= 'A' && alphabet <= 'Z') { alphabet = (char) (alphabet + shift); if(alphabet > 'Z') { alphabet = (char) (alphabet+'A'-'Z'-1); } ciphertext = ciphertext + alphabet; }else { ciphertext = ciphertext + alphabet; } } System.out.println(ciphertext); }catch(Exception e) { System.out.println("Too few arguments !"); System.exit(0); } } }

Code 2

import java.io.*;

import java.util.*;

public class Solution { //to keep track of index

public static final String alpha = "abcdefghijklmnopqrstuvwxyz";

publicstaticStringencrypt(Stringmessage,intshiftKey) {

message = message.toLowerCase();

String cipherText = "";

for (int ii = 0; ii < message.length(); ii++) {

int charPosition = aplha.indexOf(message.charAt(ii));

int keyVal = (shiftKey + charPosition) % 26;

char replaceVal = aplha.charAt(keyVal);

cipherText += replaceVal;

}

return cipherText;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String message = new String();

int key = 0;

System.out.print("Enter the String for Encryption:");

message = sc.next();

System.out.println(" Enter Shift Key:");

key = sc.nextInt();

System.out.println(" Encrpyted msg:" + encrypt(message, key));

} //main method ends

} //Solution Class End

Step by Step Solution

3.48 Rating (161 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Adding the detailed comments to the following code Code 1 Implementing the Caesar Cypher encryption technique is the aim of Code 1 With the Caesar Cypher every letter in the plaintext is changed by a ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!