· 8 years ago · Jan 10, 2018, 11:36 PM
1 private void startSecureCon(){
2
3 /*
4 1. C (plain): startsecure
5 2. S (plain): ok <component-id>
6 3. C (RSA): ok <client-challenge> <secret-key> <iv>
7 4. S (AES): ok <client-challenge>
8 5. C (AES): ok
9 */
10
11
12
13 // step 2
14 out.println("ok " + this.commponentId);
15
16 /* The client challenges the server to prove its identity by sending a challenge.
17 The message containing the challenge is encrypted with the server’s public key.
18 The server is then expected to return the decrypted challenge back to the client. */
19
20 /* The server uses its private key to decrypt the message containing the client challenge and AES initialization parameters.
21 Using the AES cipher initialized by the client, the server encrypts the return message
22 (which can only be decrypted by the client that initialized the cipher). */
23
24 // step 4 - decription
25 try {
26 String request = in.readLine();
27 System.out.println(request);
28
29 byte[] decoded = Base64.decode(request.getBytes("UTF-8"));
30 System.out.println("Decoded: ");
31 for (byte d :
32 decoded) {
33 System.out.print(d);
34 }
35
36
37 Cipher rsa = Cipher.getInstance("RSA/NONE/OAEPWithSHA256AndMGF1Padding");
38 System.out.println("Key je : " + Keys.readPrivateKey(new File("keys/server/mailbox-earth-planet")));
39 rsa.init(Cipher.DECRYPT_MODE, Keys.readPrivateKey(new File("keys/server/mailbox-earth-planet")));
40
41 System.out.println("Tu sam.");
42
43 byte[] b = rsa.doFinal(decoded);
44 System.out.println("Decrypted: ");
45 for (byte d :
46 b) {
47 System.out.print(d);
48 }
49
50// secretkey; iv;
51
52 Cipher aes = Cipher.getInstance("AES/CTR/NoPadding");
53
54
55
56
57 } catch (IOException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e ) {
58 e.printStackTrace();
59 }
60
61
62// PrivateKey privKey = Keys.readPrivateKey(new File("./keys/server/mailbox-earth-planet"));
63
64// else out.println("error");
65// } catch (IOException e) {
66// e.printStackTrace();
67// }
68
69 /*
70 3. C (RSA): ok <client-challenge> <secret-key> <iv>
71 4. S (AES): ok <client-challenge>
72 5. C (AES): ok
73 */
74
75 // If it fails immediately terminate the connection without sending an error
76 }