给定明文、密文和 IV。我可以找到重复使用的密钥吗?
Given the plaintext, ciphertext, and IV. Can I find a reused key?
大家好
我被困在我的一个编程作业的一部分 类 中。
我相信解决方案非常简单,但无论出于何种原因我无法理解它。
任务的目标是修改密文,使美元金额严格增加。 类加解密给定,不可编辑,加代码攻击。攻击充当中间人并在调用解密之前从加密中检索输出。请注意,密钥是从重复使用的文件中检索的,因此每次加密和解密都使用相同的密钥。我们也可以假设我们知道消息的布局。
我最初的反应是,因为我们知道密钥是相同的,并且因为我们有明文、密文和攻击中的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())
查看使用CBC模式解密的第一个块
PlainText = Decrypt(CipherText, Key) ^ IV.
您知道 PlainText,您知道 IV,并且您有一个要创建的新文本 FakeText。应该很清楚如何将 IV 更改为新的 IV',以便最终得到 FakeText 而不是 PlainText。你不关心解密的结果是什么,只是它是一些常数。
除此之外,就是你的功课了。
大家好
我被困在我的一个编程作业的一部分 类 中。 我相信解决方案非常简单,但无论出于何种原因我无法理解它。
任务的目标是修改密文,使美元金额严格增加。 类加解密给定,不可编辑,加代码攻击。攻击充当中间人并在调用解密之前从加密中检索输出。请注意,密钥是从重复使用的文件中检索的,因此每次加密和解密都使用相同的密钥。我们也可以假设我们知道消息的布局。
我最初的反应是,因为我们知道密钥是相同的,并且因为我们有明文、密文和攻击中的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())
查看使用CBC模式解密的第一个块
PlainText = Decrypt(CipherText, Key) ^ IV.
您知道 PlainText,您知道 IV,并且您有一个要创建的新文本 FakeText。应该很清楚如何将 IV 更改为新的 IV',以便最终得到 FakeText 而不是 PlainText。你不关心解密的结果是什么,只是它是一些常数。
除此之外,就是你的功课了。