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 Server {
- static ArrayList<Double> pastRand;
- public static void main(String args[]) throws Exception {
- // Listen for connections
- int port = Integer.parseInt(args[0]);
- ServerSocket ss = new ServerSocket(port);
- // infinite loop
- while(true) {
- try {
- Socket s = ss.accept();
- DataInputStream din = new DataInputStream(s.getInputStream());
- DataOutputStream dout = new DataOutputStream(s.getOutputStream());
- ObjectOutputStream oout = new ObjectOutputStream(s.getOutputStream());
- ObjectInputStream oin = new ObjectInputStream(s.getInputStream());
- String userid = din.readUTF();
- SecretKey desedeKey = keyAgreement(oin, oout,userid,dout,din);
- BufferedReader br = new BufferedReader(new FileReader(userid));
- String message = br.readLine();
- Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE, desedeKey);
- byte[] output = cipher.doFinal(message.getBytes("UTF8"));
- dout.writeInt(output.length);
- dout.write(output);
- }
- catch (EOFException e) {
- System.err.println("client disconnected.");
- continue;
- }
- }
- }
- // Stage 2: manual DH
- private static SecretKey keyAgreement(ObjectInputStream oin, ObjectOutputStream oout,String userId,DataOutputStream dout,DataInputStream in) throws Exception {
- String skip1024String =
- "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88B31C7C5B2D8EF6"+
- "F3C923C043F0A55B188D8EBB558CB85D38D334FD7C175743A31D186CDE33212C"+
- "B52AFF3CE1B1294018118D7C84A70A72D686C40319C807297ACA950CD9969FAB"+
- "D00A509B0246D3083D66A45D419F9C7CBD894B221926BAABA25EC355E92F78C7";
- BigInteger base = BigInteger.valueOf(2);
- BigInteger modulus = new BigInteger(skip1024String, 16);
- BigInteger a;
- do {
- a = new BigInteger(1024, new Random());
- }while(a.bitLength() < 1024);
- BigInteger ea = base.modPow(a, modulus);
- oout.writeObject(ea);
- BigInteger eb = (BigInteger)oin.readObject();
- BigInteger key = eb.modPow(a, modulus);
- byte[] keyBytes = key.toByteArray();
- SecretKeyFactory desedeFactory = SecretKeyFactory.getInstance("DESede");
- KeySpec spec = new DESedeKeySpec(keyBytes);
- SecretKey finalkey = desedeFactory.generateSecret(spec);
- byte [] encryptedsignature = new byte[in.readInt()];
- in.readFully(encryptedsignature);
- Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
- cipher.init(Cipher.DECRYPT_MODE, finalkey);
- byte [] signature = cipher.doFinal(encryptedsignature);
- ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(userId+".pub"));
- PublicKey publicKey = (PublicKey) keyIn.readObject();
- keyIn.close();
- Signature sig = Signature.getInstance("DSA");
- sig.initVerify(publicKey);
- sig.update(eb.toByteArray());
- sig.update(ea.toByteArray());
- if (sig.verify(signature))
- System.out.println("Client logged in");
- else {
- System.out.println("Client failed to log in");
- System.exit(-1);
- }
- ObjectInputStream keyInServer = new ObjectInputStream(new FileInputStream("server.prv"));
- PrivateKey privateKey = (PrivateKey) keyInServer.readObject();
- keyInServer.close();
- Signature sigServer = Signature.getInstance("DSA");
- sigServer.initSign(privateKey);
- sigServer.update(ea.toByteArray());
- sigServer.update(eb.toByteArray());
- byte[] signatureServer = sigServer.sign();
- Cipher ecipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
- ecipher.init(Cipher.ENCRYPT_MODE, finalkey);
- byte[] output = ecipher.doFinal(signatureServer);
- dout.writeInt(output.length);
- dout.write(output);
- return finalkey;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement