使用 SSLSocket Java 创建应用

2022-09-04 03:02:54

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

SSLServer

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);
        }
    }
}

SSLClient

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

我有搜索并做一些方法,但是..你可以帮我吗。


答案 1

这将生成证书:

keytool -genkey -keystore yourKEYSTORE -keyalg RSA

输入您的PASSWORD,然后使用SSL调试信息启动服务器(将您的密钥STORE放入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

比启动你的客户端(把你的密钥库放到SSLClient的目录中.class):

java -Djavax.net.ssl.trustStore=yourKEYSTORE -Djavax.net.ssl.trustStorePassword=yourPASSWORD SSLClient

答案 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);

    // ...
}

推荐