-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryptDecrypt.cpp
More file actions
101 lines (81 loc) · 2.69 KB
/
encryptDecrypt.cpp
File metadata and controls
101 lines (81 loc) · 2.69 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
#include <iostream>
#include <string>
using namespace std;
class EncryptorDecryptor {
public:
virtual string process(const string& message) = 0;
};
class XORCipher : public EncryptorDecryptor {
private:
string key;
public:
XORCipher(const string& secretKey) {
key = secretKey;
}
string process(const string& message) override {
string processedMessage = message;
for (int i = 0; i < message.length(); ++i) {
processedMessage[i] ^= key[i % key.length()];
}
return processedMessage;
}
};
class CommonAlgorithm : public EncryptorDecryptor {
public:
string process(const string& message) override {
string processedMessage = message;
for (int i = 0; i < message.length(); ++i) {
processedMessage[i] += 1;
}
return processedMessage;
}
};
int main() {
string message;
string key;
char choice;
int ch=1;
while(ch!=0)
{
try {
cout << "Enter 'E' to encrypt or 'D' to decrypt a message: ";
cin >> choice;
cin.ignore(); // Ignore the newline character
if (choice != 'E' && choice != 'e' && choice != 'D' && choice != 'd') {
throw invalid_argument("Invalid choice. Please choose 'E' to encrypt or 'D' to decrypt.");
}
EncryptorDecryptor* processor = nullptr;
if (choice == 'E' || choice == 'e') {
cout << "Enter the message to encrypt: ";
getline(cin, message);
cout << "Enter the secret key (leave empty for common encryption): ";
getline(cin, key);
if (!key.empty()) {
processor = new XORCipher(key);
} else {
processor = new CommonAlgorithm();
}
string encrypted = processor->process(message);
cout << "Encrypted message: " << encrypted << endl;
} else {
cout << "Enter the message to decrypt: ";
getline(cin, message);
cout << "Enter the secret key (leave empty for common encryption): ";
getline(cin, key);
if (!key.empty()) {
processor = new XORCipher(key);
} else {
processor = new CommonAlgorithm();
}
string decrypted = processor->process(message);
cout << "Decrypted message: " << decrypted << endl;
}
delete processor;
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}
cout<<"\nPress 1 to Continue ; Press 0 to Exit\n"<<endl;
cin>>ch;
}
return 0;
}