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

The Caesar cipher is one of the earliest known and simplest ciphers . It is a ty

ID: 3727426 • Letter: T

Question

The Caesar cipher is one of the earliest known and simplest ciphers. It is a type of substitution cipher in which each letter in the plaintext is 'shifted' a certain number of places down the alphabet.

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

U

V

W

X

Y

Z

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

H

E

L

L

O

W

O

R

L

D

Normal Text

B

Y

F

F

I

Q

I

R

M

X

Encrypted Text

You are tasked to create an encryption program which takes a key word, a variation on the Caesar Cipher. The keyword will fill the first part of the shifted array. The remaining letters will fill the end of the array. It should look like this:

Key=”TOY

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

T

O

Y

A

B

C

D

E

F

G

H

I

J

K

L

M

N

P

Q

R

S

U

V

W

X

Z

You should have an interactive menu with the following options:

1 Enter Key word //takes new key word and re-aligns shifted array

2 Encrypt message//takes a message of 140 characters and encrypts message

3 Decrypt message//takes an encrypted massage of 140 characters and decrypts it

9 Quit//exits program;

A couple tips:

If you have duplication of letters in the word, only use the first instance of that letter. For example, if my key word is “hello” then the program will strip the additional ‘l’ and use “helo”.

Use the copy function from the <cstring> library not the <algorithm> library. If you use the algorithm library you need to make a change in visual studios precompiler settings or you will get an error. You cannot assign arrays using the ‘=’ operator; only individual indices of the array can be assigned this way.

Take one case or the other for input. All Caps or all lowercase. You can use “toupper(<char>)” to set all letters to upper case: http://cpp.sh/7gwx (Links to an external site.)Links to an external site.

/* toupper example */

#include <stdio.h>

#include <iostream>

#include <ctype.h>//for toupper

using namespace std;

int main ()

{

int i=0;

char str[]="Test String. ";

char c;

while (str[i])

{

    c=str[i];

    str[i]=toupper(c);

    i++;

}

std::cout<<str<<endl;

return EXIT_SUCCESS;

}

When encrypting/decrypting, don’t change characters that are not in your Array set like punctuation and spaces.

* Also, use a file system. That is create a text file with the encrypted message, and be able to read an encrypted text file and decrypt it. This would be options:

- create encrypted file // takes message and creates text file with encrypted message

- decrypt file //Ask for file path, reads document and decrypts message

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

U

V

W

X

Y

Z

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

Explanation / Answer

CODE:

#include <iostream>

#include <string>

#include <cstring>

using namespace std;

char alpa[26];

char c = 'A';

// create alpabets based on key.

static void Cipher(string key)

{

   int i = 0;

   // initiallly store the key

   for (;i<key.length();i++){

       alpa[i] = key[i];

   }

   // and then the alphabets.

for(;i<26;i++){

       alpa[i] = c;

       c++;

   }

}

// encrypt here

static string Encipher(string input)

{

   string output = "";

    // check if the input is alphabet and convert it to uppercase and then encrypt.

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

       if(isalpha(input[i])){

           char c = toupper(input[i]);

           output += alpa[int(c)-65];

       }

       else

          output += " ";

   }

   return output;

}

int main(){

   string key;

   string message;

   cout << "Enter the KeyWord: " << endl;

   cin >> key;

   Cipher(key);

   cout << " Enter the message to Encrypt" << endl;

   cin >> message;

   cout << "Encrypted message is " << endl;

   cout << Encipher(message) << endl;

}

output:

$ ./a.out

Enter the KeyWord:

uvwxyz

Enter the message to Encrypt

HelloWorld

Encrypted message is

ByFFIQILFx