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

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))

图片转代码服务由CSDN问答提供 功能建议

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

  func AESEncryption(key [] byte,plaintext [] byte)([] byte,error)  {
c,err:= aes.NewCipher(key)
如果err!= nil {
 log.Printf(“生成AES密钥%s时发生错误”,err)
返回nil,err 
} \  n 
 gcm,错误:= cipher.NewGCM(c)
如果错误!=无{
返回nil,错误
} 
 
随机数:= make([] byte,gcm.NonceSize())  
如果_,err = io.ReadFull(rand.Reader,nonce);  err!= nil {
 log.Printf(“在生成AES密钥%s时发生错误”,err)
返回nil,err 
} 
 
返回gcm.Seal(nonce,nonce,明文,nil)  ,nil 
 
} 
 </ code> </ pre> 
 
 

plainText:=“我将成为我应得的,有没有类似freewil的东西?” </ p>

六角编码键码: e629ed98829a893899ddda67f582ede72e2a187dd1ddd5ada54f49cfe2c8675f </ p>

十六进制编码的encypted文本= 9012a33bfb0a51dec4f96404cdd7300ec6afca1fa0d6679a7c036652d014a38faf909e9c44d08dffac121aa85d48b7256fa74542e2545e27dc070adfc03af26f2a32f50c2c311d5c91ff6de2ca3b4347da70669575c9b198f4 Deciphering在golang加密的文本执行所成功地但不是在python </ p> < p>用于python解密的代码。</ p>

 密码= AES.new(binascii.unhexlify(密钥),AES.MODE_GCM)
cipher.nonce = binascii.unhexlify(nonce)  
cipher.decrypt(binascii.unhexlify(hex_encoded_encrypted_hex))
 </ code> </ pre> 
 </ div>

1条回答 默认 最新

相关推荐 更多相似问题