-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcreateExportCtxts.cpp
More file actions
76 lines (57 loc) · 2.38 KB
/
createExportCtxts.cpp
File metadata and controls
76 lines (57 loc) · 2.38 KB
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
#include <assert.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <gmp.h>
#include <paillier.h>
#include <string>
int main (int argc, char *argv[])
{
// Read public key from disk and initialize it
std::fstream pubKeyFile("pubkey.txt", std::fstream::in);
assert(pubKeyFile.is_open());
std::string hexPubKey;
std::getline(pubKeyFile, hexPubKey);
pubKeyFile.close();
paillier_pubkey_t* pubKey = paillier_pubkey_from_hex(&hexPubKey[0]);
// Read messages from disk
std::fstream message1File("message1.txt", std::fstream::in);
std::fstream message2File("message2.txt", std::fstream::in);
assert(message1File.is_open());
assert(message2File.is_open());
std::string message1;
std::string message2;
std::getline(message1File, message1);
std::getline(message2File, message2);
message1File.close();
message2File.close();
// Encrypt messages
paillier_plaintext_t* m1 = paillier_plaintext_from_ui(std::atoi(message1.c_str()));
paillier_plaintext_t* m2 = paillier_plaintext_from_ui(std::atoi(message2.c_str()));
paillier_ciphertext_t* ctxt1;
paillier_ciphertext_t* ctxt2;
ctxt1 = paillier_enc(NULL, pubKey, m1, paillier_get_rand_devurandom);
ctxt2 = paillier_enc(NULL, pubKey, m2, paillier_get_rand_devurandom);
gmp_printf("ctxt1: %Zd\n", ctxt1);
// Write ciphertexts to disk
std::fstream ctxt1File("ciphertext1.txt", std::fstream::out|std::fstream::trunc|std::fstream::binary);
std::fstream ctxt2File("ciphertext2.txt", std::fstream::out|std::fstream::trunc|std::fstream::binary);
assert(ctxt1File.is_open());
assert(ctxt2File.is_open());
// The length of the ciphertext is twice the length of the key
char* byteCtxt1 = (char*)paillier_ciphertext_to_bytes(PAILLIER_BITS_TO_BYTES(pubKey->bits)*2, ctxt1);
char* byteCtxt2 = (char*)paillier_ciphertext_to_bytes(PAILLIER_BITS_TO_BYTES(pubKey->bits)*2, ctxt2);
ctxt1File.write(byteCtxt1, PAILLIER_BITS_TO_BYTES(pubKey->bits)*2);
ctxt2File.write(byteCtxt2, PAILLIER_BITS_TO_BYTES(pubKey->bits)*2);
ctxt1File.close();
ctxt2File.close();
// Cleaning up
paillier_freepubkey(pubKey);
paillier_freeplaintext(m1);
paillier_freeplaintext(m2);
paillier_freeciphertext(ctxt1);
paillier_freeciphertext(ctxt2);
free(byteCtxt1);
free(byteCtxt2);
return 0;
}