Submitted by: Noor Fatima
Submitted to: Dr. Qaiser Javaid
Reg No. 4678-FOC/BSCS/F22
Course: Information Security
Assignment # 3
Monoalphabetic Cipher
...ipher\MonoAlphabetic Cipher\MonoAlphabetic Cipher.cpp 1
1 #include <iostream>
2 #include <unordered_map>
3 #include <string>
4 using namespace std;
5
6 // Function to encrypt the plaintext using the given key
7 string encrypt(string plaintext, unordered_map<char, char>& keyMap) {
8 string ciphertext = "";
9 for (char c : plaintext) {
10 c = toupper(c);
11 if (keyMap.find(c) != keyMap.end()) {
12 ciphertext += keyMap[c];
13 }
14 else {
15 ciphertext += c;
16 }
17 }
18 return ciphertext;
19 }
20
21 // Function to decrypt the ciphertext using the given key
22 string decrypt(string ciphertext, unordered_map<char, char>& reverseKeyMap)
{
23 string plaintext = "";
24 for (char c : ciphertext) {
25 c = toupper(c);
26 if (reverseKeyMap.find(c) != reverseKeyMap.end()) {
27 plaintext += reverseKeyMap[c];
28 }
29 else {
30 plaintext += c;
31 }
32 }
33 return plaintext;
34 }
35
36 // Helper function to create encryption and decryption maps
37 void createMaps(string key, unordered_map<char, char>& keyMap,
unordered_map<char, char>& reverseKeyMap) {
38 string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
39 for (int i = 0; i < 26; i++) {
40 keyMap[alphabet[i]] = key[i];
41 reverseKeyMap[key[i]] = alphabet[i];
42 }
43 }
44
45 int main() {
46 string key = "QWERTYUIOPASDFGHJKLZXCVBNM";
47 unordered_map<char, char> keyMap;
...ipher\MonoAlphabetic Cipher\MonoAlphabetic Cipher.cpp 2
48 unordered_map<char, char> reverseKeyMap;
49
50 createMaps(key, keyMap, reverseKeyMap);
51
52 cout << "==========================================================" <<
endl;
53 cout << "**************** Monoalphabetic Cipher ****************" <<
endl;
54 cout << "==========================================================\n
\n";
55
56 char option;
57 cout << "Select an option:\n";
58 cout << "a) Encrypt text\n";
59 cout << "b) Decrypt text\n";
60 cout << "Enter your choice (a/b): ";
61 cin >> option;
62 cin.ignore();
63
64 if (option == 'a') {
65 string plaintext;
66 cout << "Enter the plaintext: ";
67 getline(cin, plaintext);
68 string ciphertext = encrypt(plaintext, keyMap);
69 cout << "Encrypted text: " << ciphertext << endl;
70 }
71 else if (option == 'b') {
72 string ciphertext;
73 cout << "Enter the ciphertext: ";
74 getline(cin, ciphertext);
75 string decryptedText = decrypt(ciphertext, reverseKeyMap);
76 cout << "Decrypted text: " << decryptedText << endl;
77 }
78 else {
79 cout << "Invalid option! Please enter 'a' or 'b'." << endl;
80 }
81 return 0;
82 }
83
Output: