-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.cpp
More file actions
112 lines (93 loc) · 3.21 KB
/
Copy pathencryption.cpp
File metadata and controls
112 lines (93 loc) · 3.21 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string passkey = ""; // Global passkey variable
// Function prototypes
string encryptVigenere(const string& text, const string& key);
string decryptVigenere(const string& text, const string& key);
void handleCommand(const string& commandLine);
int main() {
string commandLine;
// Continuously read from stdin
while (getline(cin, commandLine))
{
handleCommand(commandLine);
if (commandLine == "QUIT")
{
break;
}
}
return 0;
}
// Encrypt using Vigenère cipher
string encryptVigenere(const string& text, const string& key) {
string result = "";
int keyLen = key.length();
for (size_t i = 0; i < text.length(); ++i) {
// Only encrypt alphabetic characters
if (isalpha(text[i]))
{
char base = isupper(text[i]) ? 'A' : 'a';
result += (text[i] - base + (toupper(key[i % keyLen]) - 'A')) % 26 + base;
}
else
{
result += text[i]; // Non-alphabetic characters remain unchanged
}
}
return result;
}
// Decrypt using Vigenère cipher
string decryptVigenere(const string& text, const string& key) {
string result = "";
int keyLen = key.length();
for (size_t i = 0; i < text.length(); ++i)
{
// Only decrypt alphabetic characters
if (isalpha(text[i])) {
char base = isupper(text[i]) ? 'A' : 'a';
result += (text[i] - base - (toupper(key[i % keyLen]) - 'A') + 26) % 26 + base;
}
else
{
result += text[i]; // Non-alphabetic characters remain unchanged
}
}
return result;
}
// Handle a command by parsing and executing the appropriate function
void handleCommand(const string& commandLine)
{
// Find the first space to separate command from argument
size_t spaceIndex = commandLine.find(' ');
string command = commandLine.substr(0, spaceIndex);
string argument = (spaceIndex == string::npos) ? "" : commandLine.substr(spaceIndex + 1);
if (command == "PASSKEY") {
passkey = argument; // Set the passkey
cout << "RESULT Passkey set successfully.\n";
} else if (command == "ENCRYPT")
{
if (passkey.empty()) { // validate existing passkey
cout << "ERROR No passkey set.\n";
} else
{
string encryptedText = encryptVigenere(argument, passkey); // encrypt message and send result to stdin
cout << "RESULT " << encryptedText << "\n";
}
} else if (command == "DECRYPT")
{
if (passkey.empty()) {// validate existing passkey
cout << "ERROR No passkey set.\n";
} else {
string decryptedText = decryptVigenere(argument, passkey); // decrypt message and send result to stdin
cout << "RESULT " << decryptedText << "\n";
}
} else if (command == "QUIT") {
cout << "RESULT Exiting program.\n";
exit(0); // Exit the program
} else {
// Handle missinput
cout << "ERROR Unknown command.\n";
}
}