-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
78 lines (65 loc) · 2.47 KB
/
Server.java
File metadata and controls
78 lines (65 loc) · 2.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
import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
/**
* Created by naveed on 2/15/15.
*/
public class Server {
public static void main(String[] args) {
String filename = args[0];
String clientFilename = args[1];
Inputs inputs = new Inputs(filename);
BigInteger[] serverInputs = inputs.getInputs();
BigInteger[] encryptedPoly = (BigInteger[]) read(clientFilename);
BigInteger publicKey = (BigInteger) read("ClientPK.out");
Paillier paillier = new Paillier();
paillier.setPublicKey(publicKey);
BigInteger[] encryptedPolyEval = new BigInteger[serverInputs.length];
int j = 0;
for (BigInteger serverInput : serverInputs) {
encryptedPolyEval[j] = paillier.Encryption(BigInteger.ZERO);
for (int i = encryptedPoly.length - 1; i >= 0; i--) {
encryptedPolyEval[j] = paillier.add(encryptedPoly[i], paillier.const_mul(encryptedPolyEval[j], serverInput));
}
encryptedPolyEval[j] = paillier.const_mul(encryptedPolyEval[j], nextRandomBigInteger(BigInteger.valueOf(2^32)));
encryptedPolyEval[j] = paillier.add(encryptedPolyEval[j], paillier.Encryption(serverInput));
j++;
}
write(encryptedPolyEval, clientFilename+".out");
}
public static void write(Object object, String filename){
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(new FileOutputStream(filename));
oos.writeObject(object);
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Object read(String filename){
ObjectInputStream ois;
Object object = null;
try {
ois = new ObjectInputStream(new FileInputStream(filename));
object = ois.readObject();
ois.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return object;
}
//This is not cryptographically secure random number.
public static BigInteger nextRandomBigInteger(BigInteger n) {
Random rand = new Random();
BigInteger result = new BigInteger(n.bitLength(), rand);
while( result.compareTo(n) >= 0 ) {
result = new BigInteger(n.bitLength(), rand);
}
return result;
}
}