登录
首页 >  Golang >  Go问答

python中的AES-GCM解密

来源:stackoverflow

时间:2024-03-09 08:08:05 441浏览 收藏

大家好,我们又见面了啊~本文《python中的AES-GCM解密》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~

问题内容

我正在尝试解密 aes_gcm 生成的密文。密文是从 golang 中的“crypto/aes”库生成的。现在,我正在尝试使用 cryptodome 库破译 python 中的加密文本。

func aesencryption(key []byte, plaintext []byte)([]byte, error){
   c, err := aes.newcipher(key)
   if err != nil {
      log.printf("error ocurred in generating aes key %s", err)
      return nil,  err
   }

   gcm, err := cipher.newgcm(c)
   if err != nil {
      return nil,  err
   }

  nonce := make([]byte, gcm.noncesize())
  if _, err = io.readfull(rand.reader, nonce); err != nil {
    log.printf("error ocurred in generating aes key %s", err)
      return nil, err
  }

  return gcm.seal(nonce, nonce, plaintext, nil), nil

}

plaintext := "我会成为我应得的,有没有像freewil这样的东西?"

十六进制编码密钥: e629ed98829a893899ddda67f582ede72e2a187dd1ddd5ada54f49cfe2c8675f

十六进制编码的加密文本= 9012a33bfb0a51dec4f96404cdd7300ec6afca1fa0d6679a7c036652d014a38faf909e9c44d08dffac121aa85d48b7256fa74542e2545e27dc070adfc03af26f2a32f50c2 c311d5c91ff6de2ca3b4347da70669575c9b198f4 在golang中破译密文执行成功,但在python中却执行失败。

python解密代码。

cipher = AES.new(binascii.unhexlify(key), AES.MODE_GCM)
cipher.nonce = binascii.unhexlify(nonce)
cipher.decrypt(binascii.unhexlify(hex_encoded_encrypted_hex))

解决方案


对我来说就像一个魅力。

from crypto.cipher import aes
import binascii

key = binascii.unhexlify('e629ed98829a893899ddda67f582ede72e2a187dd1ddd5ada54f49cfe2c8675f')
data = binascii.unhexlify('9012a33bfb0a51dec4f96404cdd7300ec6afca1fa0d6679a7c036652d014a38faf909e9c44d08dffac121aa85d48b7256fa74542e2545e27dc070adfc03af26f2a32f50c2c311d5c91ff6de2ca3b4347da70669575c9b198f4')
nonce, tag = data[:12], data[-16:]
cipher = aes.new(key, aes.mode_gcm, nonce)
cipher.decrypt_and_verify(data[12:-16], tag)

显示

b'I will become what I deserve, Is there anything like freewil?'

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>