我正在编写一个非常简单的脚本,它将根据这个公式解密一个文本字节,P′2[K] = Pn[K] ⊕ Cn-1[K] ⊕ C′[K]
oracle 是一个简单的函数,它解密然后检查最后一个字节是否等于 padding 0x15
。p'2[k] 只是 0x15(填充大小)
def decrypt(cipher):
dec = aes_context.decryptor()
text = dec.update(cipher)
if text[-1] == 0x15:
return True, "Padding Match"
else:
return False, "No Match"
但行为似乎 Undefined 。该循环是一个从 0-> 255 的简单循环(尝试解密一个块的次数)
number = 0x01
index = 0
while index < 255:
try_this_block = 0x0.to_bytes(7, "big") + number.to_bytes(1, "big")
mod_ciphertext = try_this_block + c1
state, error_text = decrypt(mod_ciphertext)
if state:
byte = try_this_block[-1] ^ 0x15 ^ c1[-1]
text_back += byte.to_bytes(1, "big")
break
else:
number += 1
index += 1
被加密的消息只是 8 字节字符串 + 8 字节填充,并且每次都使用相同的密钥和 IV 进行解密。与c1,c2对应m1,m2的密文
m1 = b"khaled G"
m2 = 0x00.to_bytes(7, "big") + 0x015.to_bytes(1, "big")
完整的源代码在这里:
from cryptography.hazmat.primitives.ciphers import algorithms, modes, Cipher
from cryptography.hazmat.backends import default_backend
import os
m1 = b"khaled G"
m2 = 0x00.to_bytes(7, "big") + 0x015.to_bytes(1, "big")
aes_context = Cipher(algorithms.AES(os.urandom(16)), modes.CBC(os.urandom(16)), default_backend())
enc = aes_context.encryptor()
c1 = enc.update(m1)
c2 = enc.update(m2)
c1, c2 = c2[0:8], c2[8:]
def decrypt(cipher):
dec = aes_context.decryptor()
text = dec.update(cipher)
if text[-1] == 0x15:
return True, "Padding Match"
else:
return False, "No Match"
text_back = b""
number = 0x01
index = 0
while index < 255:
try_this_block = 0x0.to_bytes(7, "big") + number.to_bytes(1, "big")
mod_ciphertext = try_this_block + c1
state, error_text = decrypt(mod_ciphertext)
if state:
byte = try_this_block[-1] ^ 0x15 ^ c1[-1]
text_back += byte.to_bytes(1, "big")
break
else:
number += 1
index += 1
print("text is {}".format(text_back))