def main() -> None: with open("data/25.txt", "r") as f: c = b64decode(f.read()) k = b"YELLOW SUBMARINE" cypher = AES.new(k, AES.MODE_ECB) plaintext = cypher.decrypt(c) cyphertext = aes_ctr(plaintext, RANDOM_KEY) rarw_plaintext = break_rarw(cyphertext) assert rarw_plaintext == plaintext print(rarw_plaintext.decode())
def bulk_ctr(cyphertexts: list[bytes]) -> list[bytes]: return [aes_ctr(e, RANDOM_KEY) for e in cyphertexts]
def edit(cyphertext: bytes, key: bytes, offset: int, new_text: bytes) -> bytes: plaintext = aes_ctr(cyphertext, key) plaintext = plaintext[:offset] + new_text \ + plaintext[offset + len(new_text):] return aes_ctr(plaintext, key)
def is_admin(cyphertext: bytes) -> bool: plaintext = aes_ctr(cyphertext, RANDOM_KEY) return "admin=true" in plaintext.decode(errors="replace")
def oracle(userdata: bytes) -> bytes: prefix = b"comment1=cooking%20MCs;userdata=" postfix = b";comment2=%20like%20a%20pound%20of%20bacon" userdata = userdata.replace(b";", b"").replace(b"=", b"") plaintext = pkcs7(prefix + userdata + postfix) return aes_ctr(plaintext, RANDOM_KEY)
def bulk_ctr(f): return [aes_ctr(e, RANDOM_KEY) for e in f]
def ctr_oracle(plaintext: bytes) -> int: """Stream cipher compression oracle""" key = get_random_bytes(BLOCKSIZE) return len(aes_ctr(compress(format_request(plaintext)), key))