-
Notifications
You must be signed in to change notification settings - Fork 0
/
MB.c
244 lines (183 loc) · 4.6 KB
/
MB.c
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
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/epoll.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>
#include <time.h>
#include <math.h>
int gcd(int a, int h)
{
int temp;
while (1)
{
temp = a%h;
if (temp == 0)
return h;
a = h;
h = temp;
}
}
int powMod(int a, int b, int n) {
long long x = 1, y = a;
while (b > 0) {
if (b % 2 == 1)
x = (x * y) % n;
y = (y * y) % n; // Squaring the base
b /= 2;
}
return x % n;
}
int LetterAsc(int e, int n,char *m1,int l,char *ms1){
// char ms1[256];
for (int j=0;j<l;j++){
int tmp1=(int) m1[j]; // convert to ascii
printf("the %c is %d ",m1[j],tmp1);
int tmp3=powMod(tmp1, e, n); // encyption with rsa
// convert the int to char and concatenate again
printf(" enc is %d\n",tmp3);
char tmp2[10];
sprintf(tmp2,"%d",tmp3); // int to char
if(tmp3<1000)
{
if(j==0){
strcpy(ms1,"0");
}else{
strcat(ms1,"0");}
strcat(ms1,tmp2);
}
else{
if(j==0){
strcpy(ms1,tmp2);
}else{
strcat(ms1,tmp2);}} // concat}
}
}
int CRSA(int e,int n,char *m1,int l,char *ms1){
for(int i=0;i<l;i+=4){
int t=((int)m1[i]-48)*1000+((int)m1[i+1]-48)*100+((int)m1[i+2]-48)*10+((int)m1[i+3]-48);
printf("original is %d ",t);
int tmp3=powMod(t, e, n);
printf(" enc is %d\n",tmp3);
char tmp2[10];
sprintf(tmp2,"%d",tmp3); // int to char
if(tmp3<1000)
{
if(i==0){
strcpy(ms1,"0");
}else{
strcat(ms1,"0");}
strcat(ms1,tmp2);
}
else{
if(i==0){//first time
strcpy(ms1,tmp2);
}else{
strcat(ms1,tmp2);}} // concat}
}//for
return 0;
}
int IntiB(){
int fd;//handle for Bob and MidB
// socket initialize
fd=socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(9052);
saddr.sin_addr.s_addr = INADDR_ANY;
int cs=connect(fd,(struct sockaddr *)& saddr,sizeof( saddr));
return fd;
}
int YMHandShake(int fd,char *respond, int s )
{
//for handshake d would be 2011; e would 3 n would be 3127;
int p = 59;
int q = 53;
int n = p*q;
int phi = (p-1)*(q-1);
int e2 = 3; // Pub Key K2 for handshake with MB
int k = 2; // A constant value
int d2 = (1 + (k*phi))/e2; // Private Key for handshake with MB Keep Secrete
//handshake keyword rules
char result[256]="attack";
char rule2[]="luckily";
char rule1[256];
LetterAsc(e2,n,result,strlen(result),rule1);
// printf("the message is %s\n",rule1);
// sleep(5);
// recv(fd, respond,sizeof( respond),0);
char first[256];
recv(fd,first,sizeof(first),0);
printf("the shake hand %s\n",first);
printf("sending the 1st enc %s\n",rule1);
int r=send(fd,rule1,sizeof(rule1),0);
recv(fd,respond,s,0);
return 0;
}
int MidBoxForward(char *m,int l){
int hs;//as server ID
//another connect between MidB and destination Alice
hs=socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in hsa;
hsa.sin_family = AF_INET;
hsa.sin_port = htons(9053);
hsa.sin_addr.s_addr = INADDR_ANY;
//
bind(hs,(struct sockaddr *)&hsa,sizeof(hsa));
listen(hs,3);
int alice;
alice=accept(hs,NULL,NULL);
send(alice,m,l,0);
close(hs);
return 0;
}
int main(){
// socket b
int fd=IntiB();
//preset encrypted rule for simple test
// char r1[]="271652352327169292386";
//
int p = 59;
int q = 53;
int n = p*q;
int phi = (p-1)*(q-1);
int e2 = 3; // Pub Key K2 for handshake with MB
char respond[256];
int s=sizeof(respond);
YMHandShake(fd,respond,s);
printf("%s got \n", respond);//got the rule
char m1a[256];
char m1t[256];
char m2a[256];
char m2t[256];
recv(fd,&m1a,sizeof(m1a),0);//original
recv(fd,&m1t,sizeof(m1t),0);//token
recv(fd,&m2a,sizeof(m2a),0);
recv(fd,&m2t,sizeof(m2t),0);//token
char c1t[256];
char c2t[256];
CRSA(e2,n,m1t,strlen(m1t),c1t);
CRSA(e2,n,m2t,strlen(m2t),c2t);
printf(" 1st cmr is %s\n",m1t);
printf(" the token is %s\n",c1t);
if(strcmp(c1t, respond)==0)
printf(" catch a malicious the message is blocked \n");
else
printf(" pass to A\n");
// MidBoxForward(m2a,sizeof(m2a));
if(strcmp(c2t, respond)==0)
printf(" catch a malicious \n");
else{
printf(" pass to A the enc mes is %s\n",m2a);
MidBoxForward(m2a,sizeof(m2a));
}
// int r=send(fd,rule1,sizeof(rule1),0);
// printf("%d \n",r);
close(fd);
return 0;
}