-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
307 lines (265 loc) · 6.69 KB
/
client.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <bits/stdc++.h>
#include "client.h"
#include "dns_message.h"
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <ctime>
#ifdef __WIN32__
#include <winsock2.h>
#else
# include <sys/socket.h>
#endif
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include<iostream>
#include<fstream>
#include<cstring>
#include<sstream>
#include <typeinfo>
#include "key_expand.h"
#include "encoding.h"
#include "decoding.h"
#include <unistd.h>
#define PORT 8089
typedef int SOCKET;
using namespace std;
void aes_enc() {
int extendedlength=0;
string myText;
ifstream File;
string filepath = "encryption.aes";
File.open(filepath.c_str(), std::ifstream::out | std::ifstream::trunc );
if (!File.is_open() || File.fail())
{
File.close();
printf("\nError : failed to erase file content !");
}
File.close();
fstream newfile;
newfile.open("input.txt",ios::in);
if (newfile.is_open()){
string tp;
while(getline(newfile, tp)){
int messlength=tp.length();
int extendedlength;
if((messlength%16)!=0)
{
extendedlength=messlength+(16-(messlength%16));
}
else
{
extendedlength=messlength;
}
unsigned char* encryptedtext=new unsigned char[extendedlength];
for(int i=0;i<extendedlength;i++)
{
if(i<messlength)
encryptedtext[i]=tp[i];
else
encryptedtext[i]=0;
}
string k;
ifstream infile;
infile.open("key.txt");
if (infile.is_open())
{
getline(infile, k);
infile.close();
}
istringstream tempkey(k);
unsigned char key[16];
unsigned int x;
for(int i=0;i<16;i++)
{
tempkey>>hex>>x;
key[i] = x;
}
unsigned char extendedkeys[176];
Key_extenxion(key,extendedkeys);
for(int i=0;i<extendedlength;i+=16)
{
unsigned char* temp=new unsigned char[16];
for(int j=0;j<16;j++)
{
temp[j]=encryptedtext[i+j];
}
encryption(temp , extendedkeys);
for(int j=0;j<16;j++)
{
encryptedtext[i+j]=temp[j];
}
}
ofstream fout;
ifstream fin;
fin.open("encryption.aes");
fout.open ("encryption.aes",ios::app);
if(fin.is_open())
fout<<encryptedtext<<"\n";
fin.close();
fout.close();
}
newfile.close();
}
}
void aes_dec() {
int extendedlength=0;
string myText;
string tp;
ifstream File;
string filepath = "outputtext.txt";
File.open(filepath.c_str(), std::ifstream::out | std::ifstream::trunc );
if (!File.is_open() || File.fail())
{
File.close();
printf("\nError : failed to erase file content !");
}
File.close();
ifstream MyReadFile;
MyReadFile.open("encryption.aes", ios::in | ios::binary);
if(MyReadFile.is_open())
{
while( getline (MyReadFile, myText) )
{
cout.flush();
char * x;
x=&myText[0];
int messlength=strlen(x);
char * msg = new char[myText.size()+1];
strcpy(msg, myText.c_str());
int n = strlen((const char*)msg);
unsigned char * decryptedtext = new unsigned char[n];
for (int i = 0; i < n; i++) {
decryptedtext[i] = (unsigned char)msg[i];
}
string k;
ifstream infile;
infile.open("key.txt");
if (infile.is_open())
{
getline(infile, k);
infile.close();
}
else cout << "Unable to open file";
istringstream tempkey(k);
unsigned char key[16];
unsigned int x1;
for(int i=0;i<16;i++)
{
tempkey>>hex>>x1;
key[i] = x1;
}
unsigned char extendedkeys[176];
Key_extenxion(key,extendedkeys);
for (int i = 0; i < messlength; i += 16)
{
unsigned char * temp=new unsigned char[16];
for(int j=0;j<16;j++)
temp[j]=decryptedtext[i+j];
decryption(temp , extendedkeys);
for(int j=0;j<16;j++)
decryptedtext[i+j]=temp[j];
}
for(int i=0;i<messlength;i++)
{
if(decryptedtext[i]==0 && decryptedtext[i-1]==0 )
break;
}
cout<<endl;
ofstream fout;
ifstream fin;
fin.open("outputtext.txt");
fout.open ("outputtext.txt",ios::app);
if(fin.is_open())
fout<<decryptedtext<<"\n";
fin.close();
fout.close();
}
}
else
{
cout<<"Can not open input file\n ";
}
MyReadFile.close();
}
void ChangetoDnsNameFormat(unsigned char* header, unsigned char* domain_name)
{
int lock = 0, i;
strcat((char*)domain_name, ".");
for(i = 0; i < (int)strlen((char*)domain_name); i++)
{
if(domain_name[i] == '.')
{
*header++ = (i - lock) + '0';
for(; lock < i; lock++)
{
*header++ = domain_name[lock];
}
lock++;
}
}
*header++ = '\0';
}
int format_query(unsigned char *domain)
{
srand(time(NULL));
unsigned char buf[65536];
DNS_HEADER *header = NULL;
header = (DNS_HEADER*)&buf;
header->id = (unsigned short)rand();
header->rd = 1;
header->tc = 0;
header->aa = 0;
header->opcode = 0;
header->qr = 0;
header->rcode = 0;
// header->cd = 0;
// header->ad = 0;
header->z = 0;
header->ra = 0;
header->q_count = htons(1);
header->ans_count = 0;
header->ns_count = 0;
header->rr_count = 0;
QUESTION *ques = NULL;
unsigned char *name = (unsigned char*)&buf[sizeof(DNS_HEADER)];
ChangetoDnsNameFormat(name, domain);
string query(reinterpret_cast<char*>(name));
const char *path = "input.txt";
ofstream out(path);
out << query;
out.close();
aes_enc();
ques = (QUESTION*)&buf[sizeof(DNS_HEADER) + (strlen((const char*)name) + 1)];
ques->qtype = htons(1);
// ques->qclass = htons(1);
int s = socket(AF_INET,SOCK_STREAM,0);
sockaddr_in dest;
dest.sin_family = AF_INET;
dest.sin_port = htons(PORT);
dest.sin_addr.s_addr = inet_addr("127.0.0.1");
if(inet_pton(AF_INET, "127.0.0.1", &dest.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return 0;
}
if (connect(s, (struct sockaddr *)&dest, sizeof(dest)) < 0)
{
printf("\nConnection Failed \n");
return 0;
}
printf("Connection successful");
send(s, (char*)buf, sizeof(struct DNS_HEADER) + (strlen((const char*)name) + 1) + sizeof(struct QUESTION), 0);
return 0;
}
int main() {
string hostname = "www.google.com";
unsigned char host[100];
for(int i = 0; i < hostname.length(); i++) {
host[i] = hostname[i];
}
format_query(host);
}