-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputationEnvelopeSubtask.java
225 lines (179 loc) · 7.97 KB
/
ComputationEnvelopeSubtask.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
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Pairing;
import java.security.MessageDigest;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
import it.unisa.dia.gas.jpbc.Field;
import javax.crypto.Cipher;
public class ComputationEnvelopeSubtask extends Envelope {
private String Proof;
private Pairing pairings;
private Element g1;
private Element g2;
private Element PublicKey;
// private Pairing pairing;
public Pairing getPairings() {
return pairings;
}
public void setPairings(Pairing pairing) {
this.pairings = pairing;
}
public Element getG1() {
return g1;
}
public void setG1(Element g1) {
this.g1 = g1;
}
public Element getG2() {
return g2;
}
public void setG2(Element g2) {
this.g2 = g2;
}
private static String encryptWithPublicKey(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] byteData = data.getBytes(StandardCharsets.UTF_8);
byte[] encryptedData = cipher.doFinal(byteData);
return Base64.getEncoder().encodeToString(encryptedData);
}
private static String decryptWithPrivateKey(String encryptedData, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] data = Base64.getDecoder().decode(encryptedData.getBytes());
byte[] decrypt = cipher.doFinal(data);
String decryptedData = new String(decrypt, StandardCharsets.UTF_8);
return decryptedData;
}
public static boolean isPrime(int n, double sqrt) {
if (n <= 1)
return false;
if (n == 2 || n == 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i <= sqrt; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public ComputationEnvelopeSubtask(EnvelopeType envType, Node sentBy, Node receivedBy) {
super(envType, sentBy, receivedBy);
}
public static Envelope createEnvelope(Node sender, Node receiver, Envelope prevEnvelope, String functiontype) {
try {
String decryptedString = decryptWithPrivateKey(prevEnvelope.getEncryptedContent(), sender.getPrivateKey());
String decryptedArray[] = decryptedString.substring(1, decryptedString.length() - 1).split(", ");
StringBuilder results = new StringBuilder();
if (functiontype.equals("sqrt")) {
for (int i = Integer.parseInt(decryptedArray[1]); i <= Integer.parseInt(decryptedArray[2]); i++) {
String encryptedResult = encryptWithPublicKey(String.valueOf((int) Math.sqrt(i)),
receiver.getPublicKey());
results.append(encryptedResult);
results.append(" ");
}
ComputationEnvelopeSubtask envelope = new ComputationEnvelopeSubtask(EnvelopeType.envcs, sender,
receiver);
envelope.setEncryptedContent(results.toString());
return envelope;
} else {
for (int i = Integer.parseInt(decryptedArray[1]); i <= Integer.parseInt(decryptedArray[2]); i++) {
Boolean TorF = isPrime(i, (int) Math.sqrt(i));
String res = String.valueOf(i) + "->" + String.valueOf(TorF);
System.out.println(res);
String encryptedResult = encryptWithPublicKey(res,
receiver.getPublicKey());
results.append(encryptedResult);
results.append(" ");
}
ComputationEnvelopeSubtask envelope = new ComputationEnvelopeSubtask(EnvelopeType.envcs, sender,
receiver);
envelope.setEncryptedContent(results.toString());
return envelope;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static ComputationEnvelopeSubtask createCsEnvelope(Node sender, Node receiver, Envelope prevEnvelope) {
try {
String decryptedString = decryptWithPrivateKey(prevEnvelope.getEncryptedContent(), sender.getPrivateKey());
String[] parts = decryptedString.replaceAll("[\\[\\]]", "").split(",");
List<String> encryptedResults = new ArrayList<>();
int start = Integer.parseInt(parts[0].trim());
int end = Integer.parseInt(parts[1].trim());
Pairing pairing = PairingFactory.getPairing("a.properties");
Field G1 = pairing.getG1();
Field G2 = pairing.getG2();
Field Zr = pairing.getZr();
ComputationEnvelopeSubtask envelope = new ComputationEnvelopeSubtask(EnvelopeType.envcs, sender, receiver);
envelope.setPairings(pairing);
Element privateKey = Zr.newRandomElement().getImmutable();
Element g1 = G1.newRandomElement().getImmutable();
Element g2 = G2.newRandomElement().getImmutable();
Element publicKey = g2.duplicate().mulZn(privateKey);
envelope.setG1(g1);
envelope.setG2(g2);
envelope.setPublicKey(publicKey);
StringBuilder proofBuilder = new StringBuilder();
while (start <= end) {
double sqrt = Math.sqrt(start);
boolean primeOrNot = isPrime(start, sqrt);
if (primeOrNot) {
// Prepare the result string for encryption
String str = start + "->true";
List<String> encryptedChunks = encryptInChunks(str, receiver.getPublicKey());
encryptedResults.addAll(encryptedChunks);
byte[] numberBytes = Integer.toString(start).getBytes(StandardCharsets.UTF_8);
Element hashedNumber = G1.newElementFromHash(numberBytes, 0, numberBytes.length);
Element signature = hashedNumber.duplicate().mulZn(privateKey);
String encodedSignature = Base64.getEncoder().encodeToString(signature.toBytes());
String proof = start + "->Signature:" + encodedSignature;
if (proofBuilder.length() > 0) {
proofBuilder.append("|");
}
proofBuilder.append(proof);
}
start++;
}
String joinedResults = String.join("|", encryptedResults);
envelope.setEncryptedContent(joinedResults);
envelope.setProof(proofBuilder.toString().trim());
return envelope;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static List<String> encryptInChunks(String data, PublicKey publicKey) throws Exception {
List<String> chunks = new ArrayList<>();
int maxChunkSize = 245;
// Break data into smaller chunks
for (int i = 0; i < data.length(); i += maxChunkSize) {
int end = Math.min(data.length(), i + maxChunkSize);
String chunk = data.substring(i, end);
String encryptedChunk = encryptWithPublicKey(chunk, publicKey);
chunks.add(encryptedChunk);
}
return chunks;
}
public String getProof() {
return Proof;
}
public void setProof(String proof) {
Proof = proof;
}
public Element getPublicKey() {
return PublicKey;
}
public void setPublicKey(Element publicKey) {
PublicKey = publicKey;
}
}