· 5 years ago · Mar 24, 2020, 06:24 PM
1import java.net.*;
2import java.io.*;
3import javax.crypto.*;
4import javax.crypto.spec.IvParameterSpec;
5import javax.crypto.spec.SecretKeySpec;
6import java.security.*;
7
8public class Client {
9 public static void main(String args[]) {
10
11 Socket s = null;
12 int serversocket = 4567;
13 try {
14 s = new Socket("127.0.0.1", serversocket);
15
16 DataInputStream in = new DataInputStream(s.getInputStream());
17 DataOutputStream out = new DataOutputStream(s.getOutputStream());
18
19 while (true){
20 //key
21 byte [] key = {1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4};
22
23 //hash function and new keys
24 MessageDigest md = MessageDigest.getInstance("SHA-256");
25
26 byte [] byte1 = {1};
27 byte [] aux1 = concat(key, byte1);
28 byte [] hash1= md.digest(aux1);
29 SecretKey k1 = new SecretKeySpec(hash1, 0, hash1.length, "AES");
30
31 byte [] byte2 = {2};
32 byte [] aux2 = concat(key, byte2);
33 byte [] hash2= md.digest(aux2);
34 SecretKey k2 = new SecretKeySpec(hash2, 0, hash2.length, "HMAC");
35
36 //message authentication code
37 Mac mac = Mac.getInstance("HmacSHA256");
38 mac.init(k2);
39 int nSeq = 0;
40
41 //nonce - sends the bytes in plaintext so that the server can create the same IV
42 byte[] bytesForIV=new byte[16];
43 SecureRandom sr = new SecureRandom();
44 sr.nextBytes(bytesForIV);
45 IvParameterSpec iv = new IvParameterSpec(bytesForIV);
46 out.write(bytesForIV);
47
48 //E(m, k1) = c
49 Cipher c = Cipher.getInstance("AES/CTR/PKCS5Padding");
50 c.init(Cipher.ENCRYPT_MODE, k1, iv);
51
52
53 String text = "";
54 InputStreamReader input = new InputStreamReader(System.in);
55 BufferedReader reader = new BufferedReader(input);
56
57 while (true) {
58
59 try {
60 text = reader.readLine();
61
62
63 byte [] enc = c.doFinal(text.getBytes());
64 byte [] nSeqByte = intToByteArray(nSeq);
65
66 byte [] authTagAux = concat(enc, nSeqByte);
67 byte [] authTag = mac.doFinal(authTagAux);
68
69 // pacote=[tamanho da ciphertext(4 bytes)][mac(32 bytes)][ciphertext]
70 byte[] pacote=concat(intToByteArray(enc.length),concat(authTag,enc));
71 out.write(pacote);
72 nSeq++;
73
74 }
75 catch (Exception e) {
76
77 }
78
79
80 }
81 }
82 } catch (UnknownHostException e) {
83 System.out.println("Sock:" + e.getMessage());
84 } catch (EOFException e) {
85 System.out.println("EOF:" + e.getMessage());
86 } catch (IOException e) {
87 System.out.println("IO:" + e.getMessage());
88 } catch (NoSuchPaddingException e) {
89 e.printStackTrace();
90 } catch (NoSuchAlgorithmException e) {
91 e.printStackTrace();
92 } catch (InvalidAlgorithmParameterException e) {
93 e.printStackTrace();
94 } catch (InvalidKeyException e) {
95 e.printStackTrace();
96
97 } finally {
98 if (s != null)
99 try {
100 s.close();
101 } catch (IOException e) {
102 System.out.println("close:" + e.getMessage());
103 }
104 }
105 }
106
107 private static byte [] concat(byte[] first, byte[] second) throws IOException {
108 ByteArrayOutputStream baos = new ByteArrayOutputStream();
109 baos.write(first);
110 baos.write(second);
111 byte [] result = baos.toByteArray();
112 baos.reset();
113 return result;
114 }
115
116 //https://stackoverflow.com/questions/2183240/java-integer-to-byte-array
117 public static final byte[] intToByteArray(int value) {
118 return new byte[]{
119 (byte)(value >>> 24),
120 (byte)(value >>> 16),
121 (byte)(value >>> 8),
122 (byte)value};
123 }
124}