-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonoalphabeticCipher.cpp
76 lines (58 loc) · 1.85 KB
/
MonoalphabeticCipher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//monoalphabetic cipher
//monoalphabatic ciphers are a form of Substitution ciphers which uses fixed substitution over the entire message
#include<iostream>
#include<string>
#include<cctype>
#include<stdlib.h>
using namespace std;
int main()
{
int choice,ascii,cpt,key;
string plaintext;
string ciphertext;
key = rand() % 26 ;
cout<<key;
for(;;)
{
cout<<"\n------------Enter your choice-----------\n"<<endl;
cout<<"\n1)Encrypt\n2)Decrypt\n3)Exit"<<endl;
cin>>choice;
switch(choice)
{ //encrypt case
case 1:
cout<<"Enter the plainText"<<endl;
cin>>ws;
getline(cin,plaintext); //to consider spaces in string input
for(unsigned i = 0 ; i<plaintext.size(); i++)
{
if(isalpha(plaintext[i]))
{
ascii = int(plaintext[i]); //storing the ascii value of the string
cpt = ((ascii + key)%26);//cipher text in ascii value
plaintext[i] = char(cpt);//converting ascii to character value
}//end if
}//end for
cout<<"The cipher Text is : "<<plaintext<<endl;//the cipher text
continue;
case 2://decryption
cout<<"Enter the Cipher Text"<<endl;
cin>>ws;//clearing whitespaces
getline(cin,ciphertext); //to consider spaces in string input
for(unsigned i = 0 ; i<ciphertext.size(); i++)
{
if(isalpha(ciphertext[i]))
{
ascii = int(ciphertext[i]); //storing the ascii value of the string
cpt = ((ascii - key) % 26);//cipher text in ascii value
ciphertext[i] = char(cpt);//converting ascii to character value
}//end if
}//end for
cout<<"The Original Message is : "<<ciphertext<<endl;//the cipher text
continue;
case 3:
break;
}//end switch
break;
}//end for
return 0;
}