forked from rinovethamoses97/cryptogrpahy-lab-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVigenereCipher.cpp
59 lines (53 loc) · 1.24 KB
/
VigenereCipher.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
#include<bits/stdc++.h>
using namespace std;
string generateKey(string str, string key)
{
int x = str.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.size() == str.size())
break;
key.push_back(key[i]);
}
return key;
}
string cipherText(string str, string key)
{
string cipher_text;
for (int i = 0; i < str.size(); i++)
{
int x = (str[i]-97 + key[i]-97) %26;
x += 'a';
cipher_text.push_back(x);
}
return cipher_text;
}
string originalText(string cipher_text, string key)
{
string orig_text;
for (int i = 0 ; i < cipher_text.size(); i++)
{
int x = (cipher_text[i] - key[i] + 26) %26;
x += 'a';
orig_text.push_back(x);
}
return orig_text;
}
int main()
{
string str;
cout<<"enter the plain text";
cin>>str;
string keyword;
cout<<"Enter the keyword";
cin>>keyword;
string key = generateKey(str, keyword);
string cipher_text = cipherText(str, key);
cout << "Ciphertext : "
<< cipher_text << "\n";
cout << "Original/Decrypted Text : "
<< originalText(cipher_text, key);
return 0;
}