Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import java.security.*;
- import java.security.spec.*;
- import javax.crypto.*;
- import javax.crypto.spec.*;
- import java.math.BigInteger;
- import java.nio.ByteBuffer;
- public class Client {
- public static void main(String [] args) throws Exception {
- String host = args[0];
- int port = Integer.parseInt(args[1]);
- String userId = args[2];
- // Connect to server
- Socket s = new Socket(host, port);
- DataInputStream din = new DataInputStream(s.getInputStream());
- DataOutputStream dout = new DataOutputStream(s.getOutputStream());
- ObjectInputStream oin = new ObjectInputStream(s.getInputStream());
- ObjectOutputStream oout = new ObjectOutputStream(s.getOutputStream());
- dout.writeUTF(userId);
- SecretKey desedeKey = keyAgreement(oin, oout,userId,din,dout);
- byte [] encryptedoutput = new byte[din.readInt()];
- din.readFully(encryptedoutput);
- Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
- cipher.init(Cipher.DECRYPT_MODE, desedeKey);
- String fileval = new String(cipher.doFinal(encryptedoutput));
- System.out.println(fileval);
- }
- // Stage 2: manual DH
- private static SecretKey keyAgreement(ObjectInputStream oin, ObjectOutputStream oout,String userId,DataInputStream in,DataOutputStream out) throws Exception {
- String skip1024String =
- "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88B31C7C5B2D8EF6"+
- "F3C923C043F0A55B188D8EBB558CB85D38D334FD7C175743A31D186CDE33212C"+
- "B52AFF3CE1B1294018118D7C84A70A72D686C40319C807297ACA950CD9969FAB"+
- "D00A509B0246D3083D66A45D419F9C7CBD894B221926BAABA25EC355E92F78C7";
- BigInteger base = BigInteger.valueOf(2);
- BigInteger modulus = new BigInteger(skip1024String, 16);
- BigInteger b;
- do {
- b = new BigInteger(1024, new Random());
- }while(b.bitLength() < 1024);
- BigInteger eb = base.modPow(b, modulus);
- BigInteger ea = (BigInteger)oin.readObject();
- oout.writeObject(eb);
- BigInteger key = ea.modPow(b, modulus);
- byte[] keyBytes = key.toByteArray();
- SecretKeyFactory desedeFactory = SecretKeyFactory.getInstance("DESede");
- KeySpec spec = new DESedeKeySpec(keyBytes);
- SecretKey finalkey = desedeFactory.generateSecret(spec);
- ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(userId+".prv"));
- PrivateKey privateKey = (PrivateKey) keyIn.readObject();
- keyIn.close();
- Signature sig = Signature.getInstance("DSA");
- sig.initSign(privateKey);
- sig.update(eb.toByteArray());
- sig.update(ea.toByteArray());
- byte[] signature = sig.sign();
- Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE, finalkey);
- byte[] output = cipher.doFinal(signature);
- out.writeInt(output.length);
- out.write(output);
- byte [] encryptedServerSignature = new byte[in.readInt()];
- in.readFully(encryptedServerSignature);
- Cipher dcipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
- dcipher.init(Cipher.DECRYPT_MODE, finalkey);
- byte [] signatureServer = dcipher.doFinal(encryptedServerSignature);
- ObjectInputStream keyInServer = new ObjectInputStream(new FileInputStream("server.pub"));
- PublicKey publicKey = (PublicKey) keyInServer.readObject();
- keyInServer.close();
- Signature sigServer = Signature.getInstance("DSA");
- sigServer.initVerify(publicKey);
- sigServer.update(ea.toByteArray());
- sigServer.update(eb.toByteArray());
- if (sigServer.verify(signatureServer))
- System.out.println("Client logged in");
- else {
- System.out.println("Client failed to log in");
- System.exit(-1);
- }
- return finalkey;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement