-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVigenere cipher.cpp
57 lines (50 loc) · 963 Bytes
/
Vigenere 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
#include <bits/stdc++.h>
using namespace std;
string cipher(string s, string key){
string ans = "";
int j=0;
for(int i = 0 ; i < s.size() ; i++){
if(i<key.size()){
char tmp = ((s[i]+key[i])%26);
tmp+='A';
ans+=tmp;
}
else{
char tmp = ((s[i]+s[j])%26);
tmp+='A';
ans+=tmp;
j+=1;
}
}
return ans;
}
string decipher(string s , string key){
string ans="";
int j=0;
for(int i = 0 ; i < s.size() ; i++){
if(i<key.size()){
char tmp = ((s[i]-key[i]+26)%26);
tmp+='A';
ans+=tmp;
}
else{
char tmp = ((s[i]-ans[j]+26)%26);
tmp+='A';
ans+=tmp;
j+=1;
}
}
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << "Input String and Key: \n";
string s,key;
cin >> s >> key;
string ciphertxt = cipher(s,key);
cout << (ciphertxt) << endl;
string dectxt = decipher(ciphertxt,key);
cout << dectxt;
return 0;
}