-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommunication.java
More file actions
78 lines (69 loc) · 2.75 KB
/
Communication.java
File metadata and controls
78 lines (69 loc) · 2.75 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.util.*;
import java.math.BigInteger;
import java.nio.file.*;
import java.lang.*;
//Class that handles the functions needed for the client and server to communicate with eachother.
class Communication {
//The parsed field values
public static byte[] message;
public static byte[] signature;
public static byte[] mac;
//Converts a message into the respective message, signature, mac(checksum) fields.
public static void parse(byte[] com) {
try {
String str = new String(com);
String[] portions = str.split(";;;");
if (portions.length > 3) {
System.out.println("The Communication object contains incorrect coding");
}
if (portions[0].equals("")) {
System.out.println("The Communication object does not contain the message");
} else {
message = Base64.getDecoder().decode(portions[0].getBytes());
if (portions.length > 1) {
signature = Base64.getDecoder().decode(portions[1].getBytes());
}
if (portions.length > 2) {
mac = Base64.getDecoder().decode(portions[2].getBytes());
}
}
} catch (Exception e) {
System.out.println("Parse error");
}
}
//Formats the message into a string using ;;; as a delimiter between message, signature and mac.
public byte[] format(byte[] message, byte[] signature, byte[] mac) {
String delimeter = ";;;";
byte[] encodedMessage = Base64.getEncoder().encode(message);
byte[] encodedSignature = Base64.getEncoder().encode(signature);
byte[] encodedMac = Base64.getEncoder().encode(mac);
String messageString = new String(encodedMessage);
String sigString = new String(encodedSignature);
String macString = new String(encodedMac);
String communication = messageString + delimeter + sigString + delimeter + macString;
return communication.getBytes();
}
//1. Decrypts the message if confidentiality was chosen.
//2. Verifys the signature sent with the message if authentication was chosen.
//3. Compares the computed checksum with the recieved checksum if integrity was chosen.
public static String handleMessage(byte[] information, Path path, Cryptography crypto, byte[] key,
Security security) {
parse(information);
byte[] msg = crypto.decrypt(message, key, security.confidentiality);
if (!crypto.verify(msg, signature, path, security.authentication)) {
System.out.println("Authentication failed, signature from message does not match");
}
byte[] macActual = crypto.generateMAC(msg, key, Security.integrity);
byte[] macExpected = mac;
if (!crypto.compareMAC(macActual, macExpected, security.integrity)) {
System.out.println("Integrity failed, checksum of message does not match expected!");
}
String s = "";
try {
s = new String(msg);
} catch (Exception e) {
System.out.println("byte to string conversion error");
}
return s;
}
}