الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مثال بسيط على SSL

مغلق
بدأه زائر في 11 مايو 2007 · 3 رد · 746 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ،

هذا مثال بسيط ، لتطبيق 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 ");
		}
	}
}
#2

السلام عليكم..

اخي عيسى لقد قمت منذ فتره قليله بفحص موقع بال jsp وامكانيه عمله في ال SSL ولكي استطيع القيام بعميه الفحص قمت باستخدام خطوات شبيهه بالتي قمت بها:

use the following command to create a keystore file

go to the %JAVA_HOME% ...\Java\jdk1.5.0_05\bin

then write this command

keytool -genkey -alias tomcat -keyalg RSA

When creating this file, you should use the password "changeit" for both passwords

This will create a “.keystore” file in your C:\Documents and Settings\CURRENT-USER\ folder.

بعد ذالك يجب القيام ببعض التغييرات في ال server.xml في jakarta-tomcat-5.0.28\conf

edit ...site - server.xml file.

Please replace the non SSL with this SSL <Connector> tag:

<!-- Define an SSL HTTP/1.1 Connector on port 8443 -->

<Connector className="org.apache.catalina.connector.http.HttpConnector" port="8443" minProcessors="5" maxProcessors="75"

enableLookups="true" redirectPort="443" acceptCount="10" debug="0" scheme="https" secure="true">

<Factory className="org.apache.catalina.net.SSLServerSocketFactory" clientAuth="false" protocol="TLS"

keystoreFile="C:\Documents and Settings\CURRENT-USER\ folder\.keystore"/>

</Connector>

use this link to Go to the login page.

https://127.0.0.1:8443/

#3

الله عليك فراس ، فعلاً رائع

شكراً جزيلاً

#4

يعني البرنامج ده بيعمل secure channel بين الطرفين ؟؟

يعني ممكن نقول إنه بي implement طريقه لتطبيق ال Secure communication system ؟؟

إنقله أحسن في قسم الشبكات :D: D :D

( إني أمزح )

إيه الحاجات الحلوه دي ده نظام public key كامل و كمان digital certificate :D لأ حاجه جامده

يعني انت بت implement نظام ال public key ؟

لو عند حضرتك وقت ياريت تشرح لنا و تقول أنهو ال verifier و أنه ال applicant إلخخخخخخخخخخخخخخخخخ.

جزاك الله خيرا :D

هذا الموضوع مغلق.

مواضيع مشابهة