-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
310 lines (272 loc) · 7.61 KB
/
utils.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
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
308
309
310
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//extern void encrypt(unsigned int d, unsigned int n);
//extern void decrypt(unsigned int e, unsigned int n);
extern void generateKeys();
unsigned int modular_exponentation(unsigned int a, unsigned int b, unsigned int m);
char* encrypt();
int modInverse(int A, int M) {
for (int X = 1; X < M; X++) {
int aMod = A % M;
int xMod = X % M;
int axMod = aMod * xMod;
int mod = axMod % M;
if (mod == 1) {
return X;
}
}
return -1;
}
void write_public_key(unsigned int e, unsigned int n) {
FILE *fptr;
fptr = fopen("key_pub.txt", "w");
if (fptr == NULL) {
printf("Couldn't write public key file.");
return;
}
fprintf(fptr, "%d,%d", e, n);
fclose(fptr);
}
void write_private_key(unsigned int d, unsigned int n) {
FILE *fptr;
fptr = fopen("key_priv.txt", "w");
if (fptr == NULL) {
printf("Couldn't write private key file.");
return;
}
fprintf(fptr, "%d,%d", d, n);
fclose(fptr);
}
void read_private_key() {
FILE *fptr;
char buffer[100];
int d,n;
fptr = fopen("key_priv.txt", "r");
if (fptr == NULL) {
printf("Couldn't write private key file.");
return;
}
if(fgets(buffer, sizeof(buffer), fptr) != NULL) {
if(sscanf(buffer, "%d,%d", &d, &n) == 2) {
printf("d: %d\n", d);
printf("n: %d\n", n);
} else {
fprintf(stderr, "Error parsing values\n");
fclose(fptr);
return;
}
} else {
fprintf(stderr, "Error reading file\n");
fclose(fptr);
return;
}
fclose(fptr);
return;
}
void read_public_key() {
FILE *fptr;
char buffer[100];
int e,n;
fptr = fopen("key_pub.txt", "r");
if (fptr == NULL) {
printf("Couldn't write public key file.");
return;
}
if(fgets(buffer, sizeof(buffer), fptr) != NULL) {
if(sscanf(buffer, "%d,%d", &e, &n) == 2) {
printf("e: %d\n", e);
printf("n: %d\n", n);
} else {
fprintf(stderr, "Error parsing values\n");
fclose(fptr);
return;
}
} else {
fprintf(stderr, "Error reading file\n");
fclose(fptr);
return;
}
fclose(fptr);
return;
}
int reverse_number(int num) {
char str[20];
char temp;
int len, i;
snprintf(str, sizeof(str), "%d", num);
len = strlen(str);
for (i = 0; i < len / 2; i += 2) {
temp = str[i];
str[i] = str[len - i - 2];
str[len - i - 2] = temp;
temp = str[i + 1];
str[i + 1] = str[len - i - 1];
str[len - i - 1] = temp;
}
return atoi(str);
}
char* encrypt() {
FILE *fptr;
char buffer[100];
int e,n;
fptr = fopen("key_pub.txt", "r");
if (fptr == NULL) {
printf("Couldn't write public key file.");
return NULL;
}
if(fgets(buffer, sizeof(buffer), fptr) != NULL) {
if(sscanf(buffer, "%d,%d", &e, &n) == 2) {
} else {
fprintf(stderr, "Error parsing values\n");
fclose(fptr);
return NULL;
}
} else {
fprintf(stderr, "Error reading file\n");
fclose(fptr);
return NULL;
}
fclose(fptr);
char input[100];
int output[10];
for(int i = 0; i < 10; i++) {
output[i] = 0;
}
int outputIterator = 0;
printf("Podaj tekst do zaszyfrowania (tylko cyfry i litery): ");
scanf("%s", input);
int exponent = 0;
int number = 0;
int i = 0;
for(; i < 100; i++) {
char letter = input[i];
if (letter == '\0') {
break;
}
if(letter < ' ' || letter > 'z') {
printf("Nieprawidłowe dane wejściowe!\n");
return NULL;
}
int asciiValue = ((int) letter) - 22;
int buffer_number = number;
buffer_number += pow(10, exponent) * asciiValue;
buffer_number = reverse_number(buffer_number);
if(buffer_number > n) {
output[outputIterator++] = reverse_number(number);
number = 0;
i--;
exponent = 0;
continue;
}
number = reverse_number(buffer_number);
exponent += 2;
if (input[i + 1] == '\0') {
output[outputIterator++] = reverse_number(number);
}
}
printf("e: %d, n: %d\n", e, n);
char finalOutput[outputIterator][5];
int finalOutuptInterator = 0;
int maxLength = -1;
for(int i = 0; i < outputIterator; i++) {
char encrypted[5];
printf("Not decrypted data before: %d\n", output[i]);
int modExpression = modular_exponentation(output[i], e, n);
printf("Decrypted data before: %d\n", modExpression);
sprintf(encrypted, "%d", modExpression);
int strLen = strlen(encrypted);
if(strLen < 5) {
char fixed[5];
for(int i = 0; i < 5 - strLen; i++) {
fixed[i] = '0';
}
int j = 0;
for(int i = 5 - strLen; i < 5; i++) {
fixed[i] = encrypted[j];
j++;
}
for(int i = 0; i < 5; i++) {
encrypted[i] = fixed[i];
}
}
for(int i = 0; i < 5; i++) {
finalOutput[finalOutuptInterator][i] = encrypted[i];
}
finalOutuptInterator++;
}
int finalLength = outputIterator * 5;
char *finalFinalOutput = (char*)malloc(finalLength * sizeof(char));
int iterator = 0;
for(int i = 0; i < outputIterator; i++) {
for(int j = 0; j < 5; j++) {
finalFinalOutput[iterator++] = finalOutput[i][j];
}
}
printf("%s\n", finalFinalOutput);
return finalFinalOutput;
}
unsigned int modular_exponentation(unsigned int a, unsigned int b, unsigned int m) {
unsigned int result = 1;
a = a % m;
if (a == 0) {
return 0;
}
while (b > 0) {
if((b & 1) == 1) {
result = (result * a) % m;
}
b = b >> 1;
a = (a * a) % m;
}
return result;
}
void decrypt(char* c) {
FILE *fptr;
char buffer[100];
int d,n;
fptr = fopen("key_priv.txt", "r");
if (fptr == NULL) {
printf("Couldn't write private key file.");
return;
}
if(fgets(buffer, sizeof(buffer), fptr) != NULL) {
if(sscanf(buffer, "%d,%d", &d, &n) == 2) {
printf("d: %d\n", d);
printf("n: %d\n", n);
} else {
fprintf(stderr, "Error parsing values\n");
fclose(fptr);
return;
}
} else {
fprintf(stderr, "Error reading file\n");
fclose(fptr);
return;
}
fclose(fptr);
int cLength = strlen(c);
char result[cLength];
int partNumber = cLength / 5;
char messageParts[partNumber][6];
for(int i = 0; i < partNumber; i++) {
for(int j = 0; j < 5; j++) {
messageParts[i][j] = c[j + (i * 5)];
}
messageParts[i][6] = '\0';
}
for(int i = 0; i < partNumber; i++) {
printf("Decrypted data after: %d\n", atoi(messageParts[i]));
int number = modular_exponentation(atoi(messageParts[i]), d, n);
printf("Not decrypted data after: %d\n", number);
int a = number % 100 + 22;
int b = (number - a - 22) / 100 + 22;
//printf("number2: %d, %d-%d\n", number, a, b);
}
}
void main(int argc, char *argv[]) {
generateKeys();
char* encryptedMessage = encrypt();
decrypt(encryptedMessage);
}