-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.c
70 lines (51 loc) · 1.65 KB
/
decrypt.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
#include <stdio.h>
#include <string.h>
#include <gmp.h>
extern int readPublicKey(mpz_t e, mpz_t n);
extern int readPrivateKey(mpz_t d);
extern int readPlainText(mpz_t pt);
extern int readCipherText(mpz_t ct);
// Function computing plaintext
//
// Parameter:
// pt: plaintext
// ct: ciphertext
// d
// n
void computePlaintext(mpz_t pt, mpz_t ct, mpz_t d, mpz_t n) {
mpz_powm(pt, ct, d, n);
}
// Function comparing with original plaintext
//
// Parameter:
// ct : ciphertext
// pt : plaintext
// org_pt: original plaintext
void compareWithOriginalPlaintext(mpz_t ct, mpz_t pt, mpz_t org_pt) {
printf("\nCiphertext\n");
gmp_printf(" : %Zd\n\n", ct);
printf("Decrypted plaintext\n");
gmp_printf(" : %Zd\n\n", pt);
printf("Original plaintext (%s)\n", mpz_cmp(pt, org_pt) == 0 ? "identical" : "non-identical");
gmp_printf(" : %Zd\n\n", org_pt);
printf("The decrypted plaintext is stored in ./text/plaintext.\n\n");
}
// Decrypting function
void decrypt() {
mpz_t e, n, d;
mpz_t ct, pt, org_pt;
mpz_inits(e, n, d, ct, pt, org_pt, NULL);
// 1. Read public key
if (readPublicKey(e, n) == -1) { return; }
// 2. Read private key
if (readPrivateKey(d) == -1) { return; }
// 3. Read ciphertext
if (readCipherText(ct) == -1) { return; }
// 4. Compute plaintext
computePlaintext(pt, ct, d, n);
// 5. Read original plaintext
if (readPlainText(org_pt) == -1) { return; }
// 6. Compare with original plaintext
compareWithOriginalPlaintext(ct, pt, org_pt);
mpz_clears(e, n, d, ct, pt, org_pt, NULL);
}