-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathVigenere_Cipher.cpp
80 lines (63 loc) · 1.61 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*Vigenere Cipher Algorithm is a famous cryptographic algorithm.
Vigenère cipher is a method of encrypting alphabetic text by using a series of interwoven Caesar ciphers*/
#include <bits/stdc++.h>
using namespace std;
string encryption(string message, int x, string key) {
string cipher;
for (int i = 0; i < message.size(); ++i) {
int t=(message[i] + key[i%x])%26;
cipher+=('A'+t);
}
return cipher;
}
string decryption(string cipher, int x, string key) {
string message;
for (int i = 0; i < cipher.size(); ++i) {
int t=(cipher[i] - key[i%x] +26)%26;
message+=('A'+t);
}
return message;
}
int main() {
//use only uppercase character
int choice;
cout << "Choose 1 if you want to ENCRYPT or 2 if you want to DECRYPT: ";
cin >> choice;
if(choice == 1) {
string message;
cout << "Enter a plaintext message: ";
cin >> message;
string key;
cout << "Enter a keyword: ";
cin >> key;
cout << "\nEncrypted message: " << encryption(message, key.size(), key);
cout << endl;
}
else if(choice == 2) {
string ciphertext;
cout << "Enter a ciphertext: ";
cin >> ciphertext;
string key;
cout << "Enter a keyword: ";
cin >> key;
cout << "\nDecrypted message: " << decryption(ciphertext, key.size(), key);
cout << endl;
}
else {
cout << "\nInvalid choice." << endl;
}
return 0;
}
/*
Sample I/O:
1)
Choose 1 if you want to ENCRYPT or 2 if you want to DECRYPT: 1
Enter a plaintext message: NEOALGO
Enter a keyword: MARS
Encrypted message: ZEFSXGF
2)
Choose 1 if you want to ENCRYPT or 2 if you want to DECRYPT: 2
Enter a ciphertext: ZEFSXGF
Enter a keyword: MARS
Decrypted message: NEOALGO
*/