【发布时间】:2021-01-05 20:31:16
【问题描述】:
大家好
我被困在我的一门编程课程的作业中。 我相信解决方案非常简单,但无论出于何种原因,我都无法理解。
任务的目标是修改密文,使美元金额严格增加。加密和解密的类是给定的,不能编辑,代码被添加到攻击中。攻击充当中间人,并在调用解密之前从 encrypt 检索输出。请注意,密钥是从重复使用的文件中检索的,因此每次加密和解密都使用相同的密钥。我们也可以假设我们知道消息的布局。
我最初的反应是,因为我们知道密钥是相同的,并且因为我们在攻击中有明文、密文和 IV,所以必须有一个简单的解决方案来修改密文。我在修改密文后尝试计算新标签,但密文取决于先前的输入(IV),因此不起作用。我是否认为有一个相对简单的解决方案?
注意:不一定要寻找完全编码的答案,只需要一些有关解决此问题的方法的指导。 TA 的办公时间与网上的一切都一团糟 :(
谢谢!
#### EXAMPLE KEY #####
2D7F8E92A8E7109258C879F878E12387
######################
class encrypt
import sys
import os
import Crypto.Cipher.AES
import hashlib
f = open(sys.argv[1], 'r')
key = f.readline()
key = bytes.fromhex(key[:32])
f.close()
message = \
"""AMOUNT: $ 10.00
Originating Acc Holder: Doe
Orgininating Acc #82123-098370
I authorized the above amount to be transferred to the account #38108-443280
held by a student at the National Bank of the Cayman Islands.
"""
iv = os.urandom(16)
cipher = Crypto.Cipher.AES.new(key, Crypto.Cipher.AES.MODE_CBC, IV=iv)
ciphertext = cipher.encrypt(message.encode()).hex()
tag = hashlib.sha256(message.encode()).hexdigest()
print(iv.hex() + ciphertext + tag)
class decrypt
import sys
import Crypto.Cipher.AES
import hashlib
f = open(sys.argv[1], 'r')
key = f.readline()
key = bytes.fromhex(key[:32])
f.close()
ciphertextWithTag = bytes.fromhex(sys.argv[2]) # bytes.fromhex($CT)
if len(ciphertextWithTag) < 16+16+32:
print("Ciphertext is too short!")
sys.exit(0)
iv = ciphertextWithTag[:16]
ciphertext = ciphertextWithTag[:len(ciphertextWithTag)-32] # with iv
tag = ciphertextWithTag[len(ciphertextWithTag)-32:]
cipher = Crypto.Cipher.AES.new(key, Crypto.Cipher.AES.MODE_CBC, IV=iv)
plaintext = cipher.decrypt(ciphertext[16:]) # here [16:] has spare apart iv
if tag.hex() != hashlib.sha256(plaintext).hexdigest():
print("Invalid tag!")
else:
print("Verified message")
print(plaintext.decode())
class attack
import sys
ciphertextWithTag = bytes.fromhex(sys.argv[1])
if len(ciphertextWithTag) < 16+16+32:
print("Ciphertext is too short!")
sys.exit(0)
iv = ciphertextWithTag[:16]
ciphertext = ciphertextWithTag[:len(ciphertextWithTag)-32]
tag = ciphertextWithTag[len(ciphertextWithTag)-32:]
# TODO: Modify the input so the transfer amount is more lucrative to the recipient
# TODO: Print the new encrypted message
# you can change the print content if necessary
print(ciphertext.hex() + tag.hex())
【问题讨论】:
标签: python encryption cryptography aes sha