簡體   English   中英

TLSv1.2 SSL Chat Java,SSLContext不可用

[英]TLSv1.2 SSL Chat Java, SSLContext not available

運行我的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)

我不知道我的代碼有什么問題。 我已經將密鑰庫復制到程序源代碼但仍然無法正常工作。 請幫我

謝謝

在Java 7之前, TLSv1.2未添加到缺省JCE提供程序中。請參閱Java 6Java 7標准算法名稱引用。

如果您堅持使用Java 6或更早版本且絕對需要TLS 1.2,請嘗試使用Bouncy Castle提供程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM