PyCryptodome AES,'EcbMode'对象没有属性'encrypt_and_digest'



我使用Pycharm Text EditorPyCryptodome Library使用Advanced Encryption Standard (AES)对消息进行加密。它也是最常用的CCD_ 4之一。我的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

如果您阅读文档,您将看到encrypt_and_digest()仅受现代模式支持:

  • MODE_CCM
  • MODE_EAX
  • MODE_GCM
  • MODE_SIV
  • MODE_OCB

ECB模式真的不应该用于任何事情,因为它在语义上不安全。

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():-此模块用于生成MessageMAC Tag
# 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'xf1x9axc1x12xdcI7xc8xe4xcfx1e5xe4x93ixc4'
Length of the Message:- 16
Encryption of Message Using AES:- b'x97x0e+xcb^x82xeelhs2_x90mx1c+'
MAC Tag of our Encrypted Message is:- b'c!xb2xf4x82xceT3x0cM1x04x87(y?'

2.不改变模式:-

如果要使用ECB (Electronic Code Book)模式进行Encrypt。则可以使用CCD_ 35和CCD_。

使用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'x94x88oxf0x8fxbexecx0ex1exdfx06Axdf<xbexe3'
Length of the Message:- 16
Encryption of Message Using AES:- b'xf6c)xeexeax13xdcXx9cx06Ex82~{cxc6'
Hex of Cipher Text:- f66329eeea13dc589c0645827e7b63c6
The decryption of Cipher Text Using AES:- b'Stack Overflow  

希望此解决方案能帮助您。

相关内容

  • 没有找到相关文章

最新更新