问题描述
大家好
我被困在我的编程课程之一的作业中。 我认为该解决方案非常简单,但是出于任何原因,我都无法掌握。
分配的目的是修改密文,以便严格增加美元金额。给出了加密和解密的类,并且无法对其进行编辑,而添加了代码以进行攻击。攻击充当中间人,并在调用解密之前从加密检索输出。请注意,密钥是从重用的文件中检索的,因此,每次加密和解密都使用相同的密钥。我们还可以假设我们知道消息的布局。
我的最初反应是,因为我们知道密钥是相同的,并且因为我们具有攻击的明文,密文和IV,所以必须有一种简单的解决方案来修改密文。我尝试在修改密文后计算新标签,但是密文取决于先前的输入(IV),因此无法正常工作。我是否可以假设有一个相对简单的解决方案?
注意:不一定要寻找完全编码的答案,只是希望在解决此问题的方法上有一些指导。电讯局长办公时间杂乱无章地在线:(
谢谢!
from django.forms import URLInput,TextInput
#### 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,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())
解决方法
看看使用CBC模式解密的第一个块
PlainText = Decrypt(CipherText,Key) ^ IV.
您知道IV的PlainText,并且有一个要创建的新文本FakeText。应该很清楚如何将IV更改为新的IV',以便最终得到FakeText而不是PlainText。您不在乎解密的结果是什么,只是它是恒定的。
除此之外,这是您的作业。