TLSv1.2 SSL 聊天 Java,SSLContext 不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20554257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TLSv1.2 SSL Chat Java, SSLContext not available
提问by user3097141
I have problems when run my SSL Chat program. I'm using Eclipse in Ubuntu. I try to run this code
运行 SSL 聊天程序时遇到问题。我在 Ubuntu 中使用 Eclipse。我尝试运行此代码
import java.net.*;
import java.io.*;
import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.cert.*;
public class SSLSocketClient {
private static String host;
public static void main(String[] args) {
String cipher = null;
String portNo;
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");
}
SSLContext sc;
try {
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);
}
SSLSession session= mysslsocket.getSession();
X509Certificate cert;
// cert = (X509Certificate) session.getPeerCertificates()[0];
// System.out.println(session.getPeerHost() + "has presented a certificate belonging to: ");
//Principal p=cert.getSubjectDN();
//if(chat) {
//BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
//BufferedWriter out = new BufferedWriter( new OutputStreamWriter( mysslsocket.getOutputStream()));
//while(true) {
//String s= in.readLine();
//if (!s.equals("")) {
//out.write(s);
//out.write(" \r\n ");
//out.flush();
//if (s.equals(".")) break;
// }
//}
// }
mysslsocket.startHandshake();
mysslsocket.close();
} 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();
}
}
}
When i try to run the argument in the terminal
当我尝试在终端中运行参数时
java SSLSocketClient -host localhost -port 11111 -mykeystore
I got this responds:
我得到了这样的回应:
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.main(SSLSocketClient.java:61)
I dont know what's wrong in my code. Ihave copied the keystore to the program source code but still doesn't work. Please help me
我不知道我的代码有什么问题。我已将密钥库复制到程序源代码中,但仍然无效。请帮我
Thanks
谢谢
回答by Kelsey Francis
TLSv1.2
wasn't added to the default JCE provider until Java 7. See the Java 6and Java 7standard algorithm names references.
TLSv1.2
直到 Java 7 才添加到默认 JCE 提供程序。请参阅Java 6和Java 7标准算法名称参考。
If you're stuck on Java 6 or earlier and absolutely need TLS 1.2, try the Bouncy Castleprovider.
如果您坚持使用 Java 6 或更早版本并且绝对需要 TLS 1.2,请尝试使用Bouncy Castle提供程序。