Question

In: Computer Science

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

C++

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. 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

4 Quit//exits program;

A couple tips:

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

/* toupper example */

#include <stdio.h>

#include <iostream>

#include <ctype.h>//for toupper

using namespace std;

int main ()

{

int i=0;

char str[]="Test String.\n";

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

Solutions

Expert Solution

/* C++ program to encrypt and decrypt the message using a meeu*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

using namespace std;

int checkLetter(char key[],char letter);
void lowercase(char s[]);
int find(char E[],char c);

//main function
int main()
{
int j,avail,keylen,option=1;
char E[27],D[27],letter;
char key[27];
char normal[140],encrypted[140];
//menu
while(option !=4)
{
cout<<endl<<"\n1: Enter the key :";
cout<<endl<<"\n2: Encrypt message :";
cout<<endl<<"\n3: Decrypt message :";
cout<<endl<<"4: Quit Program";
cout<<endl<<"Enter option(1,2,3,4)";
cin>>option;
switch(option)
{
case 1: //enter key and align the decryption string
cout<<"Enter tke key : ";
cin>>key;
lowercase(key);
strcpy(E,key);
letter='a';
for(j=strlen(key);j<26;j++)
{
avail=checkLetter(key,letter);
if(avail==0)
{
E[j]=letter;
E[j+1]='\0';
letter++;
}
else
{
letter++;
j--;
}
}
E[26]='\0';


case 2: // enter the mesggage for encryption
cout<<endl<<"enter the message for encryption: ";
cin.ignore();
fgets(normal, 140, stdin);
//gets(normal);
lowercase(normal); //convert normal text into lower case
int i;
for(i=0;normal[i]!='\0';i++)
{
if(normal[i]>=97 && normal[i]<= 122)
encrypted[i]=E[normal[i]-97];
else
encrypted[i]=normal[i];
}
encrypted[i]='\0';
cout<<endl<<"Encrypted to : "<<encrypted;
break;

case 3: // enter the mesggage for decryption
cout<<endl<<"enter the message for decryption : ";
cin.ignore();
fgets(encrypted, 140, stdin);
//gets(encrypted);
lowercase(encrypted);
for( i=0;encrypted[i]!='\0';i++)
{
if(encrypted[i]>=97 && encrypted[i]<= 122)
{
int loc=find(E,encrypted[i]);// returns the index of a character in encypted message in E
normal[i]=loc+97;;
}
else
normal[i]=encrypted[i];
}
normal[i]='\0';
cout<<endl<<"Decrypted to : "<<normal;
break;
}
}

return 0;


}

//this function returns the letter from alphabet is already
// exist in then key or not
int checkLetter(char key[],char letter)
{
int i=0;
while(key[i]!='\0')
{
if(key[i]==letter)
return 1;
i++;
}
return 0;
}

//This function converts a string s to lower case letter
void lowercase(char s[])
{
for(int i=0;s[i]!='\0';i++)
{
s[i]=tolower(s[i]);
}
}


//This function returns the location of a character in string E
int find(char E[],char c)
{
for(int i=0;E[i]!='\0';i++)
{
if(E[i]==c)
return i;
}
return -1;
}


/*
output:

1: Enter the key :

2: Encrypt message :

3: Decrypt message :
4: Quit Program
Enter option(1,2,3,4)1
Enter tke key : top

enter the message for encryption: HELLO WORLD

Encrypted to : ebiil wlqia


1: Enter the key :

2: Encrypt message :

3: Decrypt message :
4: Quit Program
Enter option(1,2,3,4)2

enter the message for encryption: HELLO WORLD

Encrypted to : ebiil wlqia


1: Enter the key :

2: Encrypt message :

3: Decrypt message :
4: Quit Program
Enter option(1,2,3,4)3

enter the message for decryption : HELLO WORLD

Decrypted to : khoob wbsog


1: Enter the key :

2: Encrypt message :

3: Decrypt message :
4: Quit Program
Enter option(1,2,3,4)3

enter the message for decryption : ebiil wlqia

Decrypted to : hello world


1: Enter the key :

2: Encrypt message :

3: Decrypt message :
4: Quit Program
Enter option(1,2,3,4)4


*/


Related Solutions

Julius Caesar used one of the earliest known cipher systems to communicate with Cicero in Rome...
Julius Caesar used one of the earliest known cipher systems to communicate with Cicero in Rome while he was conquering Europe. Caesar knew that there was a very high risk of ambush or spies when sending messages; therefore, he developed a cryptographic system now known as the Caesar cipher. Primary Task Response: Please provide a detailed response to the below to include specific details and examples. What is the Caesar ROT3 Cipher? How does it work? Although the Caesar cipher...
In cryptography, Caesar cipher is one of the simplest encryption techniques. The key idea of this...
In cryptography, Caesar cipher is one of the simplest encryption techniques. The key idea of this method is to replace each plaintext letter with one fixed number of places down the alphabet. Below is an example with a shift of three: Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz Cipher: DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABC To cipher a string, ‘A’ is replaced by ‘D’, ‘B’ is substituted by ‘E’, and so on. To decode a string, ‘A’ is replaced by ‘x’, etc. By using python with vs code: Write a...
3.3 Briefly define the following ciphers: -Caesar cipher -Monoalphabetic cipher -Playfair cipher 3.4 What is steganography?
3.3 Briefly define the following ciphers: -Caesar cipher -Monoalphabetic cipher -Playfair cipher 3.4 What is steganography?
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or...
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. Given an arbitrary cipher text file, you need to write a C++ program to find out the value of the shift going down the...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR...
Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR A SINGLE WORD remember, read, understand, and use the waterpump.cpp program will process an arbitrary amount of input text, however long. Arrange a stream in a two dimensional array of characters by filling up the array a line at a time, from leftmost column to rightmost column. DO NOT USE A GETLINE: this program should be able to input an entire library, not a...
use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide...
use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide the following inputs and outputs for each function/program: EncryptCaesar Two inputs: A string of the plaintext to encrypt A key (a number) ▪ For the Caesar cipher: This will indicate how many characters to shift (e.g. for a key=3, A=>D, B=>E, ..., X=>A, Y=>B, Z=>C). Note that the shift is circular. One output: ◦ A string of the ciphertext or codeword DecryptCaesar Two inputs:...
I want to compare between breaking the shift (Caesar Cipher), substitution, and Vigenere cipher by using...
I want to compare between breaking the shift (Caesar Cipher), substitution, and Vigenere cipher by using a chosen-plaintext attack or by known-plaintext attack. How much plaintext must be encrypted in order for the adversary to completely recover the key for those ciphers in chosen-plaintext attack and in known-plaintext attack? is there difference in each attack?
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its...
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its input and writes the characters to its output encrypted with a Caesar cipher. A Caesar cipher shifts each letter a certain number of positions in the alphabet. The number of positions to shift will be given to your program as a command line argument. Characters other than letters should not be encrypted. Your program should stop only at the end of input. Your program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT