-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
107 lines (77 loc) · 2.76 KB
/
main.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
#include "bpd.h"
int main(void) {
/* Génération de clés */
srand(time(NULL));
long p = random_prime_number(3,7,5000);
long q = random_prime_number(3,7,5000);
while(p==q){
q = random_prime_number(3,7, 5000);
}
long n, s, u;
generate_key_values(p, q, &n, &s, &u);
/* Pour avoir des clés positives */
if (u < 0) {
long t = (p - 1) * (q - 1);
u = u + t; // On aura toujours (s * u) mod t = 1
}
/* Affichage des clés en hexadecimal */
printf("cle publique = (%lx %lx) \n", s, n);
printf("cle privee = (%lx, %lx) \n", u, n);
/* Chiffrement */
char mess[10] = "Hello";
int len = strlen(mess);
long * crypted = encrypt(mess, s, n);
printf("Initial message : %s \n", mess);
printf("Encoded representation : \n");
print_long_vector(crypted, len);
/* Déchiffrement */
char* decoded = decrypt(crypted, len, u, n);
printf("Decoded : %s\n\n", decoded);
/* Initialisation de clés */
Key* pKey = malloc(sizeof(Key));
Key* sKey = malloc(sizeof(Key));
init_pair_keys(pKey, sKey, 3, 7);
printf("pKey : %lx, %lx \n", pKey -> val, pKey -> n);
printf("sKey : %lx, %lx \n", sKey -> val, sKey -> n);
char* chaine = key_to_str(pKey);
printf("key to str : %s \n", chaine);
Key* k = str_to_key(chaine);
printf("str to key : %lx, %lx \n", k -> val, k -> n);
/* Initialisation de signatures */
Key* pKeyC = malloc(sizeof(Key));
Key* sKeyC = malloc(sizeof(Key));
init_pair_keys(pKeyC, sKeyC, 3, 7);
char* mess2 = key_to_str(pKeyC);
printf("%s vote pour %s\n", key_to_str(pKey), mess2);
Signature* sgn = sign(mess2, sKey);
printf("Signature : ");
print_long_vector(sgn -> content, sgn -> size);
chaine = signature_to_str(sgn);
printf("signature_to_str : %s \n", chaine);
sgn = str_to_signature(chaine);
printf("str_to_signature : ");
print_long_vector(sgn -> content, sgn -> size);
/* Initialisation de déclarations signées */
Protected* pr = init_protected(pKey, mess2, sgn);
if (verify(pr)) {
printf("Signature valide\n");
} else {
printf("Signature non valide\n");
}
chaine = protected_to_str(pr);
printf("protected_to_str : %s\n", chaine);
pr = str_to_protected(chaine);
printf("str_to_protected : %s %s %s\n\n", key_to_str(pr -> pKey), pr -> mess, signature_to_str(pr -> sgn));
free(pKey);
free(sKey);
free(pKeyC);
free(sKeyC);
/* Vérification de l'installation de la bibliothèque */
const char * s2 = "Rosetta code" ;
unsigned char * d2 = SHA256 (s2, strlen (s2), 0) ;
for (int i = 0; i < SHA256_DIGEST_LENGTH ; i++){
printf ("%02x", d2[i]);
}
putchar('\n');
return 0;
}