· 5 years ago · Mar 23, 2020, 09:34 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 String text = "";
52 InputStreamReader input = new InputStreamReader(System.in);
53 BufferedReader reader = new BufferedReader(input);
54 String texto="";
55 while (true) {
56
57 try {
58 texto = reader.readLine();
59 } catch (Exception e) {
60
61 }
62
63 out.writeUTF("mensagem que vai cifrada");
64 out.writeUTF("mac da mensagem");
65
66 }
67 }
68 } catch (UnknownHostException e) {
69 System.out.println("Sock:" + e.getMessage());
70 } catch (EOFException e) {
71 System.out.println("EOF:" + e.getMessage());
72 } catch (IOException e) {
73 System.out.println("IO:" + e.getMessage());
74 } catch (NoSuchPaddingException e) {
75 e.printStackTrace();
76 } catch (NoSuchAlgorithmException e) {
77 e.printStackTrace();
78 } catch (InvalidAlgorithmParameterException e) {
79 e.printStackTrace();
80 } catch (InvalidKeyException e) {
81 e.printStackTrace();
82
83 } finally {
84 if (s != null)
85 try {
86 s.close();
87 } catch (IOException e) {
88 System.out.println("close:" + e.getMessage());
89 }
90 }
91 }
92
93 private static byte [] concat(byte[] first, byte[] second) throws IOException {
94 ByteArrayOutputStream baos = new ByteArrayOutputStream();
95 baos.write(first);
96 baos.write(second);
97 byte [] result = baos.toByteArray();
98 baos.reset();
99 return result;
100 }
101
102 //https://stackoverflow.com/questions/2183240/java-integer-to-byte-array
103 public static final byte[] intToByteArray(int value) {
104 return new byte[]{
105 (byte)(value >>> 24),
106 (byte)(value >>> 16),
107 (byte)(value >>> 8),
108 (byte)value};
109 }
110}