-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bob.c
246 lines (193 loc) · 5 KB
/
Bob.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
245
246
#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,int u){
// 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<u)
{
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 main(){
//rsa keys pre-computed for simplification so in the following case,
// for A and B communication Private Key d2 would be 3447, same p,q,n as the last pair. and public key k1:e1 would be 7
int p = 59;
int q = 53;
int n = p*q;
int phi = (p-1)*(q-1);
//4th key-pair for A and B communication
int phi4= 120;
int p4=11;
int q4=13;
int n4=p4*q4;
int e4=7;//public
int d4=103;//private
//1st B own secret key
int e1=7;
int ks0=8;// a constant value
int d2= (1 + (ks0*phi))/e1; //
// printf("%d the 2nd pri k is\n",ks2);
/*
while (e1 < phi)
{
// e must be co-prime to phi and
// smaller than phi.
if (gcd(e1, phi)==1)
break;
else
e1++;
} */
// int msg=9;
// int c = powMod(msg, e, n); // encrypted equation
// int mm = powMod(c, d, n); // decrypted
// printf("the enc is %d\n",c);
// printf("the dnc is %d\n",mm);
//handshake between MB and B----------------------
// keys
int k0=11;
char m1[256]="attack";
char m2[]="benign";
int kr=9;
char ms1[256];
// communicate for test
//message into ascii for encryption
/*
for (int j=0;j<strlen(m1);j++){
int tmp1=(int) m1[j]; // convert to ascii
printf("the %c is %d ",m1[j],tmp1);
int tmp3=powMod(tmp1, e4, n4); // 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<100)
{
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}
}*/
// printf(" d is %d",d);
LetterAsc(e4,n4,m1,strlen(m1),ms1,100);
printf("%s is the message to be sent to A\n ",ms1);
char ms3[256];
LetterAsc(e4,n4,m2,strlen(m2),ms3,100);
printf("%s is the message to be sent to A its original is %s\n ",ms3,m2);
char ms2[256];
LetterAsc(e1,n,m1,strlen(m1),ms2,1000);
char ms4[256];
LetterAsc(e1,n,m2,strlen(m2),ms4,1000);
//send to MB and A
int fo;
fo=socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in oaddr;
oaddr.sin_family = AF_INET;
oaddr.sin_port = htons(9052);
oaddr.sin_addr.s_addr = INADDR_ANY;
bind(fo,(struct sockaddr *)&oaddr,sizeof(oaddr));
listen(fo,50);
int clientSocket;
clientSocket=accept(fo,NULL,NULL);
// sleep(3);
char hs1[256];
char hello[256]="hello";
// printf("get the %s\n",hs1);
// sleep(4);
send(clientSocket,hello,sizeof(hello),0);
// close(fo);
// clientSocket=accept(fo,NULL,NULL);
int r =recv(clientSocket,&hs1,sizeof(hs1),0);
printf("get the %s\n",hs1);
char cm[256];
CRSA(e1,n,hs1,strlen(hs1),cm);
printf("the cm is %s\n",cm);
send(clientSocket,cm,sizeof(cm),0);//send the double twice RSA communitative
send(clientSocket,ms1,sizeof(ms1),0);// Message for A
send(clientSocket,ms2,sizeof(ms2),0);//token to MB
send(clientSocket,ms3,sizeof(ms3),0);//another one for A
send(clientSocket,ms4,sizeof(ms4),0);//token to MB
// printf("%d \n",r);
close(fo);
return 0;
}