1 回答
TA贡献1816条经验 获得超4个赞
您不能使用相同的 RC4 密码来加密然后解密,因为它具有内部状态。
构造一个具有相同密钥的新密码来解密:
// ENCRYPT
c, err := rc4.NewCipher([]byte("dsadsad"))
if err != nil {
log.Fatalln(err)
}
src := []byte("asdsad")
fmt.Println("Plaintext: ", src)
dst := make([]byte, len(src))
c.XORKeyStream(dst, src)
fmt.Println("Ciphertext: ", dst)
// DECRYPT
c2, err := rc4.NewCipher([]byte("dsadsad"))
if err != nil {
log.Fatalln(err)
}
src2 := make([]byte, len(dst))
c2.XORKeyStream(src2, dst)
fmt.Println("Plaintext': ", src2)
这将输出(在Go Playground上尝试):
Plaintext: [97 115 100 115 97 100]
Ciphertext: [98 41 227 117 93 79]
Plaintext': [97 115 100 115 97 100]
但正如包文档所述:
RC4 已被密码破解,不应用于安全应用程序。
因此,请使用另一种更安全的算法,例如crypto/aes.
- 1 回答
- 0 关注
- 141 浏览
添加回答
举报