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

The Caesar cipher is a very simple way of creating coded messages. The original

ID: 3549105 • Letter: T

Question

The Caesar cipher is a very simple way of creating coded messages. The original cipher, attributed to Julius Caesar, involves shifting every letter in a plaintext message 3 positions further down in the alphabet to get the corresponding ciphertext message. For example, a Q in the plaintext message would be shifted by three positions to become a T in the ciphertext. A plaintext message of "YAK AND ZEBRA" would be transformed to the ciphertext message "BDN DQG CHEUD". Note that letters near the end of the alphabet wrap around to the beginning (e.g. Y + 3 = B). You can, of course, perform a Caesar cipher transformation using other values than 3 for the shift. "YAK AND ZEBRA" with a shift of 4, for example, becomes "CEO ERH DIFVE".

In this assignment, you will write a javascript or C# program that enciphers and deciphers secret messages using a Caesar cipher. Your program should prompt the user for a message to be encoded and a key value (the Caesar shift value), and call the function, caesarEncipher, to obtain the ciphertext of the message, and print the ciphertext. You also need prompt the user for a message to be decoded and a key value (the Caesar shift value), and call the function, caesarDecipher, to obtain the plaintext of the message, and print the plaintext.

Your task

1. Write a function to provide a menu of choices, and read in a single integer. The value of that integer will determine what the program will do: 1 is encrypt plain text, 2 is decrypt ciphertext, and 3 is exit the program.

2. Write a function named caesarEncipher that accepts a key value (the Caesar shift value) as its first command-line parameter, and a string (the plaintext) for its second parameter. Your caesarEncipher function should look like this:

caesarEncipher (shift, plaintext):

/* Returns ciphertext obtained by transforming every letter in the plaintext by a Caesar cipher by

the specified shift. Non-letter characters should remain unchanged in the ciphertext.*/

[Your code goes here]

return [something]

Explanation / Answer

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace caesar

{

class Program

{

// both functions valid only for uppercase characters

public string caesarEncipher(int key, string text)// encipher function

{

string modtext = "";

int y;

for (int i = 0; i < text.Length; i++)

{

if (text[i] >= 65 && text[i] <= 91)

{

y = (int)(text[i] + key);

if (y > 91) //accounting for when ascii values exceed that of Z

y = 65 + (y - 91);

modtext += (char)y;


}

else

modtext += text[i];

}

return modtext;

}


public string caesarDecipher(int key, string modtext1)

{

string text1 = "";

int x;

for (int i = 0; i < modtext1.Length; i++)

{

if (modtext1[i] >= 65 && modtext1[i] <= 91)

{

x = (int)(modtext1[i] - key);

if (x < 65) // accounting for when ASCII values are less than that of A

x = 91 - (65 - x);

text1 += (char)x;

}

else

text1 += modtext1[i];

}

return text1;

}










static void Main(string[] args)

{

Program p1 = new Program();

while (true)

{

Console.WriteLine("enter choice: 1.Cipher 2.Decipher 3.Quit");

int choice = int.Parse(Console.ReadLine());

switch (choice)

{

case 1:

Console.WriteLine("enter text to encipher:");

string s1 = Console.ReadLine();

Console.WriteLine("enter key");

int key = int.Parse(Console.ReadLine());

string mods1 = p1.caesarEncipher(key, s1);

Console.WriteLine(" " + mods1);

break;

case 2:

Console.WriteLine("enter text to decipher:");

string s2 = Console.ReadLine();

Console.WriteLine("enter key");

int key2 = int.Parse(Console.ReadLine());

string mods2 = p1.caesarDecipher(key2, s2);

Console.WriteLine(" " + mods2);

break;

case 3:

return;


}

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote