【发布时间】:2021-07-12 15:48:49
【问题描述】:
我在 Ruby 中进行了加密和解密,并尝试用 Go 重写。我一步一步尝试,所以从 ruby 中的加密开始,然后尝试在 go 中解密,它是有效的。但是当我尝试在 Go 中编写 encryption 并在 ruby 中解密时。我在尝试提取标签时卡住了,我解释了我需要提取身份验证标签的原因
红宝石中的加密
plaintext = "Foo bar"
cipher = OpenSSL::Cipher.new('aes-256-gcm')
cipher.encrypt
cipher.iv = iv # string 12 len
cipher.key = key # string 32 len
cipher.auth_data = # string 12 len
cipherText = cipher.update(JSON.generate({value: plaintext})) + cipher.final
authTag = cipher.auth_tag
hexString = (iv + cipherText + authTag).unpack('H*').first
我尝试连接初始向量、密文和身份验证标签,因此在解密之前我可以提取它们,尤其是身份验证标签,因为我需要在 Ruby 中调用 Cipher#final 之前设置它
必须在调用 Cipher#decrypt、Cipher#key= 和 Cipher#iv=,但在调用 Cipher#final 之前。毕竟解密是 执行,标签在调用中自动验证 密码#final
这里是golang中的函数加密
ciphertext := aesgcm.Seal(nil, []byte(iv), []byte(plaintext), []byte(authData))
src := iv + string(ciphertext) // + try to add authentication tag here
fmt.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').decrypt
cipher.iv = iv # string 12 len
cipher.key = key # string 32 len
cipher.auth_data = # string 12 len
cipher.auth_tag = auth_tag
JSON.parse(cipher.update(cipher_text) + cipher.final)
我希望能够在 Go 中进行加密,并尝试在 Ruby 中解密。
【问题讨论】:
-
你能阐明你的最终目标吗?一开始你说
I have encryption and decryption in Ruby and try to rewrite with Go.- 但最后你想decrypt with decryption function in ruby。您是否在go端加密 - 然后想在ruby端解密? -
@colm.anseo 是的..如果你看到我一步一步地讲述,我的意思是我不能用大爆炸过程,所以我已经成功地使用 ruby 进行加密并使用 go for解密,我想尝试与之相反,使用 go 进行加密并使用 ruby 在准确的时间进行解密