-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyread.c
122 lines (95 loc) · 2.68 KB
/
keyread.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
#include <stdio.h>
#include <string.h>
#include <gmp.h>
// Function reading public key
//
// Parameter:
// e
// n
//
// Return:
// 0: success to read
// -1: fail to read
int readPublicKey(mpz_t e, mpz_t n) {
char str[1024], prefix[4], key[1024];
FILE *public_key_file;
// Open public key file
public_key_file = fopen("./key/public_key", "rt");
if (public_key_file == NULL) {
printf("There is no public key file.\n");
printf("The public key must be stored in ./key/public_key.\n");
return -1;
}
// Read e
strcpy(str, "");
strcpy(prefix, "");
strcpy(key, "");
fgets(str, 1024, public_key_file);
strncpy(prefix, str, 3);
if (strcmp(prefix, "e: \0") != 0) {
printf("The public key file is corrupted.\n");
printf("Please generate keys again.\n");
return -1;
}
strncpy(key, str + 3, strlen(str));
if(mpz_init_set_str(e, key, 10) == -1) {
printf("The public key e is invalid.\n");
}
// Read n
strcpy(str, "");
strcpy(prefix, "");
strcpy(key, "");
fgets(str, 1024, public_key_file);
strncpy(prefix, str, 3);
if (strcmp(prefix, "n: \0") != 0) {
printf("The public key file is corrupted.\n");
printf("Please generate keys again.\n");
return -1;
}
strncpy(key, str + 3, strlen(str));
if(mpz_init_set_str(n, key, 10) == -1) {
printf("The public key n is invalid.\n");
return -1;
}
// Close public key file
fclose(public_key_file);
return 0;
}
// Function reading private key
//
// Parameter:
// d
//
// Return:
// 0: success to read
// -1: fail to read
int readPrivateKey(mpz_t d) {
char str[1024], prefix[4], key[1024];
FILE *private_key_file;
// Open private key file
private_key_file = fopen("./key/private_key", "rt");
if (private_key_file == NULL) {
printf("There is no private key file.\n");
printf("The private key must be stored in ./key/private_key.\n");
return -1;
}
// Read d
strcpy(str, "");
strcpy(prefix, "");
strcpy(key, "");
fgets(str, 1024, private_key_file);
strncpy(prefix, str, 3);
if (strcmp(prefix, "d: \0") != 0) {
printf("The private key file is corrupted.\n");
printf("Please generate keys again.\n");
return -1;
}
strncpy(key, str + 3, strlen(str));
if(mpz_init_set_str(d, key, 10) == -1) {
printf("The private key d is invalid.\n");
return -1;
}
// Close private key file
fclose(private_key_file);
return 0;
}