Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

apply SSL on Sockets

Fri Jun 08, 2007 12:51 pm

this is simple example to follow SSL connection by Sockets in Java ...

Server :

java code
/*#################### 
* This example ONLY for Development Enviroment and testing purposes
* not for Production Enviroment this is simple example ,
* i don't want to import and export certificate or send most specific arguments
* because iam in the same PC and THERE IS NO Application Server (e.g.ColdFusion,GlassFish )
* iam care only about how to apply SSL though JAVA , so its just SIMPLE EXAMPLE !!
*
*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* JAVA in JDK1.4 realease JAVA Secure Socket Extension(JSSE)
* as default package installed when you install JKD from java.sun.com
* this example running on JDK1.6 (mustang) ,
* and supposed to run with 1.4 & 1.5 without problems
*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* keytool (part of JSSE)is utility tool for creating keys management
* from this tool you can create self-signed certificate
* and pulic/private keys , for SSL or TLS
*
*
*prerequests :
* any JAVA IDE with JDK1.4 or heigher
* little knowledge regarding Sockets in JAVA
*
*Steps :
*
*(1)invoke this statement from command-line :
* keytool -genkey -keystore CEN448
*
* if you face problem such is not recognize internal ...
* so your classpath from the ENVIROMENT VARIABLE window didn't configured well
*
*(2) after execute command it will prompt you to enter password at least 6 chars ,
* enter CEN448 note its case-sensitive and then write anything for first six questions
* then write yes at seven and press enter at eight question .
*
*(3) after perform two steps keytool create self-signed file in your home directory such as :
* C:\Documents and Settings\quick with the name CEN448 , copy it and paste it
* at your application directory such as : C:\Documents and Settings\quick\Desktop\UMLTutorial\EDU
*
*
* -genkey , to generate new key pair (public and private)
* -keystore to specify the name of the persistent keystore file that contain keys
*
* the default key pair generation algorithm is Digital Signature Algorithm (DSA)
* you can specify other algorithem by invoke -keyalg algorithemName beside command
*
* the default valid days for self-signed is 90 days
*
* Password its only for protect your file , it doesn't do anything related to the en/de.cryption
* you must specify the same password for the KeyStore you want to access in your program
* %%%%%%%%% how to Run %%%%%%%%%%%%
* first run Server.java then run Client.java and wrote
* messages from client side to the server , write bye bye to quit
*
***************** Lines Explaination ***********************
* (1) in the keyStore system property you save the sensitive data(private key)
* with the certificate for the identity purpose(as Server ).
*
* (2) protect your file with password by assign the KeyStorePassword property
*
* (3) create SSLServerSocketFactory with the default SSL server socket
* SSL server socket its same as ServerSocket but secure .
* SSLSocketFactory if SSL not configured well in JVM it will throw an Exception
*
* (4) create Secure ServerSocket and bind it to port 2345 .
*
* (5) create secure socket accepted from client
*
* Reference :
* java.sun.com/products/jsse/
* www6.software.ibm.com/developerworks/education/j-jsse/index.html
* javaworld.com/javaworld/jw-05-2001/jw-0511-howto.html
*
*/

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

public class Server {
public static void main(String[]ssl) {
try {

System.setProperty("javax.net.ssl.keyStore","CEN448");//1
System.setProperty("javax.net.ssl.keyStorePassword","CEN448");//2
//System.setProperty("javax.net.debug","ssl"); //for debug purposes uncommit this statement
SSLServerSocketFactory ssf =(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();//3
SSLServerSocket sss = (SSLServerSocket) ssf.createServerSocket(2345);//4
SSLSocket ss = (SSLSocket) sss.accept();//5
System.out.println("Successfully connected to the SecureServer ");
BufferedReader br = new BufferedReader(new InputStreamReader(ss.getInputStream()));
String out = null;
while ((out = br.readLine())!= null&&(!out.equals("bye bye"))) {
System.out.println(out);
}
System.out.println("End Session ");
} catch (Exception e) {
System.err.println("Exception while create SSLServerSocket");
}

}
}



Client :

java code
/*
*^^^^^^^^^^^^^^Lines Explaination^^^^^^^^^^^^^^
* (1) assign the System Property trustStore to use our self-signed certificate
* there is no sensitive data here only public key (as Client)
*
* (2) inform the data stream to go to the serverSocket
*
*/
import javax.net.ssl.*;
import java.io.*;

public class Client {
public static void main(String[]ssl) {
try {

System.setProperty("javax.net.ssl.trustStore","CEN448");//1
System.setProperty("javax.net.ssl.trustStorePassword","CEN448");
SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket ss = (SSLSocket) ssf.createSocket("127.0.0.1",2345);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ss.getOutputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


String out;
while ((out = br.readLine())!= null) {
bw.write(out);
bw.newLine();
bw.flush(); //2
}
} catch (Exception e) {
System.err.println("Exception while connect to Server ");
}
}
}




Re: apply SSL on Sockets

Fri Jun 08, 2007 1:11 pm

Hi Xline ,
very good code , Security sockets :D

I tried to run the classes
the client works fine , but the problem is in the Server side , i made small changes in Server Class which i removed comments and " " tags to Strings in print functions :P here it is the class , it compile with no error but when i run an exception happened " (The system cannot find the file specified)"
Code:
import javax.net.ssl.*;
import java.io.*;

public class Server {
    public static void main(String[]ssl) {
        try {
               
            System.setProperty("javax.net.ssl.keyStore","CEN448");
       System.setProperty("javax.net.ssl.keyStorePassword","CEN448");
        System.setProperty("javax.net.debug","ssl");
            SSLServerSocketFactory ssf =(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
            SSLServerSocket sss = (SSLServerSocket) ssf.createServerSocket(2345);
            SSLSocket ss = (SSLSocket) sss.accept();
            System.out.println("Successfully connected to the  SecureServer" );
            BufferedReader br = new BufferedReader(new InputStreamReader(ss.getInputStream()));
            String out = null;
            while ((out = br.readLine())!= null&&(!out.equals("bye bye"))) {
               System.out.println("out");
              }
              System.out.println("End Session ");
        } catch (Exception e) {
             System.err.println("Exception while create SSLServerSocket");
        }
     
    }
}


I didn't follow the steps
*Steps :
*
*(1)invoke this statement from command-line :
* keytool -genkey -keystore CEN448
*
* if you face problem such is not recognize internal ...
* so your classpath from the ENVIROMENT VARIABLE window didn't configured well
*
*(2) after execute command it will prompt you to enter password at least 6 chars ,
* enter CEN448 note its case-sensitive and then write anything for first six questions
* then write yes at seven and press enter at eight question .
*
*(3) after perform two steps keytool create self-signed file in your home directory such as :
* C:\Documents and Settings\quick with the name CEN448 , copy it and paste it
* at your application directory such as : C:\Documents and Settings\quick\Desktop\UMLTutorial\EDU
*
*
* -genkey , to generate new key pair (public and private)
* -keystore to specify the name of the persistent keystore file that contain keys
*
* the default key pair generation algorithm is Digital Signature Algorithm (DSA)
* you can specify other algorithem by invoke -keyalg algorithemName beside command
*
* the default valid days for self-signed is 90 days
*
* Password its only for protect your file , it doesn't do anything related to the en/de.cryption
* you must specify the same password for the KeyStore you want to access in your program
* %%%%%%%%% how to Run %%%%%%%%%%%%
* first run Server.java then run Client.java and wrote
* messages from client side to the server , write bye bye to quit
*


Re: apply SSL on Sockets

Fri Jun 08, 2007 4:48 pm

smi_333 , why you put String "out" between qoutation !

just follow the steps ,
about exception do you copy the file(certificate) CEN448 generated by command line to your Project directory ?

Re: apply SSL on Sockets

Fri Jun 08, 2007 4:57 pm

Casper
did you follow the steps ?
did you generate CEN448 file ?

if you follow the steps and still the problem be sure that the jsse package installed with your JDK(1.4 and above) ,

go to bin directory like : C:\Program Files\Java\jdk1.6.0\bin and look at keytool.exe

Re: apply SSL on Sockets

Sat Jun 09, 2007 7:00 pm

How to generate CEN448 file , is it a text file

Re: apply SSL on Sockets

Sun Jun 10, 2007 11:14 am

after invoke the command , CEN448 encrypted file will created

Post a reply
  Related Posts  to : apply SSL on Sockets
 echo client using Sockets     -  
 apply mean filter to image     -  
 apply laplacian filter to image     -  
 apply robert filter to image     -  
 apply sobel filter to image     -  
 apply prewitt filter to image     -  
 Fast Cash all currencies apply here     -  
 DEBTS CLEARANCE LOANS FOR DUBAI AND SHARJAH APPLY NOW     -  
 We offer different types of loans Easy Loans Offer Apply Now     -  
 We offer different types of loans Easy Loans Offer Apply Now     -