在GUI中使用相同的逻辑时出现错误,但是错误不会出现在控制台上。
我得到的错误是“ handshake_failure”,但在控制台上没有出现相同的错误。
Server.java:
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.security.KeyStore;
import java.security.SecureRandom;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;
public class Server {
public static void main(String [] args) {
SSLServerSocket serverSock = null;
SSLSocket socket = null;
PrintWriter out = null;
try {
//load server private key
KeyStore serverKeys = KeyStore.getInstance("JKS");
serverKeys.load(new FileInputStream("/home/plainserver.jks"),"password".toCharArray());
KeyManagerFactory serverKeyManager = KeyManagerFactory.getInstance("SunX509");
//System.out.println(KeyManagerFactory.getDefaultAlgorithm());
//System.out.println(serverKeyManager.getProvider());
serverKeyManager.init(serverKeys,"password".toCharArray());
//load client public key
KeyStore clientPub = KeyStore.getInstance("JKS");
clientPub.load(new FileInputStream("/home/clientpub.jks"),"password".toCharArray());
TrustManagerFactory trustManager = TrustManagerFactory.getInstance("SunX509");
trustManager.init(clientPub);
//use keys to create SSLSoket
SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(serverKeyManager.getKeyManagers(), trustManager.getTrustManagers(), SecureRandom.getInstance("SHA1PRNG"));
serverSock = (SSLServerSocket)ssl.getServerSocketFactory().createServerSocket(35786);
serverSock.setNeedClientAuth(true);
socket = (SSLSocket)serverSock.accept();
//send data
System.out.println("server Started");
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
out.println("data from PlainServer");
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(out!=null) out.close();
try {
if(serverSock!=null) serverSock.close();
if(socket!=null) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ConsoleClient.java:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyStore;
import java.security.SecureRandom;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManagerFactory;
public class ConsoleClient {
public static void main(String[] args) {
SSLSocket socket = null;
BufferedReader in = null;
try {
//load client private key
KeyStore clientKeys = KeyStore.getInstance("JKS");
clientKeys.load(new FileInputStream("/home/water/alok/cert/ca/plainclient.jks"),"password".toCharArray());
KeyManagerFactory clientKeyManager = KeyManagerFactory.getInstance("SunX509");
clientKeyManager.init(clientKeys,"password".toCharArray());
//load server public key
KeyStore serverPub = KeyStore.getInstance("JKS");
serverPub.load(new FileInputStream("/home/water/alok/cert/ca/serverpub.jks"),"password".toCharArray());
TrustManagerFactory trustManager = TrustManagerFactory.getInstance("SunX509");
trustManager.init(serverPub);
//use keys to create SSLSoket
SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(clientKeyManager.getKeyManagers(), trustManager.getTrustManagers(), SecureRandom.getInstance("SHA1PRNG"));
socket = (SSLSocket)ssl.getSocketFactory().createSocket("192.168.1.2", 35786);
socket.startHandshake();
//receive data
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String data;
while((data = in.readLine())!=null) {
System.out.println(data);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if(in!=null) in.close();
if(socket!=null) socket.close();
if(socket!=null) socket.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
ClientGui.java:
package test;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.net.ssl.*;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.*;
import java.security.*;
import java.security.SecureRandom;
import java.awt.event.ActionEvent;
public class test2 {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test2 window = new test2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public test2() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SSLSocket socket = null;
BufferedReader in = null;
try {
//load client private key
KeyStore clientKeys = KeyStore.getInstance("JKS");
clientKeys.load(new FileInputStream("/home/water/alok/cert/ca/plainclient.jks"),"password".toCharArray());
KeyManagerFactory clientKeyManager = KeyManagerFactory.getInstance("SunX509");
clientKeyManager.init(clientKeys,"password".toCharArray());
//load server public key
KeyStore serverPub = KeyStore.getInstance("JKS");
serverPub.load(new FileInputStream("/home/water/alok/cert/ca/serverpub.jks"),"password".toCharArray());
TrustManagerFactory trustManager = TrustManagerFactory.getInstance("SunX509");
trustManager.init(serverPub);
//use keys to create SSLSoket
SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(clientKeyManager.getKeyManagers(), trustManager.getTrustManagers(), SecureRandom.getInstance("SHA1PRNG"));
socket = (SSLSocket)ssl.getSocketFactory().createSocket("192.168.1.2", 35786);
socket.startHandshake();
//receive data
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String data;
while((data = in.readLine())!=null) {
System.out.println(data);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if(in!=null) in.close();
if(socket!=null) socket.close();
if(socket!=null) socket.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
});
btnNewButton.setBounds(161, 108, 114, 25);
frame.getContentPane().add(btnNewButton);
}
}
出什么问题了?
我什至尝试使用JavaFX,但这没有帮助。
我搜索了互联网,却一无所获。
基本上它甚至都没有连接。