这是服务器来源:
package main;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.security.KeyStore;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
public class SslReverseEchoer {
public static void main(String[] args) throws Exception {
String ksName = "keystore.jks";
char ksPass[] = "".toCharArray();
char ctPass[] = "".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(ksName), ksPass);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, ctPass);
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), null, null);
SSLServerSocketFactory ssf = sc.getServerSocketFactory();
SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(8888);
SSLSocket c = (SSLSocket) s.accept();
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
c.getOutputStream()));
BufferedReader r = new BufferedReader(new InputStreamReader(
c.getInputStream()));
String m = "Welcome to SSL Reverse Echo Server."
+ " Please type in some words.";
w.write(m, 0, m.length());
w.newLine();
w.flush();
while ((m = r.readLine()) != null) {
if (m.equals("."))
break;
char[] a = m.toCharArray();
int n = a.length;
for (int i = 0; i < n / 2; i++) {
char t = a[i];
a[i] = a[n - 1 - i];
a[n - i - 1] = t;
}
w.write(a, 0, n);
w.newLine();
w.flush();
}
w.close();
r.close();
c.close();
s.close();
}
}
这是客户代码:
package main;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.security.KeyStore;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class SslSocketClient {
public static void main(String[] args) throws Exception {
String ksName = "keystore.jks";
char ksPass[] = "".toCharArray();
char ctPass[] = "".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(ksName), ksPass);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, ctPass);
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), null, null);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintStream out = System.out;
SSLSocketFactory f = (SSLSocketFactory) sc.getSocketFactory();
SSLSocket c = (SSLSocket) f.createSocket("localhost", 8888);
c.startHandshake();
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
c.getOutputStream()));
BufferedReader r = new BufferedReader(new InputStreamReader(
c.getInputStream()));
String m = null;
while ((m = r.readLine()) != null) {
out.println(m);
m = in.readLine();
w.write(m, 0, m.length());
w.newLine();
w.flush();
}
w.close();
r.close();
c.close();
}
}
当我尝试运行它们时,我得到以下错误:
客户端:
Exception in thread "main" javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
服务器:
javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_unknown
我在编程方面不是很有经验,所以我(我想)知道错误消息是什么意思,但我不知道如何解决它。 两个密钥店都到位了。一个持有Keypair,另一个持有从Keypair生成的证书。 (删除所有密码以进行测试) 我已经在互联网上搜索了相当长的一段时间,但我发现只有很差的解释,因为大多数SSL-Connections尝试连接到网络服务器而不是自制的。 最后,我希望有两个Java应用程序通过SECURED SSL-Connection传输数据。这不是一个HTTPS服务器。