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 關注
- 165 瀏覽
添加回答
舉報