Part 1 – Program Description and Requirements Background: The Caesar cipher is a
ID: 3769098 • Letter: P
Question
Part 1 – Program Description and Requirements
Background: The Caesar cipher is a form of (very weak) encryption that converts text by shifting each character by n characters. For example, with a shift of n = 1 the text “hello” becomes “ifmmp”. The original text is called plaintext, and we’ll call the end result encoded text.
Problem: You will implement the Dingler cipher to encrypt a given plaintext string. The Dingler cipher works by shifting the text by an n given by the user – the text is shifted by using the collating sequence property of ASCII*. In order to make the encrypted text harder to decipher than in a Caesar cipher, after shifting, the text is also flipped. For example, for n = 2 with the plaintext “hello” we get:
Plaintext: hello
Shifted by 2: jgnnq
Flipped: qnngj
Your task is to write a program that converts plaintext to encoded text.
*Since we will be assuming ASCII, the shift results may be somewhat different from a regular Caesar cipher. For more information on shifting using ASCII, refer to the lecture slides. Hint: look at the ASCII table – how can I change a character by n given how characters are represented numerically in ASCII? Hint #2: try this first to simplify the problem – create a char variable (char test = ‘a’;) and determine now to turn this into a ‘b’ without writing test = ‘b’.
Requirements:
1. You must not use any global variables.
2. You must create and use at least the following three functions:
a. A function that asks the user for a string (the plaintext) and an integer number to shift each character in the plaintext string by. Both will be returned to the caller of the function.
b. A function that accepts the user’s string, shifts it by the user’s desired amount, and returns the shifted string. i. Look at the hint above regarding ASCII representation.
c. A function that accepts the shifted string and prints the string to the screen in reverse.
3. Use any other functions that you think are appropriate.
4. The main function should call the above functions in order to shift and flip the user’s plaintext and print the result.
Explanation / Answer
Chegg Expert Answer
CaeserCipher.java
//package caesar;
import java.io.*;
import java.util.Scanner;
public class CaeserCipher {
int offset = 3;
int usershiftbits;
public String function1(String s, int shiftbits) throws IOException {//this function contaons two arguments user inputted string and shitbits
StringBuilder sb = new StringBuilder();//contains string methods
for (int i = 0; i < s.length(); i++) {
char t = s.charAt(i);
if (t >= 'A' && t <= 'Z') {
int t1 = t - 'A' + shiftbits;
t1 = t1 % 26;
sb.append((char) (t1 + 'A'));//appends the string
} else if (t >= 'a' && t <= 'z') {
int t1 = t - 'a' + shiftbits;
t1 = t1 % 26;
sb.append((char) (t1 + 'a'));
}
}
return sb.toString(); //converting stringbuffer to string
}
public String function2(String s) throws IOException {//this function contains user inputted string as arguments
Scanner sc = new Scanner(System.in);
System.out.println("enter user shitbits for function2");
usershiftbits = sc.nextInt();
StringBuilder sb = new StringBuilder();//contains string builder metods
for (int i = 0; i < s.length(); i++) {
char t = s.charAt(i);
if (t >= 'A' && t <= 'Z') {
int t1 = t - 'A' + usershiftbits;
t1 = t1 % 26;
sb.append((char) (t1 + 'A'));
} else if (t >= 'a' && t <= 'z') {
int t1 = t - 'a' + usershiftbits;
t1 = t1 % 26;
sb.append((char) (t1 + 'a'));
}
}
return sb.toString();//converting stringbuffer to string
}
public String function3(String s) throws IOException {//this function decrypts function1 encrypted values
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char t = s.charAt(i);
if (t >= 'A' && t <= 'Z') {
int t1 = t - 'A' - 2;
if (t1 < 0) {
t1 = 26 + t1;
}
sb.append((char) (t1 + 'A'));
} else if (t >= 'a' && t <= 'z') {
int t1 = t - 'a' - 2;
if (t1 < 0) {
t1 = 26 + t1;
}
sb.append((char) (t1 + 'a'));
}
}
return sb.toString();
}
public String function4(String s) throws IOException {//this function decrpted function2 encrypted values
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char t = s.charAt(i);
if (t >= 'A' && t <= 'Z') {
int t1 = t - 'A' - usershiftbits;
if (t1 < 0) {
t1 = 26 + t1;
}
sb.append((char) (t1 + 'A'));
} else if (t >= 'a' && t <= 'z') {
int t1 = t - 'a' - usershiftbits;
if (t1 < 0) {
t1 = 26 + t1;
}
sb.append((char) (t1 + 'a'));
}
}
return sb.toString();//converting string buffer to string
}
public static void main(String[] args) {
try {//exception handling
System.out.println("Caesar encrypion technique");
BufferedReader b;
String oriTxt, encTxt1, encTxt2, decTxt1, decTxt2;
System.out.println("Enter string to encrypt:");
b = new BufferedReader(new InputStreamReader(System.in));
oriTxt = b.readLine();
CaeserCipher c = new CaeserCipher();
encTxt1 = c.function1(oriTxt, 2);
System.out.println("Encrypted text for function1 :" + encTxt1);
encTxt2 = c.function2(oriTxt);
System.out.println("Encrypted text for function2 :" + encTxt2);
decTxt1 = c.function3(encTxt1);
System.out.println("flipped(decrypted) text for function 1 :" + decTxt1);
decTxt2 = c.function4(encTxt2);
System.out.println("flipped(decryptped) text for function 2 :" + decTxt2);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
output
run:
Caesar encrypion technique
Enter string to encrypt:
chegg
Encrypted text for function1 :ejgii
enter user shitbits for function2
3
Encrypted text for function2 :fkhjj
flipped(decrypted) text for function 1 :chegg
flipped(decryptped) text for function 2 :chegg
BUILD SUCCESSFUL (total time: 20 seconds)
**********************************ALL THE BEST FROM CHEGG*****************************************