import java.io.*; import javax.net.ssl.*; import java.security.*; /** * SSL Client Program. */ public class sslclient { /** * SslClient main method. * * @param args the command line arguments (not used) */ public static void main(String args[]) { /* * Set up to catch any exceptions thrown. */ try { // Create/initialize the SSLContext with key material char[] passphrase = "passphrase".toCharArray(); // First initialize the key and trust material. KeyStore ksKeys = KeyStore.getInstance("JKS"); ksKeys.load(new FileInputStream("testKeys"), passphrase); KeyStore ksTrust = KeyStore.getInstance("JKS"); ksTrust.load(new FileInputStream("testTrust"), passphrase); // KeyManager's decide which key material to use. KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ksKeys, passphrase); // TrustManager's decide whether to allow connections. TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); tmf.init(ksTrust); SSLContext sslContext = SSLContext.getInstance("SSLv3"); sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); /* * Get the an SSLSocketFactory from the SSLContext. */ SSLSocketFactory sf = sslContext.getSocketFactory(); /* * Create an SSLSocket. * * Change the hard-coded IP address to the IP address or host name * of the server. */ SSLSocket s = (SSLSocket) sf.createSocket("blanca.uccs.edu", 13333); /* * Send a message to the server using the secure session. */ String sent = "Test of java SSL write"; OutputStream os = s.getOutputStream(); os.write(sent.getBytes()); /* * Write results to screen. */ System.out.println("Wrote " + sent.length() + " bytes..."); System.out.println(sent); } catch (Exception e) { System.out.println("Unexpected exception caught: " + e.getMessage()); e.printStackTrace(); } } }