我在Ubuntu中使用Eclipse的Java中的SSL Chat应用程序遇到问题。 我有这样的代码:
import java.net.*;
import java.io.*;
import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.*;
public class SSLSocketClient {
private static String host;
public static void main(String[] args) {
String cipher = null;
String portNo;
String chatType;
String mykeystoreValue;
int port = 0;
boolean mykeystore = false;
boolean chat = false;
if (args.length == 5) {
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-host")) {
host = args[++i];
continue;
}
if (args[i].equals("-port")) {
portNo = args[++i];
port = Integer.parseInt(portNo);
continue;
}
if (args[i].equals("-cipher")) {
cipher = args[++i];
continue;
}
if (args[i].equals("-chat")) {
chat = true;
continue;
}
if (args[i].equals("-mykeystore")) {
mykeystore = true;
continue;
}
}
}
else {
System.out.println("Please check again parameter!");
}
if (mykeystore) {
System.setProperty("javax.net.ssl.trustStore", "mykeystore");
System.setProperty("javax.net.ssl.trustStorePassword", "kosuke");
}
try {
SSLContext sc = SSLContext.getInstance("TLSv1.2");
sc.init(null, null, null);
SSLSocketFactory factory = (SSLSocketFactory) sc.getSocketFactory();
SSLSocket mysslsocket = (SSLSocket) factory.createSocket(host, port);
if (cipher != null) {
String[] cipherarray = { cipher };
mysslsocket.setEnabledCipherSuites(cipherarray);
}
mysslsocket.startHandshake();
mysslsocket.close();
if (chat){
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我在终端上的论点是这样的
java SSLSocketClient -host localhost -port 11111 -mykeystore
结果在终端中:
java.security.NoSuchAlgorithmException: TLSv1.2 SSLContext not available
at sun.security.jca.GetInstance.getInstance(GetInstance.java:159)
at javax.net.ssl.SSLContext.getInstance(SSLContext.java:142)
at SSLSocketClient.SSLClient(SSLSocketClient.java:33)
at SSLSocketClient.main(SSLSocketClient.java:106)
并在服务器终端中显示结果:
Using default temp DH parameters
Using default temp ECDH parameters
ACCEPT
SSL_accept:before/accept initialization
read from 0x147cb40 [0x14868a0] (11 bytes => 11 (0xB))
0000 - 16 03 03 00 dd 01 00 00-d9 03 03 ...........
read from 0x147cb40 [0x14868ae] (215 bytes => 215 (0xD7))
0000 - 52 ac 5e a8 58 2b 7b 25-63 37 01 f9 d8 83 0c 6c R.^.X+{%c7.....l
0010 - 97 bc 7b c2 77 6d e8 e4-fc a1 49 28 d7 f2 b7 bd ..{.wm....I(....
0020 - 00 00 54 c0 24 c0 28 00-3d c0 26 c0 2a 00 6b 00 ..T.$.(.=.&.*.k.
0030 - 6a c0 0a c0 14 00 35 c0-05 c0 0f 00 39 00 38 c0 j.....5.....9.8.
0040 - 23 c0 27 00 3c c0 25 c0-29 00 67 00 40 c0 09 c0 #.'.<.>
0050 - 13 00 2f c0 04 c0 0e 00-33 00 32 c0 07 c0 11 00 ../.....3.2.....
0060 - 05 c0 02 c0 0c c0 08 c0-12 00 0a c0 03 c0 0d 00 ................
0070 - 16 00 13 00 04 00 ff 01-00 00 5c 00 0a 00 34 00 ..........\...4.
0080 - 32 00 17 00 01 00 03 00-13 00 15 00 06 00 07 00 2...............
0090 - 09 00 0a 00 18 00 0b 00-0c 00 19 00 0d 00 0e 00 ................
00a0 - 0f 00 10 00 11 00 02 00-12 00 04 00 05 00 14 00 ................
00b0 - 08 00 16 00 0b 00 02 01-00 00 0d 00 1a 00 18 06 ................
00c0 - 03 06 01 05 03 05 01 04-03 04 01 03 03 03 01 02 ................
00d0 - 03 02 01 02 02 01 01 .......
<<< TLS 1.2 [length 00dd]
01 00 00 d9 03 03 52 ac 5e a8 58 2b 7b 25 63 37
01 f9 d8 83 0c 6c 97 bc 7b c2 77 6d e8 e4 fc a1
49 28 d7 f2 b7 bd 00 00 54 c0 24 c0 28 00 3d c0
26 c0 2a 00 6b 00 6a c0 0a c0 14 00 35 c0 05 c0
0f 00 39 00 38 c0 23 c0 27 00 3c c0 25 c0 29 00
67 00 40 c0 09 c0 13 00 2f c0 04 c0 0e 00 33 00