Java SSL 上下文未初始化

Java SSL context not initialised

您好,我正在尝试 运行 SSL 服务器和客户端程序。我首先使用 cmd 命令“keytool -genkey -keystore mySrvKeyStore -keyalg RSA”创建证书。 123456是我填完信息后的密码。我将证书放在与服务器和客户端相同的文件夹中,我 运行 服务器出现此错误: "java.lang.IllegalStateException: SSLContext 未初始化"

服务器:

public class SSLServer {

private static int port = 4000;
private static SSLServerSocketFactory sf;
private static SSLServerSocket ss;

public static void StabilireConexiune(int nrPort) {
    try {
        sf = (SSLServerSocketFactory) SSLServer.getServerSocketFactory();
        ss = (SSLServerSocket) sf.createServerSocket(nrPort);
        System.out.println("Server connected ready to accept new connections at the address " + ss.getLocalPort());
        String[] enable = {"TLS_DH_anon_WITH_AES_128_CBC_SHA"};
        ss.setEnabledCipherSuites(enable);
        String[] cipherSuites = ss.getEnabledCipherSuites();
        System.out.println("CipherSuites: ");
        for (int i = 0; i < cipherSuites.length; i++) {
            System.out.println(cipherSuites[i]);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

private static SSLSocket clientSocket;

public static void ConectareClient(){
    
    try{
        clientSocket = (SSLSocket) ss.accept();
        System.out.println("Client connected succesfully");
        
        InputStream input = clientSocket.getInputStream();
        InputStreamReader inputreader = new InputStreamReader(input);
        BufferedReader br = new BufferedReader(inputreader);
        String string = null;
        
        while( (string = br.readLine()) != null){
            System.out.println(string);
            System.out.flush();
        }

    }catch(Exception ex){
        ex.printStackTrace();
    }
    finally{
        try{
            clientSocket.close();
        }catch(IOException ex){
            ex.printStackTrace();
        }
    }
}

private static ServerSocketFactory getServerSocketFactory() throws NoSuchAlgorithmException{
    
    SSLServerSocketFactory ssf = null;
    try{
        KeyManagerFactory kmf;
        KeyStore ks;
    SSLContext ctx;
        
        char[] passphrase = "123456".toCharArray();
        
        ctx = SSLContext.getInstance("TLS");
        kmf = KeyManagerFactory.getInstance("SunX509");
        ks = KeyStore.getInstance("JKS");
        
        ks.load(new FileInputStream("mySrvKeystore"), passphrase);
        kmf.init(ks, passphrase);
        ctx.getServerSocketFactory();
        
        return ssf;
    }catch(Exception ex){
        ex.printStackTrace();
    }
    return null;
}

public static void main(String args[]){
    if(args.length != 0){
        port = Integer.parseInt(args[0]);
    }
    
    
    StabilireConexiune(port);
    while(true){
        ConectareClient();
    }
}}

客户:

public class SSLClient {

public static void main(String args[]) {
    conectare("127.0.0.1", 4000);
}

private static SSLSocket socket;

public static void conectare(String host, int port) {

    try {
        SSLSocketFactory factory = (SSLSocketFactory) SSLClient.getSocketFactory();
        socket = (SSLSocket) factory.createSocket(host, port);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] enable = {"TLS_DH_anon_WITH_AES_128_CBC_SHA"};
        socket.setEnabledCipherSuites(enable);
        String[] cipherSuites = socket.getEnabledCipherSuites();
        for (int i = 0; i < cipherSuites.length; i++) {
            System.out.println(cipherSuites[i]);
        }
        socket.addHandshakeCompletedListener(new HandshakeCompletedListener() {
            public void handshakeCompleted(HandshakeCompletedEvent event) {
                System.out.println("handshake done");
            }
        });
        socket.startHandshake();
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
        System.out.println("Give a message to the server...");
        String string = br.readLine();
        out.println("Message to the server..." + string);
        out.println();
        out.flush();
    }catch(IOException ex){
        ex.printStackTrace();
    }
    finally{
        try{
            socket.close();
        }catch(IOException ex){
           ex.printStackTrace();
        }
    }
}

private static SocketFactory getSocketFactory(){
    SSLSocketFactory ssf = null; 
    try{
        SSLContext ctx;
        KeyManagerFactory kmf;
        KeyStore ks;
        
        char[] passphrase = "123456".toCharArray();
        
        ctx = SSLContext.getInstance("TLS");
        kmf = KeyManagerFactory.getInstance("SunX509");
        ks = KeyStore.getInstance("JKS");
        
        ks.load(new FileInputStream("mySrvKeystore"), passphrase);
        kmf.init(ks, passphrase);
        ctx.init(kmf.getKeyManagers(), null, null);
        ssf = ctx.getSocketFactory();
        
        return ssf;
    }catch(Exception e){
        e.printStackTrace();
    }
    return null;
}}

请帮帮我,这是什么问题?

错误在这两行:

 SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.getServerSocketFactory();

为什么会抛出这个异常?方法 getServerSocketFactory() 指出:

Throws: IllegalStateException - if the SSLContextImpl requires initialization and the init() has not been called

在客户端中,你确实在调用ctx.getServerSocketFactory();

之前调用了ctx.init(kmf.getKeyManagers(), null, null);

但是在服务器中你不调用它 - 你只初始化 KeyManagerFactory。