亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用加密/rc4

如何使用加密/rc4

Go
手掌心 2022-06-27 11:07:21
我正在嘗試使用 RC4 加密/解密 Go 中的一些數據。我發現 Go 在 crypto/rc4 包中提供了 rc4 算法。我嘗試使用包加密/解密數據,但密文和解密的明文不是我所期望的。我與類似這樣的 RC4 在線工具進行了比較,但我確信 Go 的 rc4 包有一些問題。因為在我用 Go rc4 加密明文并解密密文后,解密的明文'不是我加密的。我應該找到其他圖書館嗎?我運行的代碼是這樣的。package mainimport (    "crypto/rc4"    "fmt"    "log")func main() {    c, err := rc4.NewCipher([]byte("dsadsad"))    if err != nil {        log.Fatalln(err)    }    src := []byte("asdsad")    dst := make([]byte, len(src))    fmt.Println("Plaintext: ", src)    c.XORKeyStream(dst, src)    c.XORKeyStream(src, dst)    fmt.Println("Ciphertext: ", dst)    fmt.Println("Plaintext': ", src)}輸出是這樣的Plaintext:  [97 115 100 115 97 100]Ciphertext:  [98 41 227 117 93 79]Plaintext':  [111 154 128 112 250 88]
查看完整描述

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.


查看完整回答
反對 回復 2022-06-27
  • 1 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號