Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives To gain experience and practice with working with arrays and characte

ID: 3684982 • Letter: O

Question

Objectives

To gain experience and practice with working with arrays and character manipulation.

Problem

A secret spy group has hired you to help them obfuscate their messages. They want to be able

to encrypt or decrypt messages using three different cyphers: ROT13, Caesarian Shift, and Affine

Cipher.

Specifications (overview)

As we’ve done for many labs and projects, this program will keep running until the user selects the “quit” option from the main menu. As we normally do, start by displaying the main menu. In this case, the main menu will allow the user to choose between several ciphers. Once the user has made a selection, ask for a message they would like to process (make sure you get the entire message including spaces and punctuation). Next, ask the user if they would like to encrypt or decrypt the message (if they make an invalid selection here, return them to the main menu).

At this point, we move on to cipher-specific tasks. If the user selects:

ROT13

o Process the user’s message using the rot13 function and display the output.

o Note that the rot13 function will handle both decoding and encoding.

Affine

o Regardless of if the user would like to encode or decode their message, ask the

user for a value for a and b. Note that these values must be relatively

prime/coprime but we will trust that the user knows this and will only enter

valid values so we won’t bother checking.

o Once you have the values for a and b, use the affine_encrypt or

affine_decrypt function (as appropriate) to either encode or decode the

user’s message. Next, display the result.

Caesar

o Regardless of if the user would like to encode or decode their message, ask

them for a shift value.

o Once you have the shift value, use the caesar_encrypt or

caesar_decrypt function (as appropriate) to either encode or decode the

user’s message. Next, display the result.

o Note that decoding a Caesar message with a shift of some value k is the same as

encoding a Caesar message with a shift of the value 26-k.

Specifications (specific)

Use the following functions:

void rot13(int s, const char input[], char output[]);

o s stores the size of the string

o input[] stores the user’s input

o output[] is where the encoded/decoded message will be put

o This function takes the string stored in input[] and, character-by-character,

encodes the string using the ROT13 cipher and stores the resulting characters in

the array output[].

void affine_encrypt(int s, const char input[], char

output[], int a, int b);

o s stores the size of the string.

o input[] stores the user’s input.

o ouput[] is where the encoded message will be put.

o a is the first key of the affine cipher.

o b is the second key of the affine cipher.

o This function takes the string stored in input[] and, character-by-character,

encodes the string using the affine cipher and stores the resulting characters in

the array output[].

void affine_decrypt(int s, const char input[], char

output[], int a, int b);

o s stores the size of the string.

o input[] stores the user’s input.

o ouput[] is where the encoded message will be put.

o a is the first key of the affine cipher.

o b is the second key of the affine cipher.

o This function takes the string stored in input[] and, character-by-character,

decodes the string using the affine cipher and stores the resulting characters in

the array output[].

void caesar_encrypt(int s, const char input[], char

output[], int shift);

o s stores the size of the string.

o input[] stores the user’s input.

o ouput[] is where the encoded message will be put.

o shift stores the shift amount.

o This function takes the string stored in input[] and, character-by-character,

encodes the string using the Caesar cipher and stores the resulting characters in

the array output[].

void caesar_decrypt(int s, const char input[], char

output[], int shift);

o s stores the size of the string.

o input[] stores the user’s input.

o ouput[] is where the encoded message will be put.

o shift stores the shift amount.

o This function takes the string stored in input[] and, character-by-character,

decodes the string using the Caesar cipher and stores the resulting characters in

the array output[].

int getLength(const char input[]);

o This function scans through the characters stored in input[] looking for the

‘’ character (which designates the end of the c-string). Once it finds the

character, it returns the length. If it doesn’t find the character, it just returns

the maximum length you set for your char arrays.

Sample Output
Choose a cypher:
1 - ROT13
2 - Affine Cipher
3 - Caesar Cipher
4 - QUIT!
> 1
Enter a message to be processed
> TEst Message!
Which direction?
1 - Encrypt
2 - Decrypt
> 1
Your encoded message is: GRfg Zrffntr!
Choose a cypher:
1 - ROT13
2 - Affine Cipher
3 - Caesar Cipher
4 - QUIT!
> 1
Enter a message to be processed
> grFG zRFFNTR.
Which direction?
1 - Encrypt
2 - Decrypt
> 2
Your encoded message is: teST mESSAGE.
Choose a cypher:
1 - ROT13
2 - Affine Cipher
3 - Caesar Cipher
4 - QUIT!
> 2
Enter a message to be processed
> TEST MESSAGE =D
Which direction?
1 - Encrypt
2 - Decrypt
> 1
Enter a value for a and b: 5 3
Your encoded message is: UXPU LXPPDHX =S
Choose a cypher:
1 - ROT13
2 - Affine Cipher
3 - Caesar Cipher
4 - QUIT!
> 2

Enter a message to be processed
> uxpu lxppdhx =S
Which direction?
1 - Encrypt
2 - Decrypt
> 2
Enter a value for a and b: 5 3
Your encoded message is: test message =D
Choose a cypher:
1 - ROT13
2 - Affine Cipher
3 - Caesar Cipher
4 - QUIT!
> 3
Enter a message to be processed
> test MESSAGE.
Which direction?
1 - Encrypt
2 - Decrypt
> 1
Enter a shift amount: 10
Your encoded message is: docd WOCCKQO.
Choose a cypher:
1 - ROT13
2 - Affine Cipher
3 - Caesar Cipher
4 - QUIT!
> 3
Enter a message to be processed
> docd wocckqo.
Which direction?
1 - Encrypt
2 - Decrypt
> 2
Enter a shift amount: 10
Your encoded message is: test message.
Choose a cypher:
1 - ROT13
2 - Affine Cipher
3 - Caesar Cipher
4 - QUIT!
> 4
Exiting...

Explanation / Answer

import java.io*;

import java.lang.*;

import java.util.*;

void rot13(int s, const char input[], char output[]) {

int n; int m;

char input[],output[];

String s;

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

System.out.println(“Enter size:”);

n = br.readLine();

System.out.println(“Enter Text to be processed:”);

s = br.readLine();

for (int i=0;i<=n;i++) {

for(int j=0;j<=n;j++) {

input[i] = s.charAt(j);

} }

System.out.println(“Enter Choice:”);

int ch = br.readLine();

switch(ch) {

case 1: System.out.println(“1-Encrypt”);

m= br.readLine();

void rot13_encrypt() {

for(int i=0;i<=n;i++) {

if( (input[i] >=’A’) && (input[i] <=’Z’))

output[i] = (((input[i] –‘a’) +13) %26) + ‘a’;

}

System.out.println(“Your encoded Message is:”,output[]);

}

break;

case 2: System.out.println(“2-Decrypt”);

m= br.readLine();

void rot13_decrypt() {

for(int i=0;i<=n;i++ ) {

if(input[i] >=’a’ && input[i] <=’m’) output[i] +=13;

else if(input[i] >=’A’ && input[i] <=’M’) output[i]+=13;

else if(input[i] >=’n’ && input[i] <=’z’) output[i] -=13;

else if(input[i] >=’N’ && input[i] <=’Z’) output[i]-=13;

}

System.out.println(“Your decoded Message is:”,output[]);

}

break;

case default: System.out.println(“Invalid”);

main();

} }

void affineCipher(int s, const char input[], char output[],int a,int b) {

int n; int m;

char input[],output[];

String s;

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

System.out.println(“Enter size:”);

n = br.readLine();

System.out.println(“Enter Text to be processed:”);

s = br.readLine();

for (int i=0;i<=n;i++) {

for(int j=0;j<=n;j++) {

input[i] = s.charAt(j);

} }

System.out.println(“Enter Choice:”);

int ch = br.readLine();

switch(ch) {

case 1: System.out.println(“1-Encrypt”);

m= br.readLine();

void affine_encrypt() {

int a,b;

BufferedReader br = new BufferedReader(new InputSteramReader(System.in));

System.out.printlln(“Enter values for a and b”);

a = br.readLine();

b = br.readLine();

for(int i=0;i<=n;i++) {

output[i] = (a*input[i] +b) mod n;

}

System.out.println(“Your encoded Message is:”,output[]);

}

break;

case 2: System.out.println(“2-Decrypt”);

m= br.readLine();

void affine_decrypt() {

int a,b;

BufferedReader br = new BufferedReader(new InputSteramReader(System.in));

System.out.printlln(“Enter values for a and b”);

a = br.readLine();

b = br.readLine();

for(int i=0;i<=n;i++ ) {

output[i] = (a-1 (input[i] –b)) mod n;

}

System.out.println(“Your decoded Message is:”,output[]);

}

break;

case default: System.out.println(“Invalid”);

main();

} }

void affineCipher(int s, const char input[], char output[],int a,int b) {

int n; int m;

char input[],output[];

String s;

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

System.out.println(“Enter size:”);

n = br.readLine();

System.out.println(“Enter Text to be processed:”);

s = br.readLine();

for (int i=0;i<=n;i++) {

for(int j=0;j<=n;j++) {

input[i] = s.charAt(j);

} }

System.out.println(“Enter Choice:”);

int ch = br.readLine();

switch(ch) {

case 1: System.out.println(“1-Encrypt”);

m= br.readLine();

void caesar_encrypt() {

int shift;

BufferedReader br = new BufferedReader(new InputSteramReader(System.in));

System.out.printlln(“Enter value for shift”);

shift= br.readLine();

for(int i=0;i<=n;i++) {

output[i] =(input[i] + shift) mod 26;

}

System.out.println(“Your encoded Message is:”,output[]);

}

break;

case 2: System.out.println(“2-Decrypt”);

m= br.readLine();

void affine_decrypt() {

int shift;

BufferedReader br = new BufferedReader(new InputSteramReader(System.in));

System.out.printlln(“Enter value for shift”);

shift = br.readLine();

for(int i=0;i<=n;i++ ) {

output[i] =(input[i] – shift) mod 26;

}

System.out.println(“Your decoded Message is:”,output[]);

}

break;

case default: System.out.println(“Invalid”);

main();

}

public class Cipher {

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

int ch ;

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

System.out.println(“Choose a cipher:”);

ch = br.readLine();

switch(ch) {

case 1: System.out.println(“1-ROT13”);

void rot13();

break;

case 2 : System.out.println(“2-Affine Cipher”);

void affineCipher();

break;

case 3: System.out.println(“3-Caesar cipher”);

void caesarCipher();

break;

case 4: System.out.println(“4-Quit”);

exit();

break;

case default: System.out.println(“Invalid Choice”);

}

void getLength() {

int n;

String s;

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

System.out.println(“Enter size:”);

n = br.readLine();

System.out.println(“Enter Text to be processed:”);

s = br.readLine();

for (int i=0;i<=n;i++) {

for(int j=0;j<=n;j++) {

input[i] = s.charAt(j);

} }

System.out.println(“Enter character to be searched:”);

char c = br.readLine();

if(s !=’’) {

for(int i=0;i<=n;i++) {

if(c ==input[i]) {

return c.length();

}else return n;

}

} }

}

}