-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathencryptionDecryption.cpp
More file actions
38 lines (30 loc) · 1016 Bytes
/
encryptionDecryption.cpp
File metadata and controls
38 lines (30 loc) · 1016 Bytes
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
#include <gmp.h>
#include <paillier.h>
int main(int argc, char *argv[])
{
// Security parameter (number of bits of the modulus)
const long n = 256;
// Generate public and secret keys
paillier_pubkey_t* pubKey;
paillier_prvkey_t* secKey;
paillier_keygen(n, &pubKey, &secKey, paillier_get_rand_devurandom);
// Plaintext initialization
paillier_plaintext_t* m;
m = paillier_plaintext_from_ui(2);
gmp_printf("Plaintext: %Zd\n", m);
// Encrypt the message
paillier_ciphertext_t* ctxt;
ctxt = paillier_enc(NULL, pubKey, m, paillier_get_rand_devurandom);
gmp_printf("Ciphertext: %Zd\n", ctxt);
// Decrypt the ciphertext
paillier_plaintext_t* dec;
dec = paillier_dec(NULL, pubKey, secKey, ctxt);
gmp_printf("Decrypted: %Zd\n", dec);
// Cleaning up
paillier_freepubkey(pubKey);
paillier_freeprvkey(secKey);
paillier_freeplaintext(m);
paillier_freeplaintext(dec);
paillier_freeciphertext(ctxt);
return 0;
}