14

我想创建一个应用程序使用SSLSocket:客户端向String服务器发送一个,服务器将其大写String并发送回客户端进行显示。

SSL服务器

public class SSLServer {
    public static void main(String args[]) throws Exception
    {
        try{
        //Creaet a SSLServersocket
        SSLServerSocketFactory factory=(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
        SSLServerSocket sslserversocket=(SSLServerSocket) factory.createServerSocket(1234);
        //Tạo 1 đối tượng Socket từ serversocket để lắng nghe và chấp nhận kết nối từ client
        SSLSocket sslsocket=(SSLSocket) sslserversocket.accept();
        //Tao cac luong de nhan va gui du lieu cua client
        DataInputStream is=new DataInputStream(sslsocket.getInputStream());
        PrintStream os=new PrintStream(sslsocket.getOutputStream());
        while(true)  //khi dang ket noi voi client
        {
            //Doc du lieu den
            String input=is.readUTF();
            String ketqua=input.toUpperCase();
            //Du lieu tra ve
            os.println(ketqua);
        }
        }
        catch(IOException e)
        {
           System.out.print(e);
        }
    }
}

SSL客户端

public class SSLClient {
    public static void main(String args[])
    {
        try
        {
        //Mo 1 client socket den server voi so cong va dia chi xac dinh
        SSLSocketFactory factory=(SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket sslsocket=(SSLSocket) factory.createSocket("127.0.0.1",1234);

        //Tao luong nhan va gui du lieu len server
        DataOutputStream os=new DataOutputStream(sslsocket.getOutputStream());
        DataInputStream is=new DataInputStream(sslsocket.getInputStream());

        //Gui du lieu len server
        String str="helloworld";
        os.writeBytes(str);

        //Nhan du lieu da qua xu li tu server ve
        String responseStr;
        if((responseStr=is.readUTF())!=null)
        {
            System.out.println(responseStr);
        }

        os.close();
        is.close();
        sslsocket.close();
        }
        catch(UnknownHostException e)
        {
             System.out.println(e.getMessage());
        }
        catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
}

运行时SSLServer。它显示此错误:

javax.net.ssl.SSLException: No available certificate or key corresponds 
    to the SSL cipher suites which are enabled

我已经搜索并做了一些方法,但是..你能帮帮我吗?

4

3 回答 3

13

这将生成证书:

keytool -genkey -keystore yourKEYSTORE -keyalg RSA

输入 yourPASSWORD 并使用 ssl 调试信息启动服务器(将 yourKEYSTORE 放入 SSLServer.class 目录):

java -Djavax.net.ssl.keyStore=yourKEYSTORE -Djavax.net.ssl.keyStorePassword=yourPASSWORD -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol -Djavax.net.debug=ssl SSLServer

比启动您的客户端(将您的KEYSTORE 放入带有 SSLClient.class 的目录):

java -Djavax.net.ssl.trustStore=yourKEYSTORE -Djavax.net.ssl.trustStorePassword=yourPASSWORD SSLClient
于 2014-01-22T13:15:12.310 回答
2

@corVaroxid 的回答是正确的。但是如果你想以编程方式设置配置避免全局设置(像我一样),你可以像下面这样(Kotlin):

val password = "yourPassword".toCharArray()

val keyStore = KeyStore.getInstance(File("yourKeystorePath.jks"), password)

val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
trustManagerFactory.init(keyStore)

val keyManagerFactory = KeyManagerFactory.getInstance("NewSunX509")
keyManagerFactory.init(keyStore, password)

val context = SSLContext.getInstance("TLS") //"SSL" "TLS"
context.init(keyManagerFactory.keyManagers, trustManagerFactory.trustManagers, null)

val factory = context.serverSocketFactory

(factory.createServerSocket(LISTENING_PORT) as SSLServerSocket).use { serverSocket ->
    logger.trace("Listening on port: $LISTENING_PORT")

    // ...
}

或在Java中:

final char[] password = "yourPassword".toCharArray();

final KeyStore keyStore = KeyStore.getInstance(new File("yourKeystorePath.jks"), password);

final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);

final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("NewSunX509");
keyManagerFactory.init(keyStore, password);

final SSLContext context = SSLContext.getInstance("TLS");//"SSL" "TLS"
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

final SSLServerSocketFactory factory = context.getServerSocketFactory();

try (SSLServerSocket serverSocket = ((SSLServerSocket) factory.createServerSocket(LISTENING_PORT))) { 
    logger.trace("Listening on port: " + LISTENING_PORT);

    // ...
}
于 2018-12-22T19:07:49.300 回答
0

检查您已安装的证书。确保他们支持您正在协商的密码套件。

于 2012-12-14T07:54:52.023 回答