【问题标题】:TLSv1.2 SSL Chat Java, SSLContext not availableTLSv1.2 SSL Chat Java,SSLContext 不可用
【发布时间】:2013-12-12 21:06:34
【问题描述】:

我在运行 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();
        }
    }
}

当我尝试在终端中运行参数时

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.main(SSLSocketClient.java:61)

我不知道我的代码有什么问题。我已将密钥库复制到程序源代码中,但仍然无法正常工作。请帮帮我

谢谢

【问题讨论】:

  • 您使用的是哪个版本的 JRE?
  • 我不知道,我是在 Eclipse 中使用 Java 的新手,尤其是在 Ubuntu 中。 JRE 是这里的问题吗?
  • 好的,所以我在 Java SE1.7 中创建了项目,但仍然无法正常工作。我的代码有问题还是什么?
  • 在 Java 7 下是否也遇到同样的错误?您使用的是 OpenJDK 还是 Oracle JDK?
  • 我正在使用最新的 java 7 版本,但我仍然得到 java.security.NoSuchAlgorithmException: TLSv2 SSLContext not available!

标签: java eclipse security ssl


【解决方案1】:

TLSv1.2 直到 Java 7 才添加到默认 JCE 提供程序中。请参阅 Java 6Java 7 标准算法名称参考。

如果您受困于 Java 6 或更早版本并且绝对需要 TLS 1.2,请尝试使用 Bouncy Castle 提供程序。

【讨论】:

  • 所以如果我把java升级到java 7就没有问题了?以及如何将其更新为 Java 7?我是 Java 编程新手,尤其是 Ubuntu 和 Eclipse
  • 试试sudo apt-get install openjdk-7-jdk
  • 你也可以试试TLSv1.1
猜你喜欢
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
相关资源
最近更新 更多