" SSLHandshakeException:没有共同的密码套件"

时间:2018-06-10 16:52:18

标签: java ssl encryption

我试图通过SSL安全连接将客户端连接到服务器,但我似乎被抛出了#34; SSLHandshakeExceptin:没有共同的密码套件"每次。我真的可以使用这个帮助。

这是服务器代码:

public ChatServer() {
    usedNames.add("arnold");
    SSLServerSocket ss = null;
    try {
        SecureRandom sr = new SecureRandom();
        String passphrase = "serverpwd";

        //read a file with client certificates
        KeyStore clientKeyStore = KeyStore.getInstance("JKS"); 
        clientKeyStore.load(new FileInputStream("client.public"), "public".toCharArray());

        //reads a file with servers certificate and secret key
        KeyStore serverKeyStore = KeyStore.getInstance("JKS"); 
        serverKeyStore.load(new FileInputStream("server.private"), passphrase.toCharArray());

        // tmf,kmf and sslContext
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(clientKeyStore);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(serverKeyStore, passphrase.toCharArray());
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), sr);

        // Create socket
        SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
        ss = (SSLServerSocket) factory.createServerSocket(serverPort);
        ss.setNeedClientAuth(true); // client needs to have auth.
        ss.setEnabledCipherSuites(new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA"});

    } catch (Exception e) {
        System.err.println("[system] could not create socket on port " + this.serverPort);
        e.printStackTrace(System.err);
        System.exit(1);
    }

    // start listening for new connections
    System.out.println("[system] listening ...");
    try {
        while (true) {
            Socket socket = ss.accept(); // connection established
            ((SSLSocket)socket).startHandshake(); //explicitly trigger SSL handshake
            String username = ((SSLSocket) socket).getSession().getPeerPrincipal().getName();

这是客户端代码:

SecureRandom sr = new SecureRandom();

        KeyStore serverKeyStore = KeyStore.getInstance("JKS");
        serverKeyStore.load(new FileInputStream("server.public"), "public".toCharArray());


        KeyStore clientKeyStore = KeyStore.getInstance("JKS");
        clientKeyStore.load(new FileInputStream(sslPrivate), passphrase.toCharArray());


        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(serverKeyStore);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(clientKeyStore, passphrase.toCharArray());
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), sr);

        // create socket
        SSLSocketFactory sf = sslContext.getSocketFactory();
        socket = (SSLSocket)sf.createSocket("localhost", serverPort);
        socket.setEnabledCipherSuites(new String[] { "TLS_RSA_WITH_AES_128_CBC_SHA" }); //(CipherSuite)
        socket.startHandshake(); // //explicitly trigger SSL handshake

这是在我运行"服务器"的控制台上打印的内容: enter image description here

这是在我运行"客户端"的控制台上: enter image description here

0 个答案:

没有答案