-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexportImportPtxts.cpp
More file actions
110 lines (82 loc) · 3.47 KB
/
exportImportPtxts.cpp
File metadata and controls
110 lines (82 loc) · 3.47 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
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
#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();
std::cout << "Message 1: " << message1 << std::endl;
std::cout << "Message 2: " << message2 << std::endl;
/* WRITING PHASE*/
{
// Create plaintext objects from imported messages
paillier_plaintext_t* ptxt1 = paillier_plaintext_from_ui(std::atoi(message1.c_str()));
paillier_plaintext_t* ptxt2 = paillier_plaintext_from_ui(std::atoi(message2.c_str()));
gmp_printf("Plaintext1 object created: %Zd\n", ptxt1);
gmp_printf("Plaintext2 object created: %Zd\n", ptxt2);
// Write plaintexts to disk
std::fstream ptxt1File("binPlaintext1.txt", std::fstream::out|std::fstream::trunc|std::fstream::binary);
std::fstream ptxt2File("binPlaintext2.txt", std::fstream::out|std::fstream::trunc|std::fstream::binary);
assert(ptxt1File.is_open());
assert(ptxt2File.is_open());
// The length of the plaintext is twice the length of the key
char* bytePtxt1 = (char*)paillier_plaintext_to_bytes(PAILLIER_BITS_TO_BYTES(pubKey->bits)*2, ptxt1);
char* bytePtxt2 = (char*)paillier_plaintext_to_bytes(PAILLIER_BITS_TO_BYTES(pubKey->bits)*2, ptxt2);
ptxt1File.write(bytePtxt1, PAILLIER_BITS_TO_BYTES(pubKey->bits)*2);
ptxt2File.write(bytePtxt2, PAILLIER_BITS_TO_BYTES(pubKey->bits)*2);
ptxt1File.close();
ptxt2File.close();
// Cleaning up
paillier_freeplaintext(ptxt1);
paillier_freeplaintext(ptxt2);
free(bytePtxt1);
free(bytePtxt2);
}
/* READING PHASE*/
{
// Read plaintext from disk
std::fstream ptxt1File("binPlaintext1.txt", std::fstream::in|std::fstream::binary);
std::fstream ptxt2File("binPlaintext2.txt", std::fstream::in|std::fstream::binary);
assert(ptxt1File.is_open());
assert(ptxt2File.is_open());
// The length of the binary plaintext is twice the length of the key
char* bytePtxt1 = (char*)malloc(PAILLIER_BITS_TO_BYTES(pubKey->bits)*2);
char* bytePtxt2 = (char*)malloc(PAILLIER_BITS_TO_BYTES(pubKey->bits)*2);
ptxt1File.read(bytePtxt1, PAILLIER_BITS_TO_BYTES(pubKey->bits)*2);
ptxt2File.read(bytePtxt2, PAILLIER_BITS_TO_BYTES(pubKey->bits)*2);
ptxt1File.close();
ptxt2File.close();
paillier_plaintext_t* ptxt1 = paillier_plaintext_from_bytes((void*)bytePtxt1, PAILLIER_BITS_TO_BYTES(pubKey->bits)*2);
paillier_plaintext_t* ptxt2 = paillier_plaintext_from_bytes((void*)bytePtxt2, PAILLIER_BITS_TO_BYTES(pubKey->bits)*2);
gmp_printf("Plaintext1 object read: %Zd\n", ptxt1);
gmp_printf("Plaintext2 object read: %Zd\n", ptxt2);
// Cleaning up
paillier_freeplaintext(ptxt1);
paillier_freeplaintext(ptxt2);
free(bytePtxt1);
free(bytePtxt2);
}
// Cleaning up
paillier_freepubkey(pubKey);
return 0;
}