-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXOR.cpp
87 lines (72 loc) · 1.94 KB
/
XOR.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
81
82
83
84
85
86
87
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
void cipherEncryption(){
string msg;
cout << "Enter message: ";
getline(cin, msg);
string key;
cout << "Enter key: ";
getline(cin, key);
string encrypHexa = "";
int keyItr = 0;
stringstream res;
for (int i = 0; i < msg.length(); i++){
int temp = msg[i] ^ key[keyItr];
res << hex << setfill('0') << std::setw(2) << (int)temp;
keyItr++;
if (keyItr >= key.length()){
// once all of key's letters are used, repeat the key
keyItr = 0;
}
}
res >> encrypHexa;
cout << "Encrypted Text: " << encrypHexa;
}
void cipherDecryption(){
string msg;
cout << "Enter message: ";
getline(cin, msg);
string key;
cout << "Enter key: ";
getline(cin, key);
string hexToUni = "";
for (int i = 0; i < msg.length()-1; i+=2){
// splitting hex into a pair of two
string output = msg.substr(i, 2);
// converting hex into unicode
int decimal = strtol(output.c_str(), NULL, 16);
hexToUni += (char)decimal;
}
string decrypText = "";
int keyItr = 0;
for (int i = 0; i < hexToUni.length(); i++){
int temp = hexToUni[i] ^ key[keyItr];
decrypText += (char)temp;
keyItr++;
if (keyItr >= key.length()){
// once all of key's letters are used, repeat the key
keyItr = 0;
}
}
cout << "Decrypted Text: " << decrypText;
}
int main()
{
cout << "1. Encryption\n2. Decryption\nChoose(1,2): ";
int choice;
cin >> choice;
cin.ignore();
if (choice == 1){
cout << endl << "---Encryption---" << endl;
cipherEncryption();
} else if (choice == 2){
cout << endl << "---Decryption---" << endl;
cipherDecryption();
} else {
cout << endl << "Wrong choice" << endl;
}
return 0;
}