PyCryptodome AES,“EcbMode”对象没有属性“encrypt_和_digest”

2024-04-19 05:29:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Pycharm Text EditorPyCryptodome Library对消息进行加密使用Advanced Encryption Standard (AES)。它也是主要使用的Symmetric Encryption Algorithm之一。我的AES Encryption代码如下:

from Crypto.Cipher import AES
key = os.urandom(16)
cipher = AES.new(key, AES.MODE_ECB)
ciphertext, tag = cipher.encrypt_and_digest(message)

我遇到了一个错误:-

AttributeError: 'EcbMode' object has no attribute 'encrypt_and_digest

第行:-

ciphertext, tag = cipher.encrypt_and_digest(message)

我尝试过卸载模块几次,但错误仍然存在。 Pycharm没有用红色下划线标记行,我可以使用ctrl-click进入源代码并查看encrypt_and_digest()函数是否存在

我的问题是:-

为什么代码不能通过编译器? python中是否还有另一个模块可以用来执行AES encryption


Tags: 模块andkey代码textmessagetag错误
2条回答

Error我们可以看到encrypt_and_digest()属性在AES EncryptionECB (Electronic Code Book)模式中不可用。因此,对于您的查询,有两种解决方案让我们逐一查看:-

1。通过更改模式:-

通过将mode更改为modern modes,我们可以使用encrypt_and_digest()模块。基本上encrypt_and_digest()模块是encrypt()digest()模块的组合

  • encrypt():-此模块用于Encrypt您的Message
  • digest():-此模块用于生成{}的{}
# List of 'Modern Modes' was given below:-
1. MODE_EAX
2. MODE_CCM
3. MODE_SIV
4. MODE_GCM
5. MODE_OCB

使用EAX Mode的给定场景的代码如下所述:-

# Import all the Important Libraries
from Crypto.Cipher import AES
import os

# 'pad_message()' function declaration for padding purpose.
# Because 'message' length should be always multiple of '16'
def pad_message(message):
  while len(message) % 16 != 0:
    message = message + " "
  return message

# Initialization of 'Key' and 'Message'
key = os.urandom(16)
message = input("Enter your Message for AES Encryption:- ")

# If the length of the message is not multiple of '16' then pad it
message = pad_message(message)

# Print Message, Key and Length of Message before Encryption Process
print("\nMessage:-", message)
print("Key:-", key)
print("Length of the Message:-", len(message))

# Declare New module for AES Encryption in 'EAX' Mode
cipher = AES.new(key, AES.MODE_EAX)

# Encrypt 'Message' and Generate 'MAC Tag' Using 'encrypt_and_digest()' method
cipher_text, mac_tag = cipher.encrypt_and_digest(message.encode('utf-8'))

# Print Encrypted Message 
print("\nEncryption of Message Using AES:-", cipher_text)
print("MAC Tag of our Encrypted Message is:-", mac_tag)
# Output of Above Code:-
Enter your Message for AES Encryption:- Stack Overflow

Message:- Stack Overflow  
Key:- b'\xf1\x9a\xc1\x12\xdcI7\xc8\xe4\xcf\x1e5\xe4\x93i\xc4'
Length of the Message:- 16

Encryption of Message Using AES:- b'\x97\x0e+\xcb^\x82\xeelhs2_\x90m\x1c+'
MAC Tag of our Encrypted Message is:- b'c!\xb2\xf4\x82\xceT3\x0cM1\x04\x87(y?'

2。不改变模式:-

如果要使用ECB (Electronic Code Book)模式Encrypt。然后我们可以使用encrypt()hex()模块

使用ECB Mode的给定场景的代码如下所述:-

# Import all the Important Libraries
from Crypto.Cipher import AES
import os

# 'pad_message()' function declaration for padding purpose.
# Because 'message' length should be always multiple of '16'
def pad_message(message):
  while len(message) % 16 != 0:
    message = message + " "
  return message

# Initialization of 'Key' and 'Message'
key = os.urandom(16)
message = input("Enter your Message for AES Encryption:- ")

# If the length of the message is not multiple of '16' then pad it
message = pad_message(message)

# Print Message, Key and Length of Message before Encryption Process
print("\nMessage:-", message)
print("Key:-", key)
print("Length of the Message:-", len(message))

# Declare New module for AES Encryption in 'ECB (Electronic Codebook)' Mode
cipher = AES.new(key, AES.MODE_ECB)

# Encrypt 'Message'
cipher_text  = cipher.encrypt(message.encode('utf-8'))

# Print Encrypted Message 
print("\nEncryption of Message Using AES:-", cipher_text)
print("Hex of Cipher Text:-", cipher_text.hex())

# Verify by decrypting Cipher Text whether you are recieving same message or not
decrypted_message = cipher.decrypt(cipher_text)
print("\nDecryption of Cipher Text Using AES:-", decrypted_message)
# Output of Above Code:-
Enter your Message for AES Encryption:- Stack Overflow

Message:- Stack Overflow  
Key:- b'\x94\x88o\xf0\x8f\xbe\xec\x0e\x1e\xdf\x06A\xdf<\xbe\xe3'
Length of the Message:- 16

Encryption of Message Using AES:- b'\xf6c)\xee\xea\x13\xdcX\x9c\x06E\x82~{c\xc6'
Hex of Cipher Text:- f66329eeea13dc589c0645827e7b63c6

The decryption of Cipher Text Using AES:- b'Stack Overflow  

希望此解决方案对您有所帮助

如果阅读文档,您将看到encrypt_and_digest()仅受modern modes支持:

  • MODE_CCM
  • MODE_EAX
  • MODE_GCM
  • MODE_SIV
  • MODE_OCB

ECB模式真的不应该用于任何事情,因为它不是semantically secure

相关问题 更多 >