SSLHandshakeException:没有共同的密码套件(JAVA)[重复]

Posted

技术标签:

【中文标题】SSLHandshakeException:没有共同的密码套件(JAVA)[重复]【英文标题】:SSLHandshakeException: No ciphers suites in common (JAVA) [duplicate] 【发布时间】:2016-09-02 00:47:02 【问题描述】:

有人可以告诉我我在这段代码中做错了什么吗?服务器端似乎正在工作。一旦我为服务器端运行代码,服务器就会等待客户端请求连接。然后,当我运行客户端代码时,它会在客户端引发以下异常:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at LoginDialog.doConnectToServer(LoginDialog.java:208)
    at LoginDialog.<init>(LoginDialog.java:67)
    at ClientSideMain.main(ClientSideMain.java:5)
Press any key to continue . . .

以及服务器端的以下异常:

javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: no cipher suites in common
    at sun.security.ssl.SSLSocketImpl.checkEOF(Unknown Source)
    at sun.security.ssl.AppInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at Talker.recieve(Talker.java:78)
    at CTC.run(CTC.java:49)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.net.ssl.SSLHandshakeException: no cipher suites in common
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.ServerHandshaker.chooseCipherSuite(Unknown Source)
    at sun.security.ssl.ServerHandshaker.clientHello(Unknown Source)
    at sun.security.ssl.ServerHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
    at sun.security.ssl.AppOutputStream.write(Unknown Source)
    at sun.security.ssl.AppOutputStream.write(Unknown Source)
    at java.io.DataOutputStream.writeBytes(Unknown Source)
    at Talker.send(Talker.java:58)
    at CTC.send(CTC.java:344)
    at CTC.<init>(CTC.java:31)
    at ServerSide.<init>(ServerSide.java:73)
    at ServerSideMain.main(ServerSideMain.java:5)

服务器端代码

public class ServerSide

ServerSocket serverSocket;
Socket regSocket;
Hashtable<String,User> userList;
CTC tempCTC;
File f;
DataInputStream in;
BufferedReader br;

SSLContext sslContext;
KeyManagerFactory keyManagerFactory;
KeyStore keyStore;
char[] keyStorePassphrase;

SSLServerSocketFactory sslServerSocketFactory;
SSLServerSocket sslServerSocket;
SSLSocket sslNormalSocket;

ServerSide()

    f = new File("userlist.txt");
    userList = new Hashtable<String, User>();
    loadUsers(f);

    try
    
        sslContext = SSLContext.getInstance("SSL");
        keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
        keyStore = KeyStore.getInstance("JKS");
        keyStorePassphrase = "passphrase".toCharArray();
        keyStore.load(new FileInputStream("testkeys"), keyStorePassphrase);
        keyManagerFactory.init(keyStore, keyStorePassphrase);
        sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
        sslServerSocketFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();

        sslServerSocket = (SSLServerSocket)sslServerSocketFactory.createServerSocket(12345);

        tryThread.sleep(1000);catch(Exception e)

        while(true)
        
            sslNormalSocket = (SSLSocket)sslServerSocket.accept();
            tempCTC = new CTC(sslNormalSocket, userList, f);
        
    
    catch(IOException ioe)
    
        ioe.printStackTrace();
    
    catch(NoSuchAlgorithmException nsae)
    
        nsae.printStackTrace();
    
    catch(KeyStoreException kse)
    
        kse.printStackTrace();
    
    catch(CertificateException ce)
    
        ce.printStackTrace();
    
    catch(UnrecoverableKeyException uke)
    
        uke.printStackTrace();
    
    catch(KeyManagementException kme)
    
        kme.printStackTrace();
    

客户端代码(发生异常的地方)

private void doConnectToServer()

    SSLSocketFactory        sslSocketFactory;
    KeyManagerFactory       keyManagerFactory;
    SSLContext              sslContext;
    KeyStore                keyStore;
    char[]                  keyStorePassphrase;
    SSLSocket               sslSocket;

    try
    
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

        System.setProperty("javax.net.ssl.trustStore", "samplecacerts");
        System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

        sslContext = SSLContext.getInstance("SSL");
        keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
        keyStore = KeyStore.getInstance("JKS");

        keyStorePassphrase = "passphrase".toCharArray();
        keyStore.load(new FileInputStream("testkeys"), keyStorePassphrase);

        keyManagerFactory.init(keyStore, keyStorePassphrase);
        sslContext.init(keyManagerFactory.getKeyManagers(), null, null);

        sslSocketFactory = sslContext.getSocketFactory();

        sslSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();

        sslSocket = (SSLSocket)sslSocketFactory.createSocket("127.0.0.1", 12345);

        sslSocket.startHandshake(); //line that throws the exception

        cts = new CTS(sslSocket, this);
    
    catch(Exception e)
    
        e.printStackTrace();
    

【问题讨论】:

【参考方案1】:

似乎您在双方(服务器/客户端)上没有相同的加密方法。

Caused by: javax.net.ssl.SSLHandshakeException: no cipher suites in common

确保两边都使用相同的。

【讨论】:

以上是关于SSLHandshakeException:没有共同的密码套件(JAVA)[重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何修复 javax.net.ssl.SSLHandshakeException:没有共同的密码套件? [复制]

javax.net.ssl.SSLHandshakeException:没有合适的协议(协议被禁用或密码套件不合适

SSLHandshakeException:握手期间远程主机关闭连接

Java Mail:在没有 SSL 的端口 25 上发送电子邮件时出现 SSLHandshakeException

处理异常:javax.net.ssl.SSLHandshakeException:没有合适的协议(协议被禁用或密码套件不合适)

如何解决 javax.net.ssl.SSLHandshakeException:java.security.cert.CertificateEx ception:没有名称匹配