不稳定的解密,有时会得到密码:消息身份验证失败

问题描述 投票:0回答:1

我正在尝试为我的软件创建E2E加密,但是解密非常不稳定,有时可以成功解密,有时得到cipher: message authentication failed,这是我的加密和解密代码

func Encrypt(data []byte, passphrase string) ([]byte, error) {
    // create aes.NewCipher from hashed md5 passphrase
    block, _ := aes.NewCipher([]byte(createHash(passphrase)))
    //  NewGCM returns the given 128-bit, block cipher wrapped in
    // Galois Counter Mode with the standard nonce length.
    gcm, err := cipher.NewGCM(block)
    if err != nil {
        return nil, err
    }
    // initialize slice with length of nonce that must be passed to Seal and Open.
    nonce := make([]byte, gcm.NonceSize())
    if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
        return nil, err
    }

    ciphertext := gcm.Seal(nonce, nonce, data, nil)
    return ciphertext, nil
}

func Decrypt(data []byte, passphrase string) ([]byte, error) {
    // create md5 byte slice
    key := []byte(createHash(passphrase))
    // just `reverse` algorithm with passphrase until return
    block, err := aes.NewCipher(key)
    if err != nil {
        return nil, err
    }
    gcm, err := cipher.NewGCM(block)
    if err != nil {
        return nil, err
    }
    nonceSize := gcm.NonceSize()
    nonce, ciphertext := data[:nonceSize], data[nonceSize:]
    plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
    if err != nil {
        return nil, err
    }
    return plaintext, nil
}

加密的二进制值通过http传输:

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
    return nil, err
}

decrypt, err := Decrypt(body, r.Passphrase)

我已经尝试进行的检查,ioutil.ReadAll是否正确读取内容,或解密器出现问题

go encryption
1个回答
0
投票
对不起,问题不在于加密/解密,而是在转载自文本代码的http服务中,并且现在已经修复https://github.com/codenoid/GoTral-Server/commit/493c7f654753cae36f074c1c5f382953e227d295
© www.soinside.com 2019 - 2024. All rights reserved.