السلام عليكم ،
هذا مثال بسيط ، لتطبيق Secure Socket Layer SSL ،
قدمته إلى مادة Security CEN448 اللي ادرسها حالياً ، واحببت اني اضيفه هنا ليستفيد الاخوه منه
هو مثال بسيط عباره عن Sockets وأرحب بأي آراء حول تطبيقه على AS
Server.java
/*#########################################################################
* 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 :
* http://java.sun.com/products/jsse/
* https://www6.software.ibm.com/developerworks/education/j-jsse/index.html
* http://www.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
/*
*^^^^^^^^^^^^^^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 ");
}
}
}