登录
首页 >  Golang >  Go问答

有时解密不稳定,导致消息认证失败

来源:stackoverflow

时间:2024-03-09 23:21:22 183浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《有时解密不稳定,导致消息认证失败》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

我正在尝试为我的软件创建端到端加密,但是解密非常不稳定,有时可以成功解密,有时得到cipher:消息身份验证失败,这是我的加密和解密代码

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 是否正确读取内容,或者解密器出现问题


解决方案


抱歉,问题不在于加密/解密,而是在于传输chipertext的http服务器,现已修复https://github.com/codenoid/GoTral-Server/commit/493c7f654753cae36f074c1c5f382953e227d295

以上就是《有时解密不稳定,导致消息认证失败》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>