Encryption Golang AES StreamReader加密-示例忽略加密数据的任何身份验证

Encryption Golang AES StreamReader加密-示例忽略加密数据的任何身份验证,encryption,go,aes,encryption-symmetric,Encryption,Go,Aes,Encryption Symmetric,最后,我发布了关于StackOverflow的第一个问题。我使用这个网站已经很多年了,我总能找到我所有问题的答案:) 我正在实现一个文件加密后台守护程序,它基于: 下面的引语是什么意思。关于我应该注意什么来提供安全的加密和解密 注意,这个例子过于简单,因为它 省略加密数据的任何身份验证。如果你真的是 要以这种方式使用StreamReader,攻击者可以任意翻转 输出中的位 谢谢 来自维基百科: 分组密码模式ECB、CBC、OFB、CFB、CTR和XTS提供机密性,但它们不能防止意外修改或恶意篡改

最后,我发布了关于StackOverflow的第一个问题。我使用这个网站已经很多年了,我总能找到我所有问题的答案:)

我正在实现一个文件加密后台守护程序,它基于:

下面的引语是什么意思。关于我应该注意什么来提供安全的加密和解密

注意,这个例子过于简单,因为它 省略加密数据的任何身份验证。如果你真的是 要以这种方式使用StreamReader,攻击者可以任意翻转 输出中的位

谢谢

来自维基百科:

分组密码模式ECB、CBC、OFB、CFB、CTR和XTS提供机密性,但它们不能防止意外修改或恶意篡改

这里可以找到一个很好的解释:

Go支持其他模式,这些模式支持完整性和身份验证检查。正如rossum所说,你可以使用或。你可以在网上找到很多例子。例如HashiCorp公司

另一个值得检查的库是NaCL端口:


如果您处理的是小消息,则此API可能会更易于使用。

对于身份验证,请先查看一下,如果这不适合,那么类似的在线身份验证可能更适合您。太好了!这帮我弄明白了。谢谢,太好了,我现在明白了!在一个小型测试环境中成功地实现了这一点。然而,Go的GCM或CCM实施不可简化。如果我要加密一个大文件,我必须立即将所有内容加载到RAM中。。。NaCL端口还实现了go-crypt-AEAD接口。因此,它也不能流化。有什么建议吗?;)获取代码并使其可流化。请注意,在身份验证之前使用明文是危险的(这可能就是为什么它首先不可流化的原因)。否则,您可以使用例如CTR并对其和IV执行HMAC或CMAC加密-基本上创建您自己的双密码(双密钥!)AEAD密码。不要忘记接受正确的答案。@M4ng0Squ4sh将文件分成若干块,并对每个块进行加密。使用
secretbox
有一个
开销
常量,它将告诉您加密的块将比解密的块大多少。您必须定义一种格式(如memberlist示例),并且不要重用nonce<代码>io.Reader应该相当简单<代码>io。导引头将更加困难。
func ExampleStreamReader() {
    key := []byte("example key 1234")

    inFile, err := os.Open("encrypted-file")
    if err != nil {
        panic(err)
    }
    defer inFile.Close()

    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    // If the key is unique for each ciphertext, then it's ok to use a zero
    // IV.
    var iv [aes.BlockSize]byte
    stream := cipher.NewOFB(block, iv[:])

    outFile, err := os.OpenFile("decrypted-file", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
    if err != nil {
        panic(err)
    }
    defer outFile.Close()

    reader := &cipher.StreamReader{S: stream, R: inFile}
    // Copy the input file to the output file, decrypting as we go.
    if _, err := io.Copy(outFile, reader); err != nil {
        panic(err)
    }

    // Note that this example is simplistic in that it omits any
    // authentication of the encrypted data. If you were actually to use
    // StreamReader in this manner, an attacker could flip arbitrary bits in
    // the output.
}

func ExampleStreamWriter() {
    key := []byte("example key 1234")

    inFile, err := os.Open("plaintext-file")
    if err != nil {
        panic(err)
    }
    defer inFile.Close()

    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    // If the key is unique for each ciphertext, then it's ok to use a zero
    // IV.
    var iv [aes.BlockSize]byte
    stream := cipher.NewOFB(block, iv[:])

    outFile, err := os.OpenFile("encrypted-file", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
    if err != nil {
        panic(err)
    }
    defer outFile.Close()

    writer := &cipher.StreamWriter{S: stream, W: outFile}
    // Copy the input file to the output file, encrypting as we go.
    if _, err := io.Copy(writer, inFile); err != nil {
        panic(err)
    }

    // Note that this example is simplistic in that it omits any
    // authentication of the encrypted data. If you were actually to use
    // StreamReader in this manner, an attacker could flip arbitrary bits in
    // the decrypted result.
}
func Open(out []byte, box []byte, nonce *[24]byte, key *[32]byte) ([]byte, bool)
func Seal(out, message []byte, nonce *[24]byte, key *[32]byte) []byte