-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxorcipher.cpp
More file actions
161 lines (124 loc) · 3.95 KB
/
xorcipher.cpp
File metadata and controls
161 lines (124 loc) · 3.95 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// CPP file of XORCipher
#include "xorcipher.h"
/*--------------------------Defination of XORCipher class--------------------------------*/
XORCipher::XORCipher(): key(" "), pathOfHexFile(" "), pathOfXORFile(" ") {}
XORCipher::XORCipher(string key, string pathOfHexFile, string pathOfXORFile)
{
this->key = key;
this->pathOfHexFile = pathOfHexFile;
this->pathOfXORFile = pathOfXORFile;
}
void XORCipher::setPathOfHexFile(string pathOfHexFile)
{
this->pathOfHexFile = pathOfHexFile;
}
void XORCipher::setPathOfXORFile(string pathOfXORFile)
{
this->pathOfXORFile = pathOfXORFile;
}
void XORCipher::setKey(string key)
{
this->key = key;
}
/*--------------------------Defination of Encryption class--------------------------------*/
Encryption::Encryption(string key, string pathOfHexFile, string pathOfXORFile): XORCipher(key, pathOfHexFile, pathOfXORFile) {}
bool Encryption::performXOR()
{
string hexData;
/* Storing hexadecimal values to string hexData */
fstream inputFile;
inputFile.open(pathOfHexFile, ios::in);
if(!inputFile.is_open())
{
system("cls");
system("Color 04");
cout<<"\n\n\t\t\t\tUnable to open a "<<pathOfHexFile<<" file :( "<<endl;
system("pause");
return false;
}else{
string data;
while(getline(inputFile, data))
{
hexData += data;
}
inputFile.close();
/* Performing XOR operation using key to string hexData */
string cipherText;
stringstream ss;
for (size_t i = 0; i < hexData.length(); ++i)
{
char encryptedChar;
encryptedChar = hexData[i] ^ key[i % key.length()];
ss << hex << setw(2) << setfill('0') << (int)encryptedChar;
}
ss >> cipherText;
/* Storing cipher text to file (path entered by the user) */
fstream outputFile;
outputFile.open(pathOfXORFile, ios::out);
if(!outputFile.is_open())
{
system("cls");
system("Color 04");
cout<<"\n\n\t\t\t\tUnable to open a "<<pathOfXORFile<<" file :( "<<endl;
system("pause");
return false;
}else{
outputFile << cipherText;
}
outputFile.close();
return true;
}
}
/*--------------------------Defination of Decryption class--------------------------------*/
Decryption::Decryption(string key, string pathOfHexFile, string pathOfXORFile): XORCipher(key, pathOfHexFile, pathOfXORFile){}
bool Decryption::performXOR()
{
/* First load and store (encrypted data) file in the string */
string encryptedData;
fstream inputFile;
inputFile.open(pathOfXORFile, ios::in);
if(!inputFile.is_open())
{
system("cls");
system("Color 04");
cout<<"\n\n\t\tUnable to open a "<<pathOfXORFile<<" file :( "<<endl;
system("pause");
return false;
}else{
string data;
while(getline(inputFile, data))
{
encryptedData += data;
}
inputFile.close();
/* Performing XOR operation using key to on encrypted data */
string decryptedData;
stringstream ss;
for (size_t i = 0; i < encryptedData.length(); i += 2)
{
string singleHexByte = encryptedData.substr(i, 2);
int encryptedChar;
ss << hex << singleHexByte;
ss >> encryptedChar;
char decryptedChar;
decryptedChar = encryptedChar ^ key[i / 2 % key.length()];
decryptedData += decryptedChar;
ss.clear();
}
/* Storing decrypted data to .hex file */
fstream outputFile;
outputFile.open(pathOfHexFile, ios::out);
if(!outputFile.is_open())
{
system("cls");
system("Color 04");
cout<<"\n\n\t\tUnable to open a "<<pathOfHexFile<<" file :( "<<endl;
system("pause");
return false;
}else{
outputFile << decryptedData;
}
outputFile.close();
return true;
}
}