-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPaillier.java
357 lines (357 loc) · 11.7 KB
/
Paillier.java
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package Paillier;
import java.math.BigInteger;
import java.security.SecureRandom;
/**
* @ClassName: Paillier
* @Description: This is a sample java source code of Paillier
* PKE.
*/
public class Paillier {
/**
* @ClassName: PublicKey
* @Description: This is a class for storing the public
* key (n, g) of Paillier PKE.
*/
public class PublicKey {
private BigInteger n, g;
public PublicKey(BigInteger n, BigInteger g) {
this.n = n;
this.g = g;
}
public BigInteger getN() {
return n;
}
public BigInteger getG() {
return g;
}
}
/**
* @ClassName: PrivateKey
* @Description: This is a class for storing the private
* key (lambda, mu) of Paillier PKE.
*/
public class PrivateKey {
private BigInteger lambda, mu;
public PrivateKey(BigInteger lambda, BigInteger mu) {
this.lambda = lambda;
this.mu = mu;
}
public BigInteger getLambda() {
return lambda;
}
public BigInteger getMu() {
return mu;
}
}
private final int CERTAINTY = 64;
private PublicKey pubkey; // The public key of Paillier PKE, (n, g)
private PrivateKey prikey; // The private key of Paillier PKE, (lambda, mu)
/**
* @Title: getPubkey
* @Description: This function returns the generated
* public key.
* @return PublicKey The public key used to encrypt
* the data.
*/
public PublicKey getPubkey() {
return pubkey;
}
/**
* @Title: getPrikey
* @Description: This function returns the generated
* private key.
* @return PrivateKey The private key used to decrypt
* the data.
*/
public PrivateKey getPrikey() {
return prikey;
}
/**
* @Title: keyGeneration
* @Description: This function is to help generate the
* public key and
* private key for encryption and decryption.
* @param k
* k is the security parameter, which decides
* the length of two large primes (p and q).
* @return void
*/
public void keyGeneration(int k) {
BigInteger p_prime, q_prime, p, q;
do {
p_prime = new BigInteger(k, CERTAINTY,
new SecureRandom());
p = (p_prime.multiply(BigInteger.valueOf(2)))
.add(BigInteger.ONE);
} while (!p.isProbablePrime(CERTAINTY));
do {
do {
q_prime = new BigInteger(k, CERTAINTY,new SecureRandom());
} while (p_prime.compareTo(q_prime) == 0);
q = (q_prime.multiply(BigInteger.valueOf(2)))
.add(BigInteger.ONE);
} while (!q.isProbablePrime(CERTAINTY));
// The following steps are to generate the keys
// n=p*q
BigInteger n = p.multiply(q);
// nsquare=n^2
BigInteger nsquare = n.pow(2);
// a generator g=(1+n) in Z*_(n^2)
BigInteger g = BigInteger.ONE.add(n);
// lambda = lcm(p-1, q-1) = p_prime*q_prime
BigInteger lambda = BigInteger.valueOf(2)
.multiply(p_prime)
.multiply(q_prime);
// mu = (L(g^lambda mod n^2))^{-1} mod n
BigInteger mu = Lfunction(g.modPow(lambda, nsquare), n)
.modInverse(n);
pubkey = new PublicKey(n, g);
prikey = new PrivateKey(lambda, mu);
}
/**
* @Title: encrypt
* @Description: This function is to encrypt the message
* with Paillier’s public key.
* @param m
* The message.
* @param pubkey
* The public key of Paillier PKE.
* @return BigInteger The ciphertext.
* @throws Exception
* If the message is not in Z*_n, there is
* an exception.
*/
public static BigInteger encrypt(BigInteger m,
PublicKey pubkey) throws Exception {
BigInteger n = pubkey.getN();
BigInteger nsquare = n.pow(2);
BigInteger g = pubkey.getG();
if (!belongToZStarN(m, n)) {
throw new Exception(
"Paillier.encrypt(BigInteger m, PublicKey pubkey): plaintext m is not in Z*_n");
}
BigInteger r = randomZStarN(n);
return (g.modPow(m, nsquare).multiply(r.modPow(n,
nsquare))).mod(nsquare);
}/**
* @Title: decrypt
* @Description: This function is to decrypt the ciphertext
* with the public key and the private key.
* @param c
* The ciphertext.
* @param pubkey
* The public key of Paillier PKE.
* @param prikey
* The private key of Paillier PKE.
* @return BigInteger The plaintext.
* @throws Exception
* If the cipher is not in Z*_(n^2), there is
* an exception.
*/
public static BigInteger decrypt(BigInteger c, PublicKey
pubkey, PrivateKey prikey) throws Exception {
BigInteger n = pubkey.getN();
BigInteger nsquare = n.pow(2);
BigInteger lambda = prikey.getLambda();
BigInteger mu = prikey.getMu();
if (!belongToZStarNSquare(c, nsquare)) {
throw new Exception(
"Paillier.decrypt(BigInteger c, PrivateKey prikey): ciphertext c is not in Z*_(n^2)");
}
return Lfunction(c.modPow(lambda, nsquare), n).
multiply(mu).mod(n);
}
/**
* @Title: add
* @Description: The function supports the homomorphic
* addition with two ciphertext.
* @param c1
* The ciphertext.
* @param c2
* The ciphertext.
* @param pubkey
* The public key of Paillier PKE.
* @return BigInteger The return value is c1*c2 mod n^2.
*/
public static BigInteger add(BigInteger c1, BigInteger c2,
PublicKey pubkey) {BigInteger nsquare = pubkey.getN()
.pow(2);return c1.multiply(c2).mod(nsquare);
}
/**
* @Title: mul
* @Description: The function supports the homomorphic
* multiplication with one ciphertext and one plaintext.
* @param c
* The ciphertext.
* @param m
* The plaintext.
* @param pubkey
* The public key of Paillier PKE.
* @return BigInteger The return value is c^m mod n^2.
*/
public static BigInteger mul(BigInteger c, BigInteger m,
PublicKey pubkey) {BigInteger nsquare =
pubkey.getN().pow(2);
return c.modPow(m, nsquare);
}
/**
* @Title: selfBlind
* @Description: The function supports the homomorphic
* self-blinding with one ciphertext and one random number.
* @param c
* The ciphertext.
* @param r
* A random number in Z*_n.
* @param pubkey
* The public key of Paillier PKE.
* @return BigInteger The return value is c*r^n mod n^2.
*/
public static BigInteger selfBlind(BigInteger c,
BigInteger r, PublicKey pubkey) {
BigInteger n = pubkey.getN();
BigInteger nsquare = n.pow(2);
return c.multiply(r.modPow(n, nsquare)).mod(nsquare);
}
/**
* @Title: Lfunction
* @Description: This function is the L function which is
* defined by Paillier PKE, L(mu)=(mu-1)/n.
* @param mu
* The input parameter.
* @param n
* n=p*q.
* @return BigInteger The return value is (mu-1)/n.
*/
private static BigInteger Lfunction(BigInteger mu,
BigInteger n) {return
mu.subtract(BigInteger.ONE).divide(n);
}
/**
* @Title: randomZStarN
* @Description: This function returns a ramdom number in
* Z*_n.
* @param n
* n=p*q.
* @return BigInteger A random number in Z*_n.
*/
public static BigInteger randomZStarN(BigInteger n) {
BigInteger r;
do {
r = new BigInteger(n.bitLength(), new
SecureRandom());
} while (r.compareTo(n) >= 0 || r.gcd(n).intValue()
!= 1);
return r;
}
/**
*
* @Title: belongToZStarN
* @Description: This function is to test whether the
* plaintext is in Z*_n.
* @param m
* The plaintext.
* @param n
* n=p*q.
* @return boolean If it is true, the plaintext is Z*_n,
* otherwise, not.
*/
private static boolean belongToZStarN(BigInteger m,
BigInteger n) {
if (m.compareTo(BigInteger.ZERO) < 0 ||
m.compareTo(n) >= 0
|| m.gcd(n).intValue() != 1) {
return false;
}
return true;
}
/**
*
* @Title: belongToZStarNSquare
* @Description: This function is to test whether the
* ciphertext is in
* Z*_(n^2).
* @param c
* The ciphertext.
* @param nsquare
* nsquare=n^2.
* @return boolean If it is true, the ciphertext is
* Z*_(n^2), otherwise, not.
*/
private static boolean belongToZStarNSquare(BigInteger c,
BigInteger nsquare){
if (c.compareTo(BigInteger.ZERO) < 0 ||
c.compareTo(nsquare) >= 0
|| c.gcd(nsquare).intValue() != 1) {
return false;
}
return true;
}
public static void main(String[] args) {
Paillier paillier = new Paillier();
// KeyGeneration
paillier.keyGeneration(512);
Paillier.PublicKey pubkey = paillier.getPubkey();
Paillier.PrivateKey prikey = paillier.getPrikey();
// Encryption and Decryption
BigInteger m = new BigInteger(new String("Hello").getBytes());
BigInteger c = null;
BigInteger decrypted_m = null;
try {
c = Paillier.encrypt(m, pubkey);
decrypted_m = Paillier.decrypt(c, pubkey, prikey);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (decrypted_m.compareTo(m) == 0) {
System.out.println("Encryption and Decryption test successfully.");
}
// Homomorphic Properties
// Addition
BigInteger m1 = new BigInteger("12345");
BigInteger m2 = new BigInteger("56789");
BigInteger m1plusm2 = m1.add(m2);
try {
BigInteger c1 = Paillier.encrypt(m1, pubkey);
BigInteger c2 = Paillier.encrypt(m2, pubkey);
BigInteger c1mulc2 = Paillier.add(c1, c2, pubkey);
BigInteger decrypted_c1mulc2 =
Paillier.decrypt(c1mulc2, pubkey, prikey);
if (decrypted_c1mulc2.compareTo(m1plusm2) == 0) {
System.out.println("Homomorphic addition tests successfully.");
}
} catch (Exception e) {
e.printStackTrace();
}
// Multiplication
m1 = new BigInteger("12345");
m2 = new BigInteger("56789");
BigInteger m1mulm2 = m1.multiply(m2);
try {
BigInteger c1 = Paillier.encrypt(m1, pubkey);
BigInteger c1expm2 = Paillier.mul(c1, m2, pubkey);
BigInteger decrypted_c1expm2 =
Paillier.decrypt(c1expm2, pubkey, prikey);
if (decrypted_c1expm2.compareTo(m1mulm2) == 0) {
System.out.println("Homomorphic multiplication tests successfully.");
}
} catch (Exception e) {
e.printStackTrace();
}
// Self-Blinding
m1 = new BigInteger("12345");
BigInteger r2 = Paillier.randomZStarN(pubkey.getN());
try {
BigInteger c1 = Paillier.encrypt(m1, pubkey);
BigInteger c1mulrn = Paillier.selfBlind(c1, r2,
pubkey);
BigInteger decrypted_c1mulrn =
Paillier.decrypt(c1mulrn, pubkey, prikey);
if (decrypted_c1mulrn.compareTo(m1) == 0) {
System.out.println("Homomorphic self-blinding tests successfully.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}