1 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
您不能使用相同的 RC4 密碼來(lái)加密然后解密,因?yàn)樗哂袃?nèi)部狀態(tài)。
構(gòu)造一個(gè)具有相同密鑰的新密碼來(lái)解密:
// 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 已被密碼破解,不應(yīng)用于安全應(yīng)用程序。
因此,請(qǐng)使用另一種更安全的算法,例如crypto/aes.
- 1 回答
- 0 關(guān)注
- 154 瀏覽
添加回答
舉報(bào)