使用SSLSocket Java创建应用程序

时间:2012-12-14 07:25:39

标签: java sockets ssl jsse

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

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

我有搜索并做某些事情但是......你能帮助我吗?

3 个答案:

答案 0 :(得分: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

比启动客户端(将yourKEYSTORE放入SSLClient.class目录):

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

@corVaroxid's answer是正确的。但是,如果您想以编程方式设置避免全局设置 (像我一样),则可以像下面这样( 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);

    // ...
}