有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

socketjava端到端加密握手

我试图实现一个端到端的加密聊天客户端,用于学习目的,但是我似乎无法让服务器和客户端就彼此的公钥达成一致

据我所知,他们都用对方的公钥对发送给对方的信息进行加密,并用自己的私钥对接收到的消息进行解密,我相信我遇到的问题是我无法同步接收对方的公钥,我做错了什么

客户端:

   private void setupStreams() throws IOException{
            this.output = new ObjectOutputStream(this.connection.getOutputStream());
            this.output.flush();
            this.input = new ObjectInputStream(this.connection.getInputStream());
            this.output.writeObject(this.serverKey);
            this.output.flush();
            try {
                this.serverKey = (PublicKey) this.input.readObject();
                System.out.println("Server Key Received!");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            this.showMessage("\n Streams connected successfully \n", false);
        }

服务器端:

private void setupStreams() throws IOException {
        this.output = new ObjectOutputStream(this.connection.getOutputStream());
        this.output.flush();
        this.input = new ObjectInputStream(this.connection.getInputStream());
        this.output.writeObject(clientKey);
        this.output.flush();

        try {
            this.clientKey = (PublicKey) this.input.readObject();
            System.out.println("Client Key Received!");
            this.showMessage("\n Connection streams setup succsessfully", false);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

通常,我的控制台只输出一条“接收到的密钥”控制台线路(通常是客户端),而不输出另一条。我如何制定握手协议以确保它们都接收到对方的密钥

serverKeyclientKey分别是从服务器发送的公钥和从客户端发送的公钥

密钥最初自动生成为密钥对,公钥变量被连接方的公钥覆盖


共 (0) 个答案