最后,我在 StackOverflow 上發(fā)布了我的第一個(gè)問(wèn)題。我已經(jīng)使用這個(gè)網(wǎng)站多年了,我總能找到對(duì)我所有問(wèn)題的很好的答案:)我正在實(shí)現(xiàn)一個(gè)基于官方 Golang 密碼示例的文件加密后臺(tái)守護(hù)程序: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) }以下引用是什么意思。關(guān)于提供安全加密和解密我應(yīng)該注意什么?請(qǐng)注意,此示例很簡(jiǎn)單,因?yàn)樗÷粤藢?duì)加密數(shù)據(jù)的任何身份驗(yàn)證。如果您真的以這種方式使用 StreamReader,攻擊者可以翻轉(zhuǎn)輸出中的任意位。謝謝!
1 回答

嚕嚕噠
TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
來(lái)自維基百科:
分組密碼模式 ECB、CBC、OFB、CFB、CTR 和 XTS 提供機(jī)密性,但它們不能防止意外修改或惡意篡改。
一個(gè)很好的解釋可以在這里找到:https : //security.stackexchange.com/a/33576。
Go 支持其他支持完整性和身份驗(yàn)證檢查的模式。正如 rossum 所說(shuō),您可以使用GCM或CCM。您可以在godoc.org上找到很多示例。例如 HashiCorp 的成員列表庫(kù)。
另一個(gè)庫(kù)值得檢查是NaCl的端口golang.org/x/crypto/nacl:
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
如果您正在處理小消息,這個(gè) API 可能會(huì)更容易使用。
- 1 回答
- 0 關(guān)注
- 267 瀏覽
添加回答
舉報(bào)
0/150
提交
取消