Created
March 24, 2011 03:16
-
-
Save JamesWang/884491 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package aten.ssl; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import java.net.InetSocketAddress; | |
| import java.net.ServerSocket; | |
| import java.net.Socket; | |
| import javax.net.ssl.SSLServerSocketFactory; | |
| public class SSLServer { | |
| public void process( Socket socket ) throws IOException{ | |
| InputStream in = socket.getInputStream(); | |
| OutputStream out = socket.getOutputStream(); | |
| out.write( Util.toByteArray("Hello, guys!")); | |
| int ch = 0; | |
| while( ( ch = in.read()) != '!' ){ | |
| out.write( ch ); | |
| } | |
| out.write( ch ); | |
| socket.close(); | |
| } | |
| private void newServer() throws IOException{ | |
| SSLServerSocketFactory sslFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault(); | |
| ServerSocket server = sslFactory.createServerSocket(); | |
| server.bind( new InetSocketAddress("localhost",12665)); | |
| process( server.accept() ); | |
| } | |
| public static void main(String[] args ) throws IOException{ | |
| new SSLServer().newServer(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package aten.ssl; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import java.net.Socket; | |
| import java.net.UnknownHostException; | |
| import javax.net.ssl.SSLSocketFactory; | |
| public class SSLClient { | |
| private static void process( Socket socket ) throws IOException{ | |
| OutputStream out = socket.getOutputStream(); | |
| InputStream in = socket.getInputStream(); | |
| out.write(Util.toByteArray("World")); | |
| out.write('!'); | |
| int ch = 0; | |
| while ((ch = in.read()) != '!') | |
| { | |
| System.out.print((char)ch); | |
| } | |
| System.out.println((char)ch); | |
| } | |
| public static void main(String[] args ) throws UnknownHostException, IOException{ | |
| SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault(); | |
| Socket socket = factory.createSocket("localhost", 12665); | |
| process( socket ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package aten.ssl; | |
| import java.security.KeyPair; | |
| import java.security.KeyPairGenerator; | |
| import java.security.SecureRandom; | |
| /** | |
| * User: James Wang | |
| * Date: 3/2/11 | |
| * Time: 10:34 PM | |
| */ | |
| public class Util { | |
| public final static String EMPTY = new String( new char[0] ); | |
| private static char[] HEX_CHARS = | |
| { '0', '1', '2', '3', '4', '5', '6', '7', | |
| '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; | |
| public static String toHex( byte[] data ){ | |
| if( data == null ) return EMPTY; | |
| return toHex( data, data.length); | |
| } | |
| public static String toHex( byte[] data, int dataLength ){ | |
| if( data == null ) return EMPTY; | |
| char[] chars = new char[ 2 * dataLength ]; | |
| for( int i=0,j=0; i< dataLength; i++,j=j+2){ | |
| chars[j] = HEX_CHARS[ (data[i]>>>4)&0x0F] ; | |
| chars[j+1] = HEX_CHARS[ data[i]&0xF ] ; | |
| } | |
| return new String( chars ); | |
| } | |
| public static String toString( byte[] bytes ){ | |
| return toString( bytes, bytes == null?0:bytes.length ); | |
| } | |
| public static String toString( byte[] bytes, int length ){ | |
| if( bytes == null || length == 0 ){ | |
| return EMPTY; | |
| } | |
| try{ | |
| return new String( bytes, "UTF-8" ); | |
| }catch(Exception ex ){ | |
| //Should never be here | |
| throw new RuntimeException("Cannot find UTF-8 encoding" ); | |
| } | |
| } | |
| public static byte[] asBytes(String str) { | |
| if (str == null || str.length() == 0) { | |
| return new byte[0]; | |
| } | |
| try{ | |
| byte[] bytes = new byte[str.length()]; | |
| System.arraycopy( str.getBytes("UTF-8"),0,bytes,0,bytes.length ); | |
| return bytes; | |
| }catch(Exception ex){ | |
| //Should never get here | |
| throw new RuntimeException("Cannot find UTF-8 encoding!"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment