-
Notifications
You must be signed in to change notification settings - Fork 0
/
vigenere.cpp
50 lines (40 loc) · 1.04 KB
/
vigenere.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
#include <iostream>
#include <string>
std::string encrypt(std::string plaintext, std::string keyWord)
{
std::string encrypted = "";
int shift { 0 };
for (unsigned int i { 0 }, k { 0 }; i < plaintext.length(); i++, k++)
{
// Skipping spaces
while (plaintext[i] == char(32)) i++;
// Going to the start of the key when it ends
if (k >= keyWord.length())
k = 0;
// Converting key into shift numbers
if (isupper(keyWord[k]))
shift = int(keyWord[k]) - 64;
else
shift = int(keyWord[k]) - 96;
// Encrypting plaintext
if (isupper(plaintext[i]))
encrypted += char(int(plaintext[i] + shift - 65) % 26 + 65);
else
encrypted += char(int(plaintext[i] + shift - 97) % 26 + 97);
}
return encrypted;
}
int main()
{
// Enter text
std::string text = "";
std::cout << "Enter the text to be encrypted: ";
std::getline(std::cin, text);
// Enter key
std::string key = "";
std::cout << "Enter the encryption key: ";
std::cin >> key;
// Display encrypted text
std::cout << "Encrypted text: " << encrypt(text, key);
return 0;
}