www.pudn.com > encryption.rar > AuthClient.java


/*
Christoforos Pirillos @ Villanova University - May 1999
based on code from the book "Java Network Programming" by Hughes
*/

package encryption;

import java.io.*;
import java.net.*;

/** This class is used by the remote side of the connection.*/

public class AuthClient {

protected InputStream i;
protected OutputStream o;
protected Socket s;

/** Accepts the server and user information. it automatically connects to
the server and authenticates the client*/

public AuthClient (String server, int port, String user, String password)
		throws IOException {
	s=new Socket (server, port);
	OutputStream out=s.getOutputStream();
	new DataOutputStream(out).writeUTF(user);
	out.flush();
	InputStream in = s.getInputStream();
	long encKey = new DataInputStream(in).readLong();
	DES keyDes = new DES (Password.keyFromPassword(password));
	long key=keyDes.decrypt(encKey);
	if (!DES.isParity (key)) {
		s.close();
		throw new AuthException ("Incorrect Password.");
	}
	byte[] iv=new byte[8];
	Crypt.longToBytes (Password.nextKeyFromPassword (password),iv,0);
	Cipher sessionDES = new CBCCipher (new DES (key),iv);
	long challenge = System.currentTimeMillis();
	byte[] temp = new byte[8];
	Crypt.longToBytes (challenge, temp, 0);
	out.write(sessionDES.encipher (temp));
	new DataInputStream(in).readFully(temp);
	long response = Crypt.bytesToLong (sessionDES.decipher(temp),0);
	if (response != challenge+1)
		throw new AuthException ("Challenge/response failed.");
	Crypt.longToBytes (response+1, temp, 0);
	out.write(sessionDES.encipher(temp));
	o = new CipherOutputStream (out, sessionDES);
	i = new CipherInputStream (in, sessionDES);
}
/** Returns the encrypted stream from the server*/
public InputStream getInputStream () {
	return i;
}
/** Returns the encrypted stream from the server*/
public OutputStream getOutputStream () {
	return o;
}
/** Returns the encrypted Socket from the server*/
public Socket getSocket() {
	return s;
}

/** for testing purposes*/

public static void main (String args[]) throws IOException{
	AuthClient conn = new AuthClient
("localhost",7000,args[0],args[1]);
 DataInputStream in =new DataInputStream(conn.getInputStream());
 byte[] b = new byte[8];
 int c;
	in.readFully(b);
		System.out.println(b.toString());
}
}