-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shift.cpp
97 lines (91 loc) · 1.97 KB
/
Shift.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//Shift Cipher
//code by Bipin Timalsina
#include<iostream>
using namespace std;
class Shift{
public:
int key;
string plainText,cipherText;
string getPlainText(){
cout<<"Enter plain Text "<<endl;
cin>>plainText;
return plainText;
}
string getCipherText(){
cout<<"Enter Ciphertext "<<endl;
cin>>cipherText;
return cipherText;
}
int getKey(){
cout<<"Enter the Key "<<endl;
cin>>key;
return key;
}
//function to handle negative module since c++ cant do as we need
int mod(int a, int b) {
int c = a % b;
return (c < 0) ? c + b : c;
}
//functon for encryption
void encrypt(string msg,int k){
int len = msg.length();
char p[len],c[len];
for(int i = 0; i<len;i++){
//converting into lowercase and storing in char array
p[i]=tolower(msg[i]);
int index= int(p[i])-97;
int e= mod((index+key),26);
char ca= e+97;
c[i]=ca;
}
cout<<"The encrypted message is :"<<endl;
for(int i=0;i<len;i++){
c[i]=c[i]-32;
cout<<c[i];
}
}
//functon for decryption
void decrypt(string msg,int k){
int len = msg.length();
char p[len],c[len];
for(int i = 0; i<len;i++){
//converting into uppercase and storing in char array
p[i]=toupper(msg[i]);
int index= int(p[i])-65;
int e= mod((index-key),26);
char ca= e+65;
cout<<e<<"\t"<<ca;
c[i]=ca;
}
cout<<"The plaintext after decryption:"<<endl;
for(int i=0;i<len;i++){
c[i]=c[i]+32;
cout<<c[i];
}
}
};
int main(){
Shift s;
int k;
string text;
char choice;
char more;
//repeat till user want to encrypt/decrypt
do{
cout<<"Do you want to encrypt or decrypt?(press e or d)"<<endl;
cin>>choice;
k = s.getKey();
if(choice=='e')
{
text = s.getPlainText();
s.encrypt(text,k);
}
else{
text = s.getCipherText();
s.decrypt(text,k);
}
cout<<"\n \n Want to perform more encryption/decryption?(press y or n) \n";
cin>>more;
}while(more=='y');
return 0;
}