-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonoalphabetic cipher.cpp
More file actions
108 lines (87 loc) · 2.58 KB
/
monoalphabetic cipher.cpp
File metadata and controls
108 lines (87 loc) · 2.58 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<bits/stdc++.h>
using namespace std;
string sets="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'0''1''2''3''4''5''6''7''8''9' ";
string encryption(string plaintext,string key)
{
string encrypt_text="";
for ( int i=0;i<plaintext.length();i++)
{
//alnum check wheather the character is alphanumeric or not.If the character is alphanumeric
// then that character is encrypted otherwise it remain same throughout the enryption process
if(isalnum(plaintext[i]))
{
for(int j=0;j<sets.length();j++)
{
if(plaintext.at(i)==sets.at(j))
{
encrypt_text+=key.at(j);
break;
}
}
}
else
encrypt_text+=char(plaintext[i]);
}
return encrypt_text;
}
void decryption(string ciphertext,string key)
{
string decrypt_text="";
for (int i=0;i<ciphertext.length();i++)
{
//alnum check wheather the character is alphanumeric or not.If the character is alphanumeric
// then that character is decrypted otherwise it remain same throughout the deryption process
if(isalnum(ciphertext[i]))
{
for(int j=0;j<key.length();j++)
{
if(ciphertext.at(i)==key.at(j))
{
decrypt_text+=sets.at(j);
break;
}
}
}
else
decrypt_text+=char(ciphertext[i]);
}
cout<<"After decryption cipher text becomes:"<<decrypt_text<<endl;
}
int main()
{
string message ,cipher_text;
int choice;
string key="PQRSTUVWXYZABCDEFGHIJKLMNOpqrstuvwxyzabcdefghijklmno'5''6''7''8''9''0''1''2''3''4' ";
cout<<"Enter the message:"<<endl;
getline(cin,message);
//for display the process of interchanging of characters
cout<<"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0 1 2 3 4 5 6 7 8 9"<<endl;
cout<<"|||||||||||||||||||||||||||||||||||||||||||||||||||| | | | | | | | | | |"<<endl;
cout<<key<<endl;
while(1)
{
cout<<"Enter your choice:"<<endl;
cout<<"1 for encryption"<<endl<<"2 for decryption"<<endl<<"3 for exit"<<endl;
cin>>choice;
switch(choice){
case 1:
cout<<"Encryption Process Begin"<<endl;
cout<<"---------------------------------------------"<<endl;
cipher_text=encryption(message,key);
cout<<"After encryption process ,plaintext becomes:"<<cipher_text<<endl;
break;
case 2:
cout<<"Decryption Process Begin"<<endl;
cout<<"---------------------------------------------"<<endl;
cipher_text=encryption(message,key);
decryption(cipher_text,key);
break;
case 3:
exit(0);
break;
default:
cout<<"Invalid entry!!"<<endl;
}
}
return 0;
}