在 Python 中使用 AES GCM 解密加密邮件
Decrypt an encrypted message with AES GCM in Python
我使用AES方法加密了一个从txt文件中调用的句子。我使用了 GCM 模式并创建了一个特定的密钥。一切正常(代码如下)。
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import scrypt
from Crypto.Util.number import long_to_bytes
number = 1
flag = open("sentance.txt", "rb").read()
key = scrypt(long_to_bytes(number), b"code", 32, N = 2 ** 10, r = 8, p = 1)
HexMyKey = key.hex()
cipher = AES.new(key, AES.MODE_GCM)
ciphertext, tag = cipher.encrypt_and_digest(flag)
enc = cipher.nonce + ciphertext + tag
HexEncryptedOriginalMessage = enc.hex()
我尝试实现解密过程,也就是说我只有密钥(HexMyKeyvalue)和加密消息(HexEncryptedOriginalMessage 值),我想解密它。
但问题是我错过了一些东西..
我写了下面的代码,但我收到了错误消息。
TypeError: decrypt_and_verify() missing 1 required positional
argument: 'received_mac_tag
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import scrypt
from Crypto.Util.number import long_to_bytes
key = bytes.fromhex(HexMykey)
data = bytes.fromhex(HexEncryptedOriginalMessage)
cipher = AES.new(key, AES.MODE_GCM)
dec = cipher.decrypt_and_verify(data)
你知道我如何解密那封加密的原始邮件吗?
任何帮助将不胜感激!
PyCryptodome 有很好的文档。 GCM 示例 there 使用 JSON 作为 concatenating/separating 随机数、密文和标签,但原理是相同的,可以轻松应用于您的代码。
由于您正在使用 隐式 派生的随机数,请注意 PyCryptodome 应用 16 字节的随机数。但是请注意,GCM 的建议是 12 字节的随机数(s. here,Note 部分)。
以下解密示例使用您发布的加密代码创建的密钥和密文:
from Crypto.Cipher import AES
HexMyKey = '6f9b706748f616fb0cf39d274638ee29813dbad675dd3d976e80bde4ccd7546a'
HexEncryptedOriginalMessage = '6b855acc799213c987a0e3fc4ddfb7719c9b87fcf0a0d35e2e781609143b6e2d8e743cf4aea728002a9fc77ef834'
key = bytes.fromhex(HexMyKey)
data = bytes.fromhex(HexEncryptedOriginalMessage)
cipher = AES.new(key, AES.MODE_GCM, data[:16]) # nonce
try:
dec = cipher.decrypt_and_verify(data[16:-16], data[-16:]) # ciphertext, tag
print(dec) # b'my secret data'
except ValueError:
print("Decryption failed")
如果身份验证失败,decrypt_and_verify()
生成 ValueError
。
PyCryptodome 还允许 GCM 解密无需 事先身份验证:
cipher = AES.new(key, AES.MODE_GCM, data[:16]) # nonce
dec = cipher.decrypt(data[16:-16]) # ciphertext
print(dec) # b'my secret data'
但是,出于安全原因,不应对 GCM 执行此操作,因为密文仅在成功验证后才可信。
另外,加解密代码有些不一致,加密使用scrypt作为密钥推导函数,解密直接使用推导密钥。通常,人们会期望在解密过程中也导出密钥。可能您使用此快捷方式只是为了测试目的。
我使用AES方法加密了一个从txt文件中调用的句子。我使用了 GCM 模式并创建了一个特定的密钥。一切正常(代码如下)。
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import scrypt
from Crypto.Util.number import long_to_bytes
number = 1
flag = open("sentance.txt", "rb").read()
key = scrypt(long_to_bytes(number), b"code", 32, N = 2 ** 10, r = 8, p = 1)
HexMyKey = key.hex()
cipher = AES.new(key, AES.MODE_GCM)
ciphertext, tag = cipher.encrypt_and_digest(flag)
enc = cipher.nonce + ciphertext + tag
HexEncryptedOriginalMessage = enc.hex()
我尝试实现解密过程,也就是说我只有密钥(HexMyKeyvalue)和加密消息(HexEncryptedOriginalMessage 值),我想解密它。
但问题是我错过了一些东西..
我写了下面的代码,但我收到了错误消息。
TypeError: decrypt_and_verify() missing 1 required positional argument: 'received_mac_tag
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import scrypt
from Crypto.Util.number import long_to_bytes
key = bytes.fromhex(HexMykey)
data = bytes.fromhex(HexEncryptedOriginalMessage)
cipher = AES.new(key, AES.MODE_GCM)
dec = cipher.decrypt_and_verify(data)
你知道我如何解密那封加密的原始邮件吗?
任何帮助将不胜感激!
PyCryptodome 有很好的文档。 GCM 示例 there 使用 JSON 作为 concatenating/separating 随机数、密文和标签,但原理是相同的,可以轻松应用于您的代码。
由于您正在使用 隐式 派生的随机数,请注意 PyCryptodome 应用 16 字节的随机数。但是请注意,GCM 的建议是 12 字节的随机数(s. here,Note 部分)。
以下解密示例使用您发布的加密代码创建的密钥和密文:
from Crypto.Cipher import AES
HexMyKey = '6f9b706748f616fb0cf39d274638ee29813dbad675dd3d976e80bde4ccd7546a'
HexEncryptedOriginalMessage = '6b855acc799213c987a0e3fc4ddfb7719c9b87fcf0a0d35e2e781609143b6e2d8e743cf4aea728002a9fc77ef834'
key = bytes.fromhex(HexMyKey)
data = bytes.fromhex(HexEncryptedOriginalMessage)
cipher = AES.new(key, AES.MODE_GCM, data[:16]) # nonce
try:
dec = cipher.decrypt_and_verify(data[16:-16], data[-16:]) # ciphertext, tag
print(dec) # b'my secret data'
except ValueError:
print("Decryption failed")
如果身份验证失败,decrypt_and_verify()
生成 ValueError
。
PyCryptodome 还允许 GCM 解密无需 事先身份验证:
cipher = AES.new(key, AES.MODE_GCM, data[:16]) # nonce
dec = cipher.decrypt(data[16:-16]) # ciphertext
print(dec) # b'my secret data'
但是,出于安全原因,不应对 GCM 执行此操作,因为密文仅在成功验证后才可信。
另外,加解密代码有些不一致,加密使用scrypt作为密钥推导函数,解密直接使用推导密钥。通常,人们会期望在解密过程中也导出密钥。可能您使用此快捷方式只是为了测试目的。