-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubstitutionCipher.cpp
More file actions
53 lines (53 loc) · 1.24 KB
/
Copy pathSubstitutionCipher.cpp
File metadata and controls
53 lines (53 loc) · 1.24 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
#include<iostream>
#include<vector>
#include<set>
#include <time.h>
using namespace std;
class SubstitutionCipher{
vector<int> key;
vector<int> GenerateKey(){
vector<int> key;
set<int> st;
while(key.size()<26){
int p = rand()%26;
if(st.find(p)==st.end()){
st.insert(p);
key.push_back(p);
}
}
return key;
}
public:
string Encrypt(string message){
key = GenerateKey();
string result = "";
for(auto it: message){
int p = int(it)-int('a');
p = key[p];
result+=char(p+int('a'));
}
return result;
}
string Decrypt(string Cypher){
string result = "";
vector<int> reverseKey(26,0);
for(int i=0;i<26;i++){
reverseKey[key[i]] = i;
}
for(auto it: Cypher){
result.push_back(char(reverseKey[int(it)-'a']+int('a')));
}
return result;
}
};
int main(){
SubstitutionCipher SC;
srand(time(0));
cout<<"Enter message : ";
string s;
cin>>s;
string c5 = SC.Encrypt(s);
cout<<"Substitution Cypher : "<<c5<<endl;
cout<<"Decrypt :"<<SC.Decrypt(c5);
return 0;
}