forked from nasimmaleki/Cryptography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBGN.java
349 lines (348 loc) · 10.8 KB
/
BGN.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
package BGN;
import java.math.BigInteger;
/*
* This source code uses the JPBC (Java Pairing-Based
* Cryptography) library,
* which can be downloaded from
* http://gas.dia.unisa.it/projects/jpbc/
*/
import it.unisa.dia.gas.jpbc.*;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
import it.unisa.dia.gas.plaf.jpbc.pairing.a1.TypeA1CurveGenerator;
/**
* @ClassName: BGN
* @Description: This is a sample java source code of BGN PKE.
*/
public class BGN {
/**
* @ClassName: PublicKey
* @Description: This is a class for storing the
* public key (n,G,GT,e,g,h) of BGN PKE.
*/
public class PublicKey {
private BigInteger n;
private Field<Element> Field_G, Field_GT;
private Pairing pairing;
private Element g, h;
public PublicKey(BigInteger n, Field<Element> G, Field<Element> GT, Pairing pairing, Element g,
Element h) {
this.n = n;
this.Field_G = G;
this.Field_GT = GT;
this.pairing = pairing;
this.g = g;
this.h = h;
}
public Element getG() {
return g;
}
public Element getH() {
return h;
}
public BigInteger getN() {
return n;
}
public Pairing getPairing() {
return pairing;
}
public Field<Element> getField_G() {
return Field_G;
}
public Field<Element> getField_GT() {
return Field_GT;
}
}
/**
* @ClassName: PrivateKey
* @Description: This is a class for storing the
* private key (p) of BGN PKE.
*/
public class PrivateKey {
private BigInteger p;
public PrivateKey(BigInteger p) {
this.p = p;
}
public BigInteger getP() {
return p;
}
}
private static final int T = 100; // The max range of message m
private PublicKey pubkey;
private PrivateKey prikey;
/**
* @Title: keyGeneration
* @Description: This function is responsible for
* generating the public keys and the private keys.
* @param k
* the security parameter, which decides the
* length of two large prime (p and q).
* @return void
*/
public void keyGeneration(int k) {
TypeA1CurveGenerator pg = new
TypeA1CurveGenerator(2, k);
PairingParameters pp = pg.generate();
Pairing pairing = PairingFactory.getPairing(pp);
BigInteger n = pp.getBigInteger("n");
BigInteger q = pp.getBigInteger("n0");
BigInteger p = pp.getBigInteger("n1");
Field<Element> Field_G = pairing.getG1();
Field<Element> Field_GT = pairing.getGT();
Element g = Field_G.newRandomElement().getImmutable();
Element h = g.pow(q).getImmutable();
pubkey = new PublicKey(n, Field_G, Field_GT,
pairing, g, h);
prikey = new PrivateKey(p);
}
/**
* @Title: getPubkey
* @Description: This function returns the public key of
* BGN PKE.
* @return PublicKey The public key used to encrypt
* the data.
*/
public PublicKey getPubkey() {
return pubkey;
}
/**
* @Title: getPrikey
* @Description: This function returns the private key of
* BGN PKE.
* @return PrivateKey The private key used to decrypt
* the data.
*/
public PrivateKey getPrikey() {
return prikey;
}
/**
* @Title: encrypt
* @Description: This function is to encrypt the message
* m, m in [0,1,2,...,T],
* T=100 with public key.
* @param m
* The message
* @param pubkey
* The public key of BGN PKE.
* @return Element The ciphertext.
* @throws Exception
* If the plaintext is not in [0,1,2,...,n],
* there is an exception.
*/
public static Element encrypt(int m, PublicKey pubkey)
throws Exception {
if (m > T) {
throw new Exception(
"BGN.encrypt(int m, PublicKey pubkey): "
+ "plaintext m is not in [0,1,2,...,"
+ T + "]");
}
Pairing pairing = pubkey.getPairing();
Element g = pubkey.getG();
Element h = pubkey.getH();
BigInteger r = pairing.getZr().newRandomElement()
.toBigInteger();
return g.pow(BigInteger.valueOf(m)).mul(h.pow(r))
.getImmutable();
}
/**
*
* @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 BGN PKE.
* @param prikey
* The private key of BGN PKE.
* @return int The plaintext.
* @throws Exception
* If the plaintext is not in [0,1,2,...,n],
* there is an exception.
*/
public static int decrypt(Element c, PublicKey pubkey,
PrivateKey prikey) throws Exception {
BigInteger p = prikey.getP();
Element g = pubkey.getG();
Element cp = c.pow(p).getImmutable();
Element gp = g.pow(p).getImmutable();
for (int i = 0; i <= T; i++) {
if (gp.pow(BigInteger.valueOf(i)).isEqual(cp)) {
return i;
}
}
throw new Exception(
"BGN.decrypt(Element c, PublicKey pubkey, PrivateKey prikey): "
+ "plaintext m is not in [0,1,2,...,"
+ T + "]");
}
public static int decrypt_mul2(Element c, PublicKey pubkey,
PrivateKey prikey) throws Exception {
BigInteger p = prikey.getP();
Element g = pubkey.getG();
Element cp = c.pow(p).getImmutable();
Element egg = pubkey.getPairing().pairing(g, g).pow(p)
.getImmutable();
for (int i = 0; i <= T; i++) {
if (egg.pow(BigInteger.valueOf(i)).isEqual(cp)) {
return i;
}
}
throw new Exception(
"BGN.decrypt(Element c, PublicKey pubkey, PrivateKey prikey): "
+ "plaintext m is not in [0,1,2,...,"
+ T + "]");
}
/**
* @Title: add
* @Description: The function supports the homomorphic
* addition with two ciphertext.
* @param c1
* The ciphertext.
* @param c2
* The ciphertext.
* @parampubkey
* The public key of BGN PKE.
* @return Element The return value is c1*c2.
*/
public static Element add(Element c1, Element c2) {
return c1.mul(c2).getImmutable();
}
/**
* @Title: mul1
* @Description: The function supports the homomorphic
* multiplication with one ciphertext
* and one plaintext.
* @paramc
* The ciphertext.
* @paramm
* The plaintext.
* @parampubkey
* The public key of BNG PKE.
* @return Element The return value is c^m.
*/
public static Element mul1(Element c1, int m2) {
return c1.pow(BigInteger.valueOf(m2)).getImmutable();
}
/**
* @Title: mul2
* @Description: TODO
* @param c1
* The ciphertext.
* @param c2
* The ciphertext.
* @param pubkey
* The public key of BNG PKE.
* @return Element The return value is e(c1,c2).
*/
public static Element mul2(Element c1, Element c2,
PublicKey pubkey) {
Pairing pairing = pubkey.getPairing();
return pairing.pairing(c1, c2).getImmutable();
}
/**
* @Title: selfBlind
* @Description: The function supports the homomorphic
* self-blinding with one ciphertext
* and one random number.
* @paramc
* The ciphertext.
* @paramr
* A random number in Z_n.
* @param pubkey
* The public key of BNG PKE.
* @return Element The return value is c1*h^r2.
*/
public static Element selfBlind(Element c1, BigInteger r2,
PublicKey pubkey) {
Element h = pubkey.getH();
return c1.mul(h.pow(r2)).getImmutable();
}
public static void main(String[] args) {
BGN bgn = new BGN();
// Key Generation
bgn.keyGeneration(512);
BGN.PublicKey pubkey = bgn.getPubkey();
BGN.PrivateKey prikey = bgn.getPrikey();
// Encryption and Decryption
int m = 5;
Element c = null;
int decrypted_m = 0;
try {
c = BGN.encrypt(m, pubkey);
decrypted_m = BGN.decrypt(c, pubkey, prikey);
} catch (Exception e) {
e.printStackTrace();
}
if (decrypted_m == m) {
System.out.println("Encryption and Decryption "
+ "test successfully.");
}
// Homomorphic Properties
// Addition
int m1 = 5;
int m2 = 6;
try {
Element c1 = BGN.encrypt(m1, pubkey);
Element c2 = BGN.encrypt(m2, pubkey);
Element c1mulc2 = BGN.add(c1, c2);
int decrypted_c1mulc2 = BGN.decrypt(c1mulc2,
pubkey, prikey);
if (decrypted_c1mulc2 == (m1 + m2)) {
System.out.println("Homomorphic addition "
+ "tests successfully.");
}
} catch (Exception e) {
e.printStackTrace();
}
// multiplication-1
m1 = 5;
m2 = 6;
try {
Element c1 = BGN.encrypt(m1, pubkey);
Element c1expm2 = BGN.mul1(c1, m2);
int decrypted_c1expm2 = BGN.decrypt(c1expm2,
pubkey, prikey);
if (decrypted_c1expm2 == (m1 * m2)) {
System.out.println("Homomorphic multiplication-1 "
+ "tests successfully.");
}
} catch (Exception e) {
e.printStackTrace();
}
// multiplication-2
m1 = 5;
m2 = 6;
try {
Element c1 = BGN.encrypt(m1, pubkey);
Element c2 = BGN.encrypt(m2, pubkey);
Element c1pairingc2 = pubkey.getPairing()
.pairing(c1, c2).getImmutable();
int decrypted_c1pairingc2 =
BGN.decrypt_mul2(c1pairingc2, pubkey, prikey);
if (decrypted_c1pairingc2 == (m1 * m2)) {
System.out.println("Homomorphic multiplication-2 "
+ "tests successfully.");
}
} catch (Exception e) {
e.printStackTrace();
}
// self-Blinding
m1 = 5;
try {
BigInteger r2 = pubkey.getPairing().getZr()
.newRandomElement().toBigInteger();
Element c1 = BGN.encrypt(m1, pubkey);
Element c1_selfblind = BGN.selfBlind(c1,
r2, pubkey);
int decrypted_c1_selfblind =
BGN.decrypt(c1_selfblind, pubkey, prikey);
if (decrypted_c1_selfblind == m1) {
System.out.println("Homomorphic self-blinding "
+ "tests successfully.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}