PyCryptodome AES,'EcbMode' 对象没有属性 'encrypt_and_digest'
PyCryptodome AES, 'EcbMode' object has no attribute 'encrypt_and_digest'
我正在使用 Pycharm Text Editor
和 PyCryptodome 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
?
如果您阅读文档,您会发现 encrypt_and_digest()
仅受 modern modes:
支持
MODE_CCM
MODE_EAX
MODE_GCM
MODE_SIV
MODE_OCB
ECB 模式真的不应该用于任何事情,因为它不是 semantically secure。
从Error
我们可以看出encrypt_and_digest()
属性在AES Encryption
的ECB (Electronic Code Book)
模式下不可用。因此,您的查询有两个解决方案,让我们一一查看:-
1.通过改变模式:-
通过将 mode
更改为 modern modes
我们可以使用 encrypt_and_digest()
模块。基本上 encrypt_and_digest()
模块是 encrypt()
和 digest()
模块的组合。
encrypt()
:- 这个模块用于 Encrypt
你的 Message
digest()
:- 此模块用于生成 MAC Tag
of Message
# 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。不改变模式:-
如果您想 Encrypt
使用 ECB (Electronic Code Book)
模式。然后我们可以使用 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
希望此解决方案对您有所帮助。
我正在使用 Pycharm Text Editor
和 PyCryptodome 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
?
如果您阅读文档,您会发现 encrypt_and_digest()
仅受 modern modes:
MODE_CCM
MODE_EAX
MODE_GCM
MODE_SIV
MODE_OCB
ECB 模式真的不应该用于任何事情,因为它不是 semantically secure。
从Error
我们可以看出encrypt_and_digest()
属性在AES Encryption
的ECB (Electronic Code Book)
模式下不可用。因此,您的查询有两个解决方案,让我们一一查看:-
1.通过改变模式:-
通过将 mode
更改为 modern modes
我们可以使用 encrypt_and_digest()
模块。基本上 encrypt_and_digest()
模块是 encrypt()
和 digest()
模块的组合。
encrypt()
:- 这个模块用于Encrypt
你的Message
digest()
:- 此模块用于生成MAC Tag
ofMessage
# 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。不改变模式:-
如果您想 Encrypt
使用 ECB (Electronic Code Book)
模式。然后我们可以使用 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
希望此解决方案对您有所帮助。