· 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.InvalidAlgorithmParameterException;
7import java.security.InvalidKeyException;
8import java.security.MessageDigest;
9import java.security.NoSuchAlgorithmException;
10
11public class Server{
12 public static void main(String args[]){
13 int number=0;
14 try{
15 int serverPort = 4567;
16 ServerSocket listenSocket = new ServerSocket(serverPort);
17 while(true) {
18 Socket clientSocket = listenSocket.accept();
19 new Connection(clientSocket,number);
20 number++;
21 }
22 } catch(IOException e) {
23 System.out.println("Listen:" + e.getMessage());
24 }
25 }
26}
27
28class Connection extends Thread {
29 DataInputStream in;
30 DataOutputStream out;
31 Socket clientSocket;
32 int number;
33
34 public Connection (Socket aClientSocket,int clientNumber) {
35 try{
36 number=clientNumber;
37 clientSocket = aClientSocket;
38 in = new DataInputStream(clientSocket.getInputStream());
39 out = new DataOutputStream(clientSocket.getOutputStream());
40 this.start();
41 }catch(IOException e){System.out.println("Connection:" + e.getMessage());}
42 }
43
44 public void run(){
45 try{
46 while(true){
47 //key
48 byte [] key = {1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4};
49
50 //hash function and new keys
51 MessageDigest md = MessageDigest.getInstance("SHA-256");
52
53 byte [] byte1 = {1};
54 byte [] aux1 = concat(key, byte1);
55 byte [] hash1= md.digest(aux1);
56 SecretKey k1 = new SecretKeySpec(hash1, 0, hash1.length, "AES");
57
58 byte [] byte2 = {2};
59 byte [] aux2 = concat(key, byte2);
60 byte [] hash2= md.digest(aux2);
61 SecretKey k2 = new SecretKeySpec(hash2, 0, hash2.length, "AES");
62
63 //message authentication code
64 Mac mac = Mac.getInstance("HmacSHA256");
65 mac.init(k2);
66 int nSeq = 0;
67
68 //nonce - receives IV in plaintext
69 byte[] nonce=new byte[16];
70 in.read(nonce);
71 IvParameterSpec iv = new IvParameterSpec(nonce);
72
73 //D(m, k1) = c - só se decifra se a autenticação do mac correr bem
74 Cipher c = Cipher.getInstance("AES/CTR/PKCS5Padding");
75 c.init(Cipher.DECRYPT_MODE, k1, iv);
76 int text;
77
78 while(true){
79 String ciphertext = in.readUTF();
80 System.out.println(ciphertext);
81
82 String t = in.readUTF();
83 System.out.println(t);
84 }
85
86
87 }
88 }
89 catch (NoSuchPaddingException e) {
90 e.printStackTrace();
91 }
92 catch (NoSuchAlgorithmException e) {
93 e.printStackTrace();
94 }
95 catch (InvalidKeyException e) {
96 e.printStackTrace();
97 }
98 catch (InvalidAlgorithmParameterException e) {
99 e.printStackTrace();
100 }
101 catch (IOException e) {
102 e.printStackTrace();
103 }
104 }
105
106 private byte [] concat(byte[] first, byte[] second) throws IOException {
107 ByteArrayOutputStream baos = new ByteArrayOutputStream();
108 baos.write(first);
109 baos.write(second);
110 byte [] result = baos.toByteArray();
111 baos.reset();
112 return result;
113 }
114
115 //https://stackoverflow.com/questions/2183240/java-integer-to-byte-array
116 public static final byte[] intToByteArray(int value) {
117 return new byte[] {
118 (byte)(value >>> 24),
119 (byte)(value >>> 16),
120 (byte)(value >>> 8),
121 (byte)value};
122 }
123}