未初始化java ssl上下文

piah890a  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(568)

你好,我正在尝试运行ssl服务器和客户端程序。我首先使用cmd命令“keytool-genkey-keystoremysrvkeystore-keyalgrsa”创建一个证书。123456是我填写信息后的密码。我将证书与服务器和客户端放在同一个文件夹中,运行服务器时出现以下错误:“java.lang.illegalstateexception:sslcontext is not initialized”
服务器:

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

请帮帮我,有什么问题吗?

mbskvtky

mbskvtky1#

错误在这两行:

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

为什么会抛出这个异常?方法getserversocketfactory()状态:
抛出:illegalstateexception-如果sslcontextimpl需要初始化,并且尚未调用init()
在客户机中,您确实需要 ctx.init(kmf.getKeyManagers(), null, null); 在你打电话之前 ctx.getServerSocketFactory(); 但是在服务器中,您不调用这个-您只初始化keymanagerfactory。

相关问题