首页 > 解决方案 > 是否有任何标准协议可以通过网络传输 RSA 加密文本?

问题描述

我有一个客户端和一个服务器端代码。我必须从客户端获取一条消息作为输入,对其进行加密并将其发送到服务器。但是当我在服务器端打印这条加密消息时,它与在客户端不一样。这是我这样做的方法:

首先,我使用rsa.EncryptOAEP()函数加密消息。该消息是具有其他成员的结构的成员。我正在使用 gob 包对这个结构进行编码并将其发送到服务器。

这是客户端代码的片段:

func SocketClient() {
    conn, err := net.Dial("tcp", ":9000")
    if err != nil {
        log.Fatalln(err)
    }
    defer conn.Close()
    enc := gob.NewEncoder(conn)
    buff, _ := input_reader.ReadBytes('\n')    //this is the message to be encrypted
    label := []byte("")
    hash := sha256.New()
    ciphertext, _ := rsa.EncryptOAEP(
        hash,
        rand.Reader,
        &receiverPublicKey,
        buff,
        label,
    )
    fmt.Printf("%s",ciphertext) //this is to check if the text is same or not
    block.Message = ciphertext //this is the struct to be sent over network
    enc.Encode(block)
}

这是服务器端代码:

func handleConn(conn net.Conn) {
    defer conn.Close()
    dec := gob.NewDecoder(conn)
    dec.Decode(&block)
    fmt.Printf("%s",block.Message) //it is different from the text printed in client side code
}

当我打印消息时,两边应该是一样的。那么是否有一些标准协议或处理加密文本的东西?

标签: goencryptiontcpclient-serverrsa

解决方案


推荐阅读