Question

In: Computer Science

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Main { static Scanner sc=new Scanner(System.in);...

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Scanner;

public class Main {

static Scanner sc=new Scanner(System.in);

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException {

// TODO code application logic here

System.out.print("Enter any String: ");

String str = br.readLine();

System.out.print("\nEnter the Key: ");

int key = sc.nextInt();

String encrypted = encrypt(str, key);

System.out.println("\nEncrypted String is: " +encrypted);

String decrypted = decrypt(encrypted, key);

System.out.println("\nDecrypted String is: "

+decrypted); System.out.println("\n");

}

public static String encrypt(String str, int key)

{ String encrypted = "";

for(int i = 0; i < str.length(); i++) {

int c = str.charAt(i);

if (Character.isUpperCase(c)) {

c = c + (key % 26);

if (c > 'Z')

c = c - 26;

}

else if (Character.isLowerCase(c)) {

c = c + (key % 26);

if (c > 'z')

c = c - 26;

}

encrypted += (char) c;

}

return encrypted;

}

public static String decrypt(String str, int key)

{ String decrypted = "";

for(int i = 0; i < str.length(); i++) {

int c = str.charAt(i);

if (Character.isUpperCase(c)) {

c = c - (key % 26);

if (c < 'A')

c = c + 26;

}

else if (Character.isLowerCase(c)) {

c = c - (key % 26);

if (c < 'a')

c = c + 26;

}

decrypted += (char) c;

}

return decrypted;

}

}

What is the Caesar cipher?

Write the algorithm of the illustrated program?

Solutions

Expert Solution

What is the Caesar cipher?

Ciphertext is an encrypted version of plaintext. A plaintext is converted to a cipher text ny using an encryption algorithm. It is the unreadable output of an encryption algorithm and it can not be understandable until it has been converted into plain text using a key. Caesar Cipher technique is one of the earliest and most widely used encryption technique. It is also known as  shift cipher, Caesar's code or Caesar shift. It’s simply a type of substitution cipher, i.e., each letter of a given text is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 2, A would be replaced by C, B would become D, and so on.

Write the algorithm of the illustrated program?

The above program is based on the substitution cipher technique where any character of plain text from the given fixed set of characters is substituted by some other character from the same set depending on a key. The algorithm is mentioned below.

Algorithm :

  • Get the key value (an integer value between 0-25 which is required to denote the required shift) and the text (a string of lower case letters / upper case letters ) which needs to be encrypted.
  • Traverse the given text one character at a time .
  • Now, substitute each and every characters of the given text as per the given key value.
  • Return the new string generated.

************************************************************************************************************************

Begin:
Declare variable: str,key,c,encrypted, decrypted;
Print: "Enter any string: "
Input: str
Print: "Enter the key: "
Input: key
// Encryption
for each character (c) in str
   if c is UpperCase then
       c= c + (key % 26)
if (c> 'Z') then
            c= c-26
   else if c is UpperCase then
       c= c + (key % 26)
if (c> 'z') then
            c= c-26
   encrypted = encrypted + (char) c
End Loop
Print: encrypted

// Decryption      

for each character (c) in str
   if c is UpperCase then
       c= c - (key % 26)
if (c <'A') then
            c= c+26
   else if c is UpperCase then
       c= c - (key % 26)
if (c<'a') then
            c= c+26
   decrypted = decrypted + (char) c
End Loop
Print: decrypted

End:
***************************************************************************************************************************

The output of the above program is :


Related Solutions

import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num=0; String s = ""; while (true) { System.out.println("Enter an integer greater than 1: "); try { // reading input s = sc.nextLine(); // converting into int num = Integer.parseInt(s); break; } catch (Exception e) { System.out.println(s + " is not valid input."); } } // Now we have a valid number // putting into square int square = num; int count...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // parse the number of strings int numStrings = Integer.parseInt(sc.nextLine());    // parse each string String[] stringsArray = new String[numStrings]; for (int i = 0; i < numStrings; i++) { stringsArray[i] = sc.nextLine(); }    // print whether there are duplicates System.out.println(hasDuplicates(stringsArray)); }    private static boolean hasDuplicates(String[] stringsArray) { // TODO fill this in and remove the below line...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new...
import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new Scanner(System.in);           boolean gameOver = false; int colSize = 10; int rowSize= 10; String floorTile= "."; int playerX = 0; int playerY= 0; String playerTile="@"; int exitX= colSize-1; int exitY= rowSize-1; String exitTile="# "; int zombieX=5; int zombieY=5; // Defining Second Zombie int zombie2Y= 8; int zombie2X= 3; // Defining third zombie int zombie3Y= 1; int zombie3X= 7; String zombieTile="*"; String zombie2Tile="*";...
import java.util.Scanner; public class Grade { public static void main(String[] args) { Scanner scnr = new...
import java.util.Scanner; public class Grade { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // Reading score from user System.out.println("Enter the student's score:"); double score = scnr.nextDouble(); System.out.print("Grade:"); // Checking if score is less than 60 if(score<60){ System.out.println("F"); } // Checking if score is less than 70 else if(score<70){ System.out.println("D"); } // Checking if score is less than 80 else if(score<80){ System.out.println("C"); } // Checking if score is less than 90 else if(score<90){ System.out.println("B"); } // Checking...
Java Starter Code: import java.util.Scanner; public class GameScore { static Scanner keyboard = new Scanner(System.in); public...
Java Starter Code: import java.util.Scanner; public class GameScore { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { int team1[] = new int[4]; int team2[] = new int[4]; for (int qtr = 0; qtr < 4; qtr++) { quarterScoring(team1, team2, qtr); } int team1Total = teamTotal(team1); int team2Total = teamTotal(team2); displayGameResults(team1, team2); if (team1Total > team2Total) { System.out.println("Team 1 has won the game!"); } else { System.out.println("Team 2 has won the game!"); } } static int teamTotal(int[]...
import chapter6.date.SimpleDate; import java.util.Scanner; public class SimpleDateTestDefault { public static void main(String[] args) { Scanner stdin...
import chapter6.date.SimpleDate; import java.util.Scanner; public class SimpleDateTestDefault { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); SimpleDate d1 = new SimpleDate(); SimpleDate d2 = new SimpleDate(stdin.nextInt(), stdin.nextInt(), stdin.nextInt()); System.out.println(d1); System.out.println(d2); System.out.println(d1.before(d2)); System.out.println(d2.before(d1)); } } Implement SimpleDate class in chapter6.date package with the following attributes: day, (int type,  private) month, (int type,  private) year (int type,  private) The class should have the following methods: a constructor with three parameters: year, month, and day a constructor with no parameters which initialize the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT