-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleApplication4.cpp
87 lines (71 loc) · 1.64 KB
/
ConsoleApplication4.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>
using namespace std;
void encrypt()
{
string sec_key;
cout << "enter the secret key" << endl;
cin >> sec_key;
string msg;
cout << "enter the message to encrypt it" << endl;
cin >> msg;
int num_in_dec;
int z = 0;
string encrypt_as_dec = "";
string encrypt_as_text = "";
string encrypt_as_hex = "";
for (int i = 0; i < msg.length(); i++)
{
num_in_dec = int(msg[i]) ^ int(sec_key[z]);
encrypt_as_dec += to_string(num_in_dec);
encrypt_as_text += char(num_in_dec);
ostringstream num_in_hex;
num_in_hex<<setfill('0')<<setw(2)<<hex<<num_in_dec;
encrypt_as_hex += num_in_hex.str();
}
cout << "message as decimal: " << encrypt_as_dec << endl;
cout << "message as text: " << encrypt_as_text << endl;
cout << "message as hex: " << encrypt_as_hex << endl;
}
void decrypt()
{
string sec_key2;
cout << "enter the secret key" << endl;
cin >> sec_key2;
string msg2;
cout << "enter hex to decipher to text" << endl;
cin >> msg2;
int conv_to_dec, num_in_dec_after_xor;
int w = 0;
string decrypt = "";
for (int q = 0; q < msg2.length(); q += 2)
{
string part = msg2.substr(q, 2);
int ch = stoul(part, nullptr, 16);
num_in_dec_after_xor = (int(ch) ^ int(sec_key2[w]));
decrypt += char(num_in_dec_after_xor);
}
cout << decrypt << endl;
}
int main()
{
int userinput;
cout<<"what do u like to do ?"<<endl;
cout<<"1-Cipher a message"<<endl;
cout<<"2-Decipher a message"<<endl;
cout<<"3-End"<<endl;
cin>>userinput;
if(userinput == 1)
{
encrypt();
}
else if(userinput == 2)
{
decrypt();
}
else if(userinput == 3)
{
cout << "end" << endl;
}
}