Java Security

Ogg-Vorbis format, 16Mbytes MP3, 16Mbytes WAV format, 175Mbytes

Security Manager

Security Policy

Code Permissions

Sample permissions

Keystores

Signing files


Java Cryptography Extension (JCE)

Some private key algorithms

Some public/private key algorithms

Class Cipher

Example

The echo server using DES

import java.io.*;
import java.net.*;
import javax.crypto.spec.*;
import javax.crypto.*;
import java.security.*;
import java.security.spec.*;

public class EchoServer {
    
    public static int MYECHOPORT = 8189;

    public static void main(String argv[]) {
	ServerSocket s = null;
	try {
	    s = new ServerSocket(MYECHOPORT);
	} catch(IOException e) {
	    System.out.println(e);
	    System.exit(1);
	}
	while (true) {
	    Socket incoming = null;
	    try {
		incoming = s.accept();
	    } catch(IOException e) {
		System.out.println(e);
		continue;
	    }

	    /*
	    try {
		incoming.setSoTimeout(10000); // 10 seconds
	    } catch(SocketException e) {
		e.printStackTrace();
	    }
	    */

	    try {
		handleSocket(incoming);
	    } catch(InterruptedIOException e) {
		System.out.println("Time expired " + e);
	    } catch(IOException e) {
		System.out.println(e);
	    }

	    try {
		incoming.close();
	    } catch(IOException e) {
		// ignore
	    }
	}  
    }
    
    public static void handleSocket(Socket incoming) throws IOException {
	DataInputStream in =
	    new DataInputStream(incoming.getInputStream());
	DataOutputStream out =
	    new DataOutputStream(incoming.getOutputStream());

	// should get user id first in order to work out which
	// key to use. Here, just use the same key for everyone
	byte[] key = new byte[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
	             // should be DESKeySpec.DES_KEY_LEN bytes
	DESKeySpec spec = null;
	try {
	    spec = new DESKeySpec(key);
	} catch(InvalidKeyException e) {
	    e.printStackTrace();
	    return;
	}
	SecretKeyFactory factory = null;
	try {
	    factory = SecretKeyFactory.getInstance("DES");
	} catch(NoSuchAlgorithmException e) {
	    e.printStackTrace();
	    return;
	}
	SecretKey secret = null;
	try {
	    secret = factory.generateSecret(spec);
	} catch(InvalidKeySpecException e) {
	    e.printStackTrace();
	    return;
	}

	Cipher inCipher = null;
	Cipher outCipher = null;
	try {
	    inCipher = Cipher.getInstance("DES");
	    outCipher = Cipher.getInstance("DES");
	} catch( NoSuchAlgorithmException e) {
	    e.printStackTrace();
	    return;
	} catch(NoSuchPaddingException e) {
	    e.printStackTrace();
	    return;
	}

	try {
	    inCipher.init(Cipher.DECRYPT_MODE, secret);
	    outCipher.init(Cipher.ENCRYPT_MODE, secret);
	} catch(InvalidKeyException e) {
	    e.printStackTrace();
	    return;
	}

	while (true) {
	    // read a byte saying how many bytes of data
	    // are coming
	    byte length = in.readByte();
	    byte[] inBytes = new byte[length];
	    int nread = in.read(inBytes);
	    if (nread != length) {
		break;
	    }
	    byte[] inDecrypt = null;
	    try {
		inDecrypt = inCipher.doFinal(inBytes);
	    } catch(IllegalBlockSizeException e) {
		e.printStackTrace();
		break;
	    } catch(BadPaddingException e) {
		e.printStackTrace();
		break;
	    }

	    String inStr = new String(inDecrypt);
	    System.out.println("Read from client: " + inStr);
	    if (inStr.equals("BYE")) {
		break;
	    }

	    // send it back
	    byte[] outCrypt = null;
	    try {
		outCrypt = outCipher.doFinal(inStr.getBytes());
	    } catch(IllegalBlockSizeException e) {
		e.printStackTrace();
		break;
	    } catch(BadPaddingException e) {
		e.printStackTrace();
		break;
	    }

	    out.writeByte((byte) outCrypt.length);
	    out.write(outCrypt);

	}
	incoming.close();
    }
}

SSL (TLS)

SSL (Secure Sockets Layer)

Echo Server with SSL/TLS

I can't get this to see any certificates yet


import java.io.*;
import java.net.*;
import javax.net.ssl.*;

public class TLSEchoServer {
    
    public static int MYECHOPORT = 8189;

    public static void main(String argv[]) {
	try {
	    SSLServerSocketFactory factory =
		(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
	    
	    SSLServerSocket sslSocket =
		(SSLServerSocket) factory.createServerSocket(MYECHOPORT);

	    while (true) {
		Socket incoming = sslSocket.accept();
		new SocketHandler(incoming).start();
	    }  
	} catch(IOException e) {
	    e.printStackTrace();
	    System.exit(30);
	}
    }
}

class SocketHandler extends Thread {

    Socket incoming;

    SocketHandler(Socket incoming) {
	this.incoming = incoming;
    }

    public void run() {
	try {
	    BufferedReader reader =
		new BufferedReader(new InputStreamReader(
				   incoming.getInputStream()));
	    PrintStream out =
		new PrintStream(incoming.getOutputStream());

	    boolean done = false;
	    while ( ! done) {
		String str = reader.readLine();
		if (str == null) 
		    done = true;
		else {
		    System.out.println("Read from client: " + str);
		    out.println("Echo: " + str);
		    if (str.trim().equals("BYE"))
			done = true;
		}
		
	    }
	    incoming.close();
	} catch(IOException e) {
	    e.printStackTrace();
	}
    }
}

Echo Client with SSL/TLS



/**
 * Client.java
 *
 *
 * Created: Fri Jul 20 12:54:51 2001
 *
 * @author <a href="mailto: ">Jan Newmarch</a>
 * @version
 */

import java.io.*;
import java.net.*;
import javax.net.ssl.*;

public class TLSEchoClient{

    public static final int MYECHOPORT = 8189;
    
    public static void main(String[] args){

	if (args.length != 1) {
	    System.err.println("Usage: Client address");
	    System.exit(1);
	}

	InetAddress address = null;
	try {
	    address = InetAddress.getByName(args[0]);
	} catch(UnknownHostException e) {
	    e.printStackTrace();
	    System.exit(2);
	}

	Socket sock = null;
	try {
	    sock = new Socket(address, MYECHOPORT);
	} catch(IOException e) {
	    e.printStackTrace();
	    System.exit(3);
	}
	SSLSocketFactory factory =
	    (SSLSocketFactory) SSLSocketFactory.getDefault();
	
	SSLSocket sslSocket = null;
	try {
	    sslSocket =
	    (SSLSocket) factory.createSocket(sock, args[0], MYECHOPORT, true);
	} catch(IOException e) {
	    e.printStackTrace();
	    System.exit(3);
	}
	
	BufferedReader reader = null;
	PrintStream out = null;

	try {
	    reader = new BufferedReader(new InputStreamReader(
                                    sslSocket.getInputStream()));
	    out = new PrintStream(sslSocket.getOutputStream());
	} catch(IOException e) {
	    e.printStackTrace();
	    System.exit(6);
	}

	String line = null;
	try {
	    // Just send a goodbye message, for testing
	    out.println("BYE");
	    line = reader.readLine();
	} catch(IOException e) {
	    e.printStackTrace();
	    System.exit(6);
	}

	System.out.println(line);
	System.exit(0);
    }
} // Client

Running the client and server

References

  1. Li Gong "Inside Java2 Platform Security" Addison-Wesley 1999
  2. Java Cryptography Extension http://java.sun.com/products/jce/index-14.html
  3. Java Secure Socket Extension http://java.sun.com/products/jsse/index-14.html


Jan Newmarch <jan@newmarch.name>
Last modified: Thu Aug 22 14:16:22 EST 2002
Copyright © Jan Newmarch, Monash University, 2007
Creative Commons License This work is licensed under a Creative Commons License
The moral right of Jan Newmarch to be identified as the author of this page has been asserted.