· 6 years ago · Feb 22, 2019, 03:46 PM
1import java.io.*;
2import java.net.*;
3import java.util.*;
4import java.security.*;
5import java.security.spec.*;
6import javax.crypto.*;
7import javax.crypto.spec.*;
8import java.math.BigInteger;
9import java.nio.ByteBuffer;
10
11public class Server {
12
13 static ArrayList<Double> pastRand;
14
15 public static void main(String args[]) throws Exception {
16
17 // Listen for connections
18 int port = Integer.parseInt(args[0]);
19 ServerSocket ss = new ServerSocket(port);
20
21 // infinite loop
22 while(true) {
23
24 try {
25
26 Socket s = ss.accept();
27 DataInputStream din = new DataInputStream(s.getInputStream());
28 DataOutputStream dout = new DataOutputStream(s.getOutputStream());
29 ObjectOutputStream oout = new ObjectOutputStream(s.getOutputStream());
30 ObjectInputStream oin = new ObjectInputStream(s.getInputStream());
31
32 String userid = din.readUTF();
33
34 SecretKey desedeKey = keyAgreement(oin, oout,userid,dout,din);
35
36 BufferedReader br = new BufferedReader(new FileReader(userid));
37 String message = br.readLine();
38
39 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
40 cipher.init(Cipher.ENCRYPT_MODE, desedeKey);
41 byte[] output = cipher.doFinal(message.getBytes("UTF8"));
42 dout.writeInt(output.length);
43 dout.write(output);
44
45
46 }
47 catch (EOFException e) {
48 System.err.println("client disconnected.");
49 continue;
50 }
51 }
52 }
53
54
55
56
57
58 // Stage 2: manual DH
59
60 private static SecretKey keyAgreement(ObjectInputStream oin, ObjectOutputStream oout,String userId,DataOutputStream dout,DataInputStream in) throws Exception {
61
62 String skip1024String =
63 "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88B31C7C5B2D8EF6"+
64 "F3C923C043F0A55B188D8EBB558CB85D38D334FD7C175743A31D186CDE33212C"+
65 "B52AFF3CE1B1294018118D7C84A70A72D686C40319C807297ACA950CD9969FAB"+
66 "D00A509B0246D3083D66A45D419F9C7CBD894B221926BAABA25EC355E92F78C7";
67 BigInteger base = BigInteger.valueOf(2);
68 BigInteger modulus = new BigInteger(skip1024String, 16);
69
70 BigInteger a;
71 do {
72 a = new BigInteger(1024, new Random());
73 }while(a.bitLength() < 1024);
74
75 BigInteger ea = base.modPow(a, modulus);
76 oout.writeObject(ea);
77 BigInteger eb = (BigInteger)oin.readObject();
78 BigInteger key = eb.modPow(a, modulus);
79
80 byte[] keyBytes = key.toByteArray();
81 SecretKeyFactory desedeFactory = SecretKeyFactory.getInstance("DESede");
82 KeySpec spec = new DESedeKeySpec(keyBytes);
83
84 SecretKey finalkey = desedeFactory.generateSecret(spec);
85
86 byte [] encryptedsignature = new byte[in.readInt()];
87 in.readFully(encryptedsignature);
88
89 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
90 cipher.init(Cipher.DECRYPT_MODE, finalkey);
91 byte [] signature = cipher.doFinal(encryptedsignature);
92
93 ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(userId+".pub"));
94 PublicKey publicKey = (PublicKey) keyIn.readObject();
95 keyIn.close();
96
97 Signature sig = Signature.getInstance("DSA");
98 sig.initVerify(publicKey);
99 sig.update(eb.toByteArray());
100 sig.update(ea.toByteArray());
101
102 if (sig.verify(signature))
103 System.out.println("Client logged in");
104 else {
105 System.out.println("Client failed to log in");
106 System.exit(-1);
107 }
108
109
110 ObjectInputStream keyInServer = new ObjectInputStream(new FileInputStream("server.prv"));
111 PrivateKey privateKey = (PrivateKey) keyInServer.readObject();
112 keyInServer.close();
113
114 Signature sigServer = Signature.getInstance("DSA");
115 sigServer.initSign(privateKey);
116 sigServer.update(ea.toByteArray());
117 sigServer.update(eb.toByteArray());
118 byte[] signatureServer = sigServer.sign();
119
120 Cipher ecipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
121 ecipher.init(Cipher.ENCRYPT_MODE, finalkey);
122 byte[] output = ecipher.doFinal(signatureServer);
123 dout.writeInt(output.length);
124 dout.write(output);
125 return finalkey;
126 }
127
128
129
130
131
132}