📄 simpleclient.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 SimpleClient {
private static SecureRandom secureRandom;
private static KeyStore serverKeyStore, clientKeyStore;
private static TrustManagerFactory tmf;
private static KeyManagerFactory kmf;
private static SSLContext sslContext;
private static SSLSocketFactory sf;
private static SSLSocket s;
private static final int port = 5566;
private static String host = "server1";
private static String passphrase = "password";
public static void main(String[] args)throws IOException
{
System.out.println("Beigin Client ...");
try {
System.out.println("connecting...");
SimpleClient.SRandom();
SimpleClient.SetupClientKeyStore();
SimpleClient.SetupServerKeyStore();
SimpleClient.TManagerFactory();
SimpleClient.KManagerFactory();
SimpleClient.SContext();
SSLSocket c = SimpleClient.SCFactory();
System.out.println("handshaking...");
c.startHandshake();
BufferedReader in = new BufferedReader(
new InputStreamReader(c.getInputStream()));
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
c.getOutputStream())));
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
String line;
String strin;
for(;;)
{
System.out.print("Enter a line:");
strin = stdin.readLine();
if (strin.length() == 0) break;
out.println (strin);
out.flush();
line = in.readLine();
System.out.println("Msg from Server is: " + line);
}
in.close();
out.close();
c.close();
System.out.println("done...");
}catch (IOException e){
System.out.println("SimpleClient died: " + e.getMessage());
e.printStackTrace();
}
}
//-------------------SRandom------------------------------------------
private static void SRandom() throws IOException{
secureRandom = new SecureRandom();
secureRandom.nextInt();
}
//--------------------SetupServerKeyStore--------------------------------------------
private static void SetupServerKeyStore() throws IOException {
try{
serverKeyStore = KeyStore.getInstance( "JKS" );
serverKeyStore.load( new FileInputStream( "server.public" ), passphrase.toCharArray() );
}catch(Exception e){
System.out.println("SetupServerKeyStore ERROR ...");
}
}
//-------------------------------------------------------------------
private static void SetupClientKeyStore() throws IOException {
try{
clientKeyStore = KeyStore.getInstance( "JKS" );
clientKeyStore.load( new FileInputStream( "client.private" ), passphrase.toCharArray() );
}catch(Exception e){
System.out.println("SetupClientKeyStore Error ...");
}
}
//-----------------------------------------------------------
private static void TManagerFactory() throws IOException {
try{
tmf = TrustManagerFactory.getInstance( "SunX509" );
tmf.init( serverKeyStore );
}catch(Exception e){
System.out.println("TManagerFactory ERROR ...");
}
}
//------------------------------------------------------------
private static void KManagerFactory() throws IOException {
try{
kmf = KeyManagerFactory.getInstance( "SunX509" );
kmf.init( clientKeyStore, passphrase.toCharArray() );
}catch(Exception e){
System.out.println("KMagagerFactory 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 SSLSocket SCFactory() throws IOException {
try{
sf = sslContext.getSocketFactory();
s = (SSLSocket)sf.createSocket(host, port);
}catch(Exception e){
System.out.println("SCFactory ERROR ...");
s = null;
}
return s;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -