Encryption ValueError:MAC检查失败
我正在做一个超级小而简单的程序,它试图更多地理解这个模块,但是发生了一些非常奇怪的事情 下面的代码返回:Encryption ValueError:MAC检查失败,encryption,python,pycrypto,Encryption,Python,Pycrypto,我正在做一个超级小而简单的程序,它试图更多地理解这个模块,但是发生了一些非常奇怪的事情 下面的代码返回: Traceback (most recent call last): File ".\crypto.py", line 100, in <module> init = Emisor() File ".\crypto.py", line 15, in __init__ self.encrypt() File ".\crypto.py", line 71, in encrypt Re
Traceback (most recent call last):
File ".\crypto.py", line 100, in <module> init = Emisor()
File ".\crypto.py", line 15, in __init__ self.encrypt()
File ".\crypto.py", line 71, in encrypt Receptor(cipher_text,tag_mac,self.key)
File ".\crypto.py", line 84, in __init__ self.decrypt(self.cipher_text,self.tag_mac,self.key)
File ".\crypto.py", line 93, in decrypt plain_text = cipher.decrypt_and_verify(cipher_text,tag_mac)
File "C:\Users\EQUIPO\AppData\Local\Programs\Python\Python37-32\lib\site-packages\Crypto\Cipher\_mode_gcm.py", line 569, in decrypt_and_verify self.verify(received_mac_tag)
File "C:\Users\EQUIPO\AppData\Local\Programs\Python\Python37-32\lib\site-packages\Crypto\Cipher\_mode_gcm.py", line 510, in verify raise ValueError("MAC check failed")
ValueError: MAC check failed
你忘了转移临时货币。nonce可以以普通格式传输,也不需要在AAD中,因为nonce会自动验证AEAD密码(如GCM) 从文件中:
nonce
(字节)–固定nonce的值。对于组合消息/密钥,它必须是唯一的如果不存在,库将创建一个随机nonce(AES为16字节长)。
因此,如果要使用默认的随机生成的16字节nonce,则需要检索并传输nonce
然而,由于GCM对于12字节的nonce更安全、更高效,我宁愿自己生成(安全随机)12字节的nonce,并使用/传输它。该库似乎默认为16字节的随机nonce,因为这是AES的块大小。这通常是个好主意,但对GCM来说不是
当然,你不应该简单地发送密钥,秘密密钥应该事先确定。你在一篇帖子中提出了非常不同的问题,请将它们分开。你的主要问题似乎是一个基本的代码疑难解答问题。您需要包含完整的错误(包括跟踪)。我们不会运行人们在这里发布的随机代码。这看起来更像是一个编程问题,而不是一个安全问题。对不起,我已经输入了完整的错误,至于多个嵌套问题(实际上是3),我这样做是因为要发布每个问题,我必须等待40分钟。我也很抱歉。请将屏幕截图替换为错误的副本/粘贴。一个原因是为了事先得到完整的错误(你切断了一束)?我认为对称加密就是这样发送密钥的,为了安全起见,我们也应该使用非对称加密,这种方法叫做混合加密。那么你如何事先做到这一点呢?什么不应该像这样发送?没有收缩。我的意思是,在加密或解密发生之前,需要建立is。然而,目前我没有看到任何关于混合加密或DH、ECIES或任何其他建立密钥的方法的提及,所以我认为我应该提及它。你可以而且需要发送任何东西:AAD,nonce,身份验证标签,但不是密钥,这就是我要说的。也许“prevent”可以删除,我不知道,但我想我们在同一页:)收缩->矛盾,愚蠢的拼写错误
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
class Transmitter():
def __init__(self):
self.random_password = None
self.message_plain = True
self.key = None
self.password_option()
self.text_option()
self.encrypt()
def password_option(self):
while ( self.random_password == None ):
random = input("\nDo you want to generate a random symmetric key? (y/n)\n\n>>> ").strip().lower()
if random == "y":
self.random_password = True
self.random()
elif random == "n":
self.random_password = False
self.random()
else:
pass
def text_option(self):
if self.message_plain:
question = input("\nHow will you enter your message?\n\n[1] file\n\n[2] directly in the program\n\n>>> ").strip()
if question == "1":
path = input(r"\nEnter the file path\n\n>>> ").strip()
name = path.split("\\")[-1]
self.message_plain = open(name,mode = "rb")
elif question == "2":
self.message_plain = input("\nEnter your message\n\n>>> ").strip()
self.message_plain = self.message_plain.encode("utf-8")
def random(self):
if self.random_password:
self.key = get_random_bytes(16)
else:
self.key = input("\nEnter your password\n\n>>> ").strip()
def encrypt(self):
cipher = AES.new(self.key,AES.MODE_GCM)
cipher.update(b"header")
cipher_text,tag_mac = cipher.encrypt_and_digest(self.message_plain)
Receiver(cipher_text,tag_mac,self.key)
class Receiver():
def __init__(self,cipher_text,tag_mac,key):
self.cipher_text = cipher_text
self.tag_mac = tag_mac
self.key = key
self.decrypt(self.cipher_text,self.tag_mac,self.key)
def decrypt(self,cipher_text,tag_mac,key):
#try:
# nonce = aes_cipher.nonce
cipher = AES.new(key,AES.MODE_GCM)
cipher.update(b"header")
plain_text = cipher.decrypt_and_verify(cipher_text,tag_mac)
#except ValueError:
# print("\nAn error has occurred.")
if __name__ == '__main__':
init = Transmitter()