-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputationEnvelopeTask.java
More file actions
45 lines (40 loc) · 1.73 KB
/
ComputationEnvelopeTask.java
File metadata and controls
45 lines (40 loc) · 1.73 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
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import javax.crypto.Cipher;
public class ComputationEnvelopeTask extends Envelope {
public ComputationEnvelopeTask(EnvelopeType envType, Node sentBy, Node receivedBy) {
super(envType, sentBy, receivedBy);
}
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 Envelope createEnvelope(Node Sender, Node Reciever,
List<Envelope> rRes) {
List<String> results = new ArrayList<>();
ArrayList<String> hashes = new ArrayList<>();
for (Envelope e : rRes) {
try {
String decryptedArr[] = e.getEncryptedContent().split(" ");
for (int i = 0; i < decryptedArr.length; i++) {
String decryptedContent = decryptWithPrivateKey(decryptedArr[i],
e.getReceivedBy().getPrivateKey());
results.add(decryptedContent);
}
hashes.add(e.calculateHash());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Envelope envelope = new Envelope(EnvelopeType.envcm, Sender, null);
return envelope;
}
}