· 6 years ago · Feb 22, 2019, 03:44 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 Client {
12
13 public static void main(String [] args) throws Exception {
14
15 String host = args[0];
16 int port = Integer.parseInt(args[1]);
17 String userId = args[2];
18
19 // Connect to server
20 Socket s = new Socket(host, port);
21 DataInputStream din = new DataInputStream(s.getInputStream());
22 DataOutputStream dout = new DataOutputStream(s.getOutputStream());
23 ObjectInputStream oin = new ObjectInputStream(s.getInputStream());
24 ObjectOutputStream oout = new ObjectOutputStream(s.getOutputStream());
25
26 dout.writeUTF(userId);
27 SecretKey desedeKey = keyAgreement(oin, oout,userId,din,dout);
28
29 byte [] encryptedoutput = new byte[din.readInt()];
30 din.readFully(encryptedoutput);
31
32 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
33 cipher.init(Cipher.DECRYPT_MODE, desedeKey);
34 String fileval = new String(cipher.doFinal(encryptedoutput));
35
36 System.out.println(fileval);
37
38 }
39
40
41
42
43 // Stage 2: manual DH
44 private static SecretKey keyAgreement(ObjectInputStream oin, ObjectOutputStream oout,String userId,DataInputStream in,DataOutputStream out) throws Exception {
45
46 String skip1024String =
47 "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88B31C7C5B2D8EF6"+
48 "F3C923C043F0A55B188D8EBB558CB85D38D334FD7C175743A31D186CDE33212C"+
49 "B52AFF3CE1B1294018118D7C84A70A72D686C40319C807297ACA950CD9969FAB"+
50 "D00A509B0246D3083D66A45D419F9C7CBD894B221926BAABA25EC355E92F78C7";
51
52 BigInteger base = BigInteger.valueOf(2);
53 BigInteger modulus = new BigInteger(skip1024String, 16);
54
55 BigInteger b;
56 do {
57 b = new BigInteger(1024, new Random());
58 }while(b.bitLength() < 1024);
59
60 BigInteger eb = base.modPow(b, modulus);
61 BigInteger ea = (BigInteger)oin.readObject();
62 oout.writeObject(eb);
63
64 BigInteger key = ea.modPow(b, modulus);
65
66 byte[] keyBytes = key.toByteArray();
67 SecretKeyFactory desedeFactory = SecretKeyFactory.getInstance("DESede");
68 KeySpec spec = new DESedeKeySpec(keyBytes);
69
70 SecretKey finalkey = desedeFactory.generateSecret(spec);
71
72 ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(userId+".prv"));
73 PrivateKey privateKey = (PrivateKey) keyIn.readObject();
74 keyIn.close();
75
76 Signature sig = Signature.getInstance("DSA");
77 sig.initSign(privateKey);
78 sig.update(eb.toByteArray());
79 sig.update(ea.toByteArray());
80 byte[] signature = sig.sign();
81
82 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
83 cipher.init(Cipher.ENCRYPT_MODE, finalkey);
84 byte[] output = cipher.doFinal(signature);
85 out.writeInt(output.length);
86 out.write(output);
87
88 byte [] encryptedServerSignature = new byte[in.readInt()];
89 in.readFully(encryptedServerSignature);
90
91 Cipher dcipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
92 dcipher.init(Cipher.DECRYPT_MODE, finalkey);
93 byte [] signatureServer = dcipher.doFinal(encryptedServerSignature);
94
95 ObjectInputStream keyInServer = new ObjectInputStream(new FileInputStream("server.pub"));
96 PublicKey publicKey = (PublicKey) keyInServer.readObject();
97 keyInServer.close();
98
99 Signature sigServer = Signature.getInstance("DSA");
100 sigServer.initVerify(publicKey);
101 sigServer.update(ea.toByteArray());
102 sigServer.update(eb.toByteArray());
103
104 if (sigServer.verify(signatureServer))
105 System.out.println("Client logged in");
106 else {
107 System.out.println("Client failed to log in");
108 System.exit(-1);
109 }
110
111 return finalkey;
112 }
113
114}