dongming5444 2018-08-25 21:31
浏览 3275
已采纳

Python中的AES-GCM解密

I am trying to decrypt cipher text generated from AES_GCM. The cipher text was generated from "crypto/aes" library in golang. Now, I am trying to decipher the encrypted text in python using cryptodome library.

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 := "I will become what I deserve, Is there anything like freewil?"

Hex encoded key : e629ed98829a893899ddda67f582ede72e2a187dd1ddd5ada54f49cfe2c8675f

hex encoded encypted text = 9012a33bfb0a51dec4f96404cdd7300ec6afca1fa0d6679a7c036652d014a38faf909e9c44d08dffac121aa85d48b7256fa74542e2545e27dc070adfc03af26f2a32f50c2c311d5c91ff6de2ca3b4347da70669575c9b198f4 Deciphering the encrypted text in golang executes successfully but not in python.

Code for decryption in python.

cipher = AES.new(binascii.unhexlify(key), AES.MODE_GCM)
cipher.nonce = binascii.unhexlify(nonce)
cipher.decrypt(binascii.unhexlify(hex_encoded_encrypted_hex))
  • 写回答

1条回答 默认 最新

  • doumao6048 2018-08-25 23:02
    关注

    Worked like a charm for me.

    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)
    

    displays

    b'I will become what I deserve, Is there anything like freewil?'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格