-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVigenere Cipher.cpp
More file actions
80 lines (69 loc) · 2.22 KB
/
Vigenere Cipher.cpp
File metadata and controls
80 lines (69 loc) · 2.22 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
//============================================================================
// Name : Vigenère Cipher.cpp
// Author : SidPro
// Version : 1.0
// Description :
/*
polyalphabetic ciphers
is the Vigenère cipher. In this scheme, the set of related monoalphabetic substitution
rules consists of the 26 Caesar ciphers with shifts of 0 through 25. Each cipher is
denoted by a key letter, which is the ciphertext letter that substitutes for the plaintext
letter a.
Thus, the first letter of the key is added to the first letter of the plaintext, mod 26,
the second letters are added, and so on through the first m letters of the plaintext.
For the next m letters of the plaintext, the key letters are repeated. This process
continues until all of the plaintext sequence is encrypted. A general equation of the
encryption process is
C[i] = (p[i] + k[i mod m]) mod 26
Similarly, decryption is a generalization of above,
p[i] = (C[i] - k[i mod m]) mod 26
key : deceptive
key : deceptivedeceptivedeceptive
Message: wearediscoveredsaveyourself
Cipher : zicvtwqngrzgvtwavzhcqyglmgj
*/
//============================================================================
#include<iostream>
#include <cctype>
using namespace std;
string Vigenere_Cipher_encryption(string message,string key){
int n = message.length(),m = key.length();
int k=0;
string ency="";
for(int i=0;i<n;++i,++k){
if(message[i]==' '){
ency+=' ';
--k;
continue;
}
ency += 'a' + ( (message[i] -'a') + (key[(k%m)] - 'a') ) % 26;
}
return ency;
}
string Vigenere_Cipher_decryption(string message,string key){
int n = message.length(),m = key.length();
int k=0,temp;
string ency="";
for(int i=0;i<n;++i,++k){
if(message[i]==' '){
ency+=' ';
--k;
continue;
}
temp = (message[i] -'a') - (key[(k%m)] - 'a');
ency += 'a' + (temp<0?(temp+26):temp) % 26;
}
return ency;
}
int main(){
//the quick brown fox jumps over the lazy dog
string s;
string key="deceptive";
cout<<"Enter Message: ";
getline(cin,s);
string p = Vigenere_Cipher_encryption(s,key);
cout<<"Vigenere_Cipher_encryption :"<<p<<"\n";
cout<<"Vigenere_Cipher_decryption: "<<Vigenere_Cipher_decryption(p,key)<<"\n";
system("pause");
return 0;
}