-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceasar_cipher.cpp
63 lines (61 loc) · 1.05 KB
/
ceasar_cipher.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
#include<bits/stdc++.h>
using namespace std;
class ceasar
{
string ip;
int key;
string cipher;
public:
void encrypt();
void decrypt();
};
void ceasar :: encrypt()
{
cout<<"\n\tEnter Message:";
cin>>ip;
cout<<"\tEnter Key:";
cin>>key;
key %= 26;
cipher.clear();
for(int i=0;i<ip.size();i++)
{
int tmp = (ip[i] + key);
cipher += (tmp > 'z' ? (tmp - 26):tmp);
}
cout<<"\n\tEncrypted Message:"<<cipher<<endl;
}
void ceasar :: decrypt()
{
cout<<"\n\tEnter Key:";
cin>>key;
key = key%26;
ip.clear();
for(int i=0;i<cipher.size();i++)
{
int tmp = (cipher[i] - key);
ip += (tmp < 'a' ? (tmp + 26):tmp);
}
cout<<"\n\tDecrypted Message:"<<ip<<endl;
}
int main()
{
cout<<"\t------Ceasar Cipher--------";
int opt;
ceasar c;
while(1)
{
cout<<"\n\tSelect the following option\n\t1.Encrypt a message\n\t2.Decrypt the message\n\t3.Exit\n\tOption:";
cin>>opt;
if(opt>2)
break;
switch(opt)
{
case 1:
c.encrypt();
break;
case 2:
c.decrypt();
break;
}
}
}