· 8 years ago · Jan 27, 2018, 11:02 AM
1case 0:
2 {
3 String command = c.ca.login(c.ca.uuid);
4 c.ca.sendCommandToServer(command, c.ca.sessionKey);
5 JsonObject obj = c.ca.readResult();
6 c.ca.id = obj.get("result").getAsInt();
7 System.out.println(c.ca.id);
8 break;
9 }
10 case 1:
11 {
12 String command = c.ca.createCommand(keysGen.savePublicKey(c.ca.publicKey),
13 keysGen.savePublicKey(c.ccKeyPair.getPubKey()));
14 c.ca.sendCommandToServer(command, c.ca.sessionKey);
15 JsonObject obj = c.ca.readResult();
16 c.ca.id = obj.get("result").getAsInt();
17 System.out.println(obj.toString());
18 break;
19 }
20 case 2:
21 {
22 String command = c.ca.listCommand();
23 c.ca.sendCommandToServer(command, c.ca.sessionKey);
24 JsonObject obj = c.ca.readResult();
25 JsonArray result = obj.get("data").getAsJsonArray();
26
27 for(JsonElement cli : result) {
28 JsonObject temp = cli.getAsJsonObject();
29 int id = temp.get("id").getAsInt();
30 c.ca.clients.put(id, temp);
31 }
32 c.ca.clients.keySet().stream().forEach((i) -> {
33 System.out.println(c.ca.clients.get(i));
34 });
35 break;
36 }
37 case 3:
38 {
39 String command = c.ca.newCommand(c.ca.id);
40 c.ca.sendCommandToServer(command, c.ca.sessionKey);
41 JsonObject obj = c.ca.readResult();
42 JsonArray result = obj.get("result").getAsJsonArray();
43 result.forEach(System.out::println);
44 break;
45 }
46 case 4:
47 {
48 String command = c.ca.allCommand(c.ca.id);
49 c.ca.sendCommandToServer(command, c.ca.sessionKey);
50 JsonObject obj = c.ca.readResult();
51 JsonArray result = obj.get("result").getAsJsonArray();
52 JsonArray receivedMsg = result.get(0).getAsJsonArray();
53 JsonArray sentMsg = result.get(1).getAsJsonArray();
54 System.out.println("Received:");
55 for(JsonElement e : receivedMsg) {
56 System.out.print(e.getAsString() + " ");
57 }
58 System.out.println("Sent:");
59 for(JsonElement e : sentMsg) {
60 System.out.print(e.getAsString() + " ");
61 }
62 break;
63 }
64 case 5:
65 {
66 String message = "arroz";
67 SymmetricKeyCryptography secretGen = new SymmetricKeyCryptography(32, "AES");
68 SecretKeySpec secretKey = secretGen.getSecretKey();
69 String encryptedMessage = sym.encryptText(message, secretKey);
70 String duplicate = sym.encryptText(message, c.ca.personalKey);
71 String secretKeyString = sym.keyToString(secretKey);
72
73 String command = c.ca.sendCommand(1, 1, encryptedMessage, duplicate, cryp.encryptText(secretKeyString, c.ca.publicKey));
74
75 c.ca.sendCommandToServer(command, c.ca.sessionKey);
76 JsonObject obj = c.ca.readResult();
77 break;
78 }
79 case 6:
80 {
81 String command = c.ca.recvCommand(c.ca.id, "1_1");
82 c.ca.sendCommandToServer(command, c.ca.sessionKey);
83 JsonObject obj = c.ca.readResult();
84 JsonArray result = obj.get("result").getAsJsonArray();
85 System.out.println("Sender: " + result.get(0).getAsString());
86 String key = result.get(2).getAsString();
87 String decodedKey = cryp.decryptText(key, c.ca.privateKey);
88 SecretKeySpec secretKey = sym.stringToKey(decodedKey);
89 System.out.println("Msg: " + sym.decryptText(result.get(1).getAsString(), secretKey));
90
91 break;
92 }
93 case 7:
94 {
95 String msgID = "_1_1";
96 String id = msgID.substring(1, msgID.length());
97 byte[] msg = (Utils.getFileInBytes(new File("receipts/" + c.ca.id + "/" + id)));
98 String decrypted = sym.decryptText(new String(msg), c.ca.personalKey);
99 System.out.println("Receipt: " + decrypted);
100 String sig = Base64.getEncoder().encodeToString(
101 sigGen.sign(decrypted.getBytes("UTF-8"), c.ca.ccKeyPair.getPrivKey()));
102 String command = c.ca.receiptCommand(1, "_1_1", sig);
103 c.ca.sendCommandToServer(command, c.ca.sessionKey);
104 break;
105 }
106 case 8:
107 {
108 String msgID = "_1_1";
109 String id = msgID.substring(1, msgID.length());
110 String command = c.ca.sentMessageCommand(c.ca.id, id);
111 c.ca.sendCommandToServer(command, c.ca.sessionKey);
112 JsonObject obj = c.ca.readResult();
113 String result = obj.get("result").getAsString();
114 String decrypted = sym.decryptText(result, c.ca.personalKey);
115 System.out.println("Sent message: " + decrypted);
116 command = c.ca.statusCommand(1, "1_1");
117 c.ca.sendCommandToServer(command, c.ca.sessionKey);
118 obj = c.ca.readResult();
119 JsonObject temp = obj.get("result").getAsJsonObject();
120 String msg = temp.get("msg").getAsString();
121 JsonArray receipts = temp.get("receipts").getAsJsonArray();
122 System.out.println(msg);
123 receipts.forEach(System.out::println);
124
125 break;
126 }
127 case 9:
128 {
129 // quit
130 }