In: Computer Science
Caesar Cipher in Java Problem?
Objective
Practice the cumulative sum and char data type
Problem
You want to create an app to encrypt the text messages that you send to your friends. Once your friend gets the message, the message should be decrypted so that your friend understands it. To implement this app Caesar cipher algorithm should be used. Caesar Cipher text is formed by rotating each letter by a given amount. For example, if you rotate the letter ‘A’ by 3 you should get ‘D’. rotate ‘B’ by 3 you should get ‘E’. Toward the end of the alphabet you wrap around, if example rotate ‘X’ by 3 you should get ‘A’. rotate ‘Y’ by 3 you should get ’B’
Methods
Public static void main(String[] args)
public static void run(Scanner kb)
Public static String encrypt (String message, int key)
{
Convert the message to uppercase using the method toUpperCase from the String class
Declare a variable of type string called result to hold the encrypted message, initialize it to “”;
Create a loop to go through each letter of the message
{
Get each letter and store it in a variable of type char called c, use the charAt method: char c = message.charAt(i)
If the letter is between ‘A’ and ‘Z’
{
Add the key to the letter: c = c + key
//checking for wrap around
If c is greater than ‘Z’
{
Subtract 26 from c
}
else
{
Add 26 to c
}
}//end of if
Add the content of the variable c to the variable result (cumulative sum)
}//end of the loop
Return result
}//end of the method
Public static String decrypt (String message, int key):
{
Declare a String called result and initialize it to “”
Create a for loop to go through each letter of the message
{
Get the character at each index char c = message.charAt(i)
If the variable c is between ‘A’ and ‘Z’
{
Subtract the value of the variable key from the variable c
If the content of the variable c is less than ‘A’ //check for wrap around
{
Find the difference between the letter ‘A’ and the variable c : int diff = ‘A’ - c
c = (char)(‘Z’ – diff + 1)
}
else if c > ‘Z’ //
{
Int diff = ‘Z’ - c
C = (char)((‘A’ + diff + 1)
}
}
Concatenate the variable c to the variable result
}//end for
Return result
}//end of the method
Requirements
Sample output:
How many times to you want to use the app: 4
Your message? I love java programming
Encoding key? 5
The encrypted message is:
N QTAJ OFAF UWTLWFRRNSL
The decrypted message is:
I LOVE JAVA PROGRAMMING
import java.util.*;
public class CaesarCipherProgram {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println(" Input the plaintext message : ");
String plaintext = sc.nextLine();
System.out.println(" Enter the value by which
each character in the plaintext
message gets shifted : ");
int shift = sc.nextInt();
String ciphertext = "";
char alphabet;
for(int i=0; i < plaintext.length();i++)
{
// Shift one character at a time
alphabet = plaintext.charAt(i);
// if alphabet lies between a and z
if(alphabet >= 'a' && alphabet <= 'z')
{
// shift alphabet
alphabet = (char) (alphabet + shift);
// if shift alphabet greater than 'z'
if(alphabet > 'z') {
// reshift to starting position
alphabet = (char) (alphabet+'a'-'z'-1);
}
ciphertext = ciphertext + alphabet;
}
// if alphabet lies between 'A'and 'Z'
else if(alphabet >= 'A' && alphabet <= 'Z') {
// shift alphabet
alphabet = (char) (alphabet + shift);
// if shift alphabet greater than 'Z'
if(alphabet > 'Z') {
//reshift to starting position
alphabet = (char) (alphabet+'A'-'Z'-1);
}
ciphertext = ciphertext + alphabet;
}
else {
ciphertext = ciphertext + alphabet;
}
}
System.out.println(" ciphertext : " + ciphertext);
}
}