forked from ankitam2030/C-files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raj Tiwari
288 lines (246 loc) · 5.84 KB
/
raj Tiwari
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
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
#define M 4 // Size of checksum
#define ONES ((1 << M) - 1) // 111... upto size of checksum
int checksumCalc(int data[], int len, int checksum)
{
int sum = 0;
// Calculate the sum of the data
for(int i = 0; i < len; i++)
sum += data[i];
sum += checksum;
// If sum bigger than size of checksum, Wrap the sum
if(ceil(log2(sum)) > M)
{
int wrap = sum >> M;
sum = (sum % (wrap << M)) + wrap;
}
int onescomp = ONES ^ sum; // Bitwise XOR of Sum with 1111...
return onescomp;
}
int main()
{
int len, data[MAXSIZE];
printf("====================19BCE0565====================\n\n\n");
printf("Enter number of data being sent: ");
scanf("%d", &len);
printf("Enter data between 0 and %d: ", ONES);
for(int i = 0; i < len; i++)
scanf("%d", &data[i]);
int checksum = checksumCalc(data, len, 0);
printf("=======Sender=======\n");
printf("Checksum is: %d\nTransmitting...\n\n", checksum);
// checksum -= 2; // Introducing Error
int reject = checksumCalc(data, len, checksum);
printf("======Receiver======\n");
if(reject)
printf("Error in transmission. Rejected!\n\n");
else
printf("Data received with no errors!\n\n");
}
63 distanceVector.c
@@ -0,0 +1,63 @@
#include <stdio.h>
struct Router
{
unsigned dist[20];
unsigned from[20];
} route[10];
void startMessage()
{
printf("=====================================\n");
printf(" 19BCE0565\n");
printf("=====================================\n\n");
}
int main()
{
int distMatrix[20][20];
int numOfNodes, i, j, k, count = 0;
startMessage();
printf("\nEnter the number of nodes : ");
scanf("%d", &numOfNodes);
// Get Distance Matrix
printf("\nEnter the distance matrix :\n");
for (i = 0; i < numOfNodes; i++)
{
for (j = 0; j < numOfNodes; j++)
{
scanf("%d", &distMatrix[i][j]);
distMatrix[i][i] = 0;
route[i].dist[j] = distMatrix[i][j];
route[i].from[j] = j;
}
}
do
{
count = 0;
for (i = 0; i < numOfNodes; i++)
for (j = 0; j < numOfNodes; j++)
for (k = 0; k < numOfNodes; k++)
if (route[i].dist[j] > distMatrix[i][k] + route[k].dist[j])
{
// Calculate the minimum distance
route[i].dist[j] = route[i].dist[k] + route[k].dist[j];
route[i].from[j] = k;
count++;
}
} while (count != 0);
// Display Distance Vectors
for (i = 0; i < numOfNodes; i++)
{
printf("\n\n For router %d\n", i + 1);
for (j = 0; j < numOfNodes; j++)
{
printf("\t\nnode %d via %d Distance %d ", j + 1, route[i].from[j] + 1, route[i].dist[j]);
}
}
printf("\n\n");
}
179 encryption.c
@@ -0,0 +1,179 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100], en[100], i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
void clrscr()
{
printf("\e[1;1H\e[2J");
}
void startMessage()
{
printf("=====================================\n");
printf(" 19BCE0565\n");
printf("=====================================\n\n");
}
void main()
{
clrscr();
startMessage();
printf("\nEnter First Prime Number\n");
scanf("%ld", &p);
flag = prime(p);
if (flag == 0)
{
printf("\nNumber is not Prime!\n");
exit(EXIT_FAILURE);
}
printf("\nEnter Second Prime Number\n");
scanf("%ld", &q);
flag = prime(q);
if (flag == 0 || p == q)
{
printf("\nNumber is not Prime or Both input are equal!\n");
exit(EXIT_FAILURE);
}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s",msg);
for (i = 0; msg[i] != '\0'; i++)
m[i] = msg[i];
n = p * q;
t = (p - 1) * (q - 1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for (i = 0; i < j - 1; i++)
printf("\n%ld\t%ld", e[i], d[i]);
encrypt();
decrypt();
}
int prime(long int pr)
{
int i;
j = sqrt(pr);
for (i = 2; i <= pr / 2; i++)
{
if (pr % i == 0)
return 0;
}
return 1;
}
void ce()
{
int k = 0;
for (i = 2; i < t; i++)
{
if (t % i == 0)
continue;
flag = prime(i);
if (flag == 1 && i != p && i != q)
{
e[k] = i;
flag = cd(e[k]);
if (flag > 0)
{
d[k] = flag;
k++;
}
if (k == 99)
break;
}
}
}
long int cd(long int x)
{
long int k = 1;
while (1)
{
k = k + t;
if (k % x == 0)
return (k / x);
}
}
void encrypt()
{
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while (i != len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for (j = 0; j < key; j++)
{
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for (i = 0; en[i] != -1; i++)
printf("%c", (int)en[i]);
}
void decrypt()
{
long int pt, ct, key = d[0], k;
i = 0;
while (en[i] != -1)
{
ct = temp[i];
k = 1;
for (j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for (i = 0; m[i] != -1; i++)
printf("%c", (int)m[i]);
prin