In: Computer Science
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
/* 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
*/