Total members 11893 |It is currently Tue Nov 05, 2024 10:06 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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 ");
}
}
}





Author:
Newbie
User avatar Posts: 17
Have thanks: 0 time

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
Quote:
*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
*


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

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 ?


Author:
Newbie
User avatar Posts: 17
Have thanks: 0 time

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


Author:
Newbie
User avatar Posts: 17
Have thanks: 0 time

How to generate CEN448 file , is it a text file

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

after invoke the command , CEN448 encrypted file will created


Author:
Newbie
User avatar Posts: 17
Have thanks: 0 time
Post new topic Reply to topic  [ 6 posts ] 

  Related Posts  to : apply SSL on Sockets
 echo client using Sockets     -  
 apply mean filter to image     -  
 apply sobel filter to image     -  
 apply prewitt filter to image     -  
 Fast Cash all currencies apply here     -  
 apply laplacian filter to image     -  
 apply robert filter to image     -  
 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     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com