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

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

從密碼 aes 256 GCM 戈朗中提取標簽

從密碼 aes 256 GCM 戈朗中提取標簽

Go
qq_遁去的一_1 2022-09-12 20:24:41
我在Ruby中有加密和解密,并嘗試用Go重寫。我一步一步地嘗試,所以從Ruby中的加密開始,并嘗試在go中解密,這是有效的。但是當我嘗試在Go中寫入加密并在Ruby中解密時。當我嘗試提取標簽時,我遇到了困難,我解釋了為什么我需要提取auth標簽的原因加密 在紅寶石中plaintext = "Foo bar"cipher = OpenSSL::Cipher.new('aes-256-gcm')cipher.encryptcipher.iv = iv # string 12 lencipher.key = key # string 32 lencipher.auth_data = # string 12 lencipherText = cipher.update(JSON.generate({value: plaintext})) + cipher.finalauthTag = cipher.auth_taghexString = (iv + cipherText + authTag).unpack('H*').first我嘗試連接一個初始向量,一個密文和身份驗證標簽,所以在解密之前,我可以提取它們,特別是身份驗證標簽,因為我需要在Ruby中調用Cipher#final之前設置它auth_tag必須在調用密碼#解密,密碼#key=和密碼#iv=之后,但在調用密碼#final之前設置標記。執行所有解密后,將在調用密碼#final時自動驗證標記這是高浪中的函數加密ciphertext := aesgcm.Seal(nil, []byte(iv), []byte(plaintext), []byte(authData))src := iv + string(ciphertext) // + try to add authentication tag herefmt.Printf(hex.EncodeToString([]byte(src)))如何提取身份驗證標簽并將其與iv和密文連接起來,以便我可以在ruby中使用解密功能進行解密raw_data = [hexString].pack('H*')cipher_text = raw_data.slice(12, raw_data.length - 28)auth_tag = raw_data.slice(raw_data.length - 16, 16)cipher = OpenSSL::Cipher.new('aes-256-gcm').decryptcipher.iv = iv # string 12 lencipher.key = key # string 32 lencipher.auth_data = # string 12 lencipher.auth_tag = auth_tagJSON.parse(cipher.update(cipher_text) + cipher.final)我希望能夠在Go中進行加密,并嘗試在Ruby中解密。
查看完整描述

2 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

您希望加密流如下所示:


func encrypt(in []byte, key []byte) (out []byte, err error) {


    c, err := aes.NewCipher(key)

    if err != nil {

        return

    }


    gcm, err := cipher.NewGCM(c)

    if err != nil {

        return

    }


    nonce := make([]byte, gcm.NonceSize())

    if _, err = io.ReadFull(rand.Reader, nonce); err != nil {

        return

    }


    out = gcm.Seal(nonce, nonce, in, nil) // include the nonce in the preable of 'out'

    return

}

根據 aes,您的輸入長度應為 16、24 或 32 個字節。新密碼文檔。key


上述函數中的加密字節將包括前綴(長度為 16、24 或 32 個字節) - 因此可以在解密階段輕松提取,如下所示:outnonce


// `in` here is ciphertext

nonce, ciphertext := in[:ns], in[ns:]

其中計算如下:ns


c, err := aes.NewCipher(key)

if err != nil {

    return

}


gcm, err := cipher.NewGCM(c)

if err != nil {

    return

}


ns := gcm.NonceSize()

if len(in) < ns {

    err = fmt.Errorf("missing nonce - input shorter than %d bytes", ns)

    return

}

編輯

如果您使用默認密碼設置進行加密(見上文):go

gcm, err := cipher.NewGCM(c)

中,標記字節大小將為 。16

注意:如果是密碼。使用新GCM標簽大小 - 然后大小顯然會有所不同(基本上在字節之間的任何地方)1216

因此,讓我們假設標簽大小為 ,掌握此知識,并且知道完整的有效負載安排是:16

IV/nonce + raw_ciphertext + auth_tag

解密的一側是有效載荷的最后16個字節;并且raw_ciphertext是 up 之后的所有字節,直到 開始的位置。auth_tagRubyIV/nonceauth_tag


查看完整回答
反對 回復 2022-09-12
?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

aesgcm.Seal自動將 GCM 標記附加到密文的末尾。您可以在源代碼中看到它:


    var tag [gcmTagSize]byte

    g.auth(tag[:], out[:len(plaintext)], data, &tagMask)

    copy(out[len(plaintext):], tag[:])                   // <---------------- here

所以你已經完成了,你不需要其他任何東西。 已返回末尾附加了 auth 標記的密文。gcm.Seal


同樣,您也不需要提取 的 auth 標記,它也會自動執行此操作:gcm.Open


    tag := ciphertext[len(ciphertext)-g.tagSize:]        // <---------------- here

    ciphertext = ciphertext[:len(ciphertext)-g.tagSize]

因此,在解密過程中,您所要做的就是提取IV(nonce)并將其余部分作為密文傳遞。


查看完整回答
反對 回復 2022-09-12
  • 2 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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