📄 simpleserver.java
字号:
import java.io.*;import java.net.*;import java.security.KeyStore;import java.security.*;import javax.net.*;import javax.net.ssl.*;import javax.security.cert.X509Certificate;public class SimpleServer { private static SecureRandom secureRandom; private static KeyStore serverKeyStore, clientKeyStore; private static TrustManagerFactory tmf; private static KeyManagerFactory kmf; private static SSLContext sslContext; private static SSLServerSocketFactory sf; private static SSLServerSocket ss; private static final int port = 5566; private static String passphrase = "password"; public static void main(String[] args) { Socket c; SSLServerSocket s; System.out.println("Server Begin ....");// sf = null; try { System.out.println("creating socket..."); SimpleServer.SRandom(); SimpleServer.SetupClientKeyStore(); SimpleServer.SetupServerKeyStore(); SimpleServer.TManagerFactory(); SimpleServer.KManagerFactory(); SimpleServer.SContext(); s = SimpleServer.SSSFactory(); c = s.accept(); PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( c.getOutputStream()))); BufferedReader in = new BufferedReader( new InputStreamReader(c.getInputStream())); // echo messages from the client System.out.println("start echo ..."); String line; // out.println("hello\nhow do you do."); while ((line = in.readLine()) != null){ System.out.println("Msg from client is: " + line); out.println(line); out.flush(); } System.out.println("done..."); }catch(IOException e){ System.out.println("SimpleServer died: " + e.getMessage()); e.printStackTrace(); } }//-------------------SRandom------------------------------------------ private static void SRandom() throws IOException{ secureRandom = new SecureRandom(); secureRandom.nextInt(); }//--------------------SetupServerKeyStore-------------------------------------------- private static void SetupClientKeyStore() throws IOException { try{ clientKeyStore = KeyStore.getInstance("JKS"); clientKeyStore.load( new FileInputStream( "client.public" ), passphrase.toCharArray() ); }catch(Exception e){ System.out.println("SetupClientKeyStore ERROR ...."); } }//------------------------------------------------------------------- private static void SetupServerKeyStore() throws IOException { try{ serverKeyStore = KeyStore.getInstance( "JKS" ); serverKeyStore.load( new FileInputStream( "server.private" ), passphrase.toCharArray() ); }catch(Exception e){ System.out.println("SetupServerKeyStore ERROR ...."); } }//----------------------------------------------------------- private static void TManagerFactory() throws IOException { try{ tmf = TrustManagerFactory.getInstance( "SunX509" ); tmf.init( clientKeyStore ); }catch(Exception e){ System.out.println("SetupServerKeyStore ERROR ...."); } }//------------------------------------------------------------ private static void KManagerFactory() throws IOException { try{ kmf = KeyManagerFactory.getInstance("SunX509"); System.out.println("KeyManagerFactory.getInstance(SunX509) OK ..."); kmf.init( serverKeyStore, passphrase.toCharArray() ); System.out.println("kmf.init OK ...."); }catch(Exception e){ System.out.println("KManagerFactory ERROR ...."); System.out.println("KM" + e); } }//----------------------------------------------------------- private static void SContext() throws IOException { try{ sslContext = SSLContext.getInstance( "TLS" ); sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), secureRandom ); }catch(Exception e){ System.out.println("SContext ERROR ..."); } }//-------------------------------------------------------------- private static SSLServerSocket SSSFactory() throws IOException { try{ sf = sslContext.getServerSocketFactory(); ss = (SSLServerSocket)sf.createServerSocket( port ); ss.setNeedClientAuth( true ); }catch(Exception e){ System.out.println("SSLServerSocket ERROR ..."); ss = null; } return ss; }//-----------------------------------------------------------}/* private static ServerSocketFactory getServerSocketFactory(String passwd) { SSLServerSocketFactory ssf = null; try { // set up key manager to do server authentication SSLContext ctx; KeyManagerFactory kmf; KeyStore ks; char[] passphrase = passwd.toCharArray(); ctx = SSLContext.getInstance("TLS"); kmf = KeyManagerFactory.getInstance("SunX509"); ks = KeyStore.getInstance("JKS");// ks.load(new FileInputStream("wahabPrivateStore"), passphrase); ks.load(new FileInputStream("serverkeys"), passphrase); kmf.init(ks, passphrase); ctx.init(kmf.getKeyManagers(), null, null); ssf = ctx.getServerSocketFactory(); return ssf; } catch (Exception e) { e.printStackTrace(); } return null; }}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -