def test_solve(): with open("files/10.txt") as f: ct = base64.b64decode(f.read()) with open("files/play_that_funky_music.txt") as f: pt = f.read() k, iv = b"YELLOW SUBMARINE", bytes(16) assert util.AesCbcCipher(k, iv).decrypt(ct).decode() == pt
def encrypt(pt): pt = junk() + pt + junk() k, iv = util.rbytes(16), util.rbytes(16) if util.rbool(): return "ECB", util.AesEcbCipher(k).encrypt(pt) else: return "CBC", util.AesCbcCipher(k, iv).encrypt(pt)
def is_padding_ok(self, token): iv, ct = token[:16], token[16:] try: _ = util.AesCbcCipher(self.key, iv).decrypt(ct) except ValueError: return False else: return True
def test_aes_cbc_cipher(): for n in range(1, 33): k, iv, pt = util.rbytes(16), util.rbytes(16), util.rbytes(n) cipher = util.AesCbcCipher(k, iv) assert cipher.decrypt(cipher.encrypt(pt)) == pt
def __init__(self): self.key = util.rbytes(16) self.cbc = util.AesCbcCipher(self.key, self.key)
def create_token(self): iv, pt = util.rbytes(16), util.rchoice(self.secrets) return iv + util.AesCbcCipher(self.key, iv).encrypt(pt)
def create_token(self, userdata: str) -> bytes: if ";" in userdata or "=" in userdata: raise ValueError("invalid userdata") s = f"comment1=cooking%20MCs;userdata={userdata};comment2=%20like%20a%20pound%20of%20bacon" iv = util.rbytes(16) return iv + util.AesCbcCipher(self.key, iv).encrypt(s.encode())
def is_admin(self, token: bytes): iv, ct = token[:16], token[16:] return b";admin=true;" in util.AesCbcCipher(self.key, iv).decrypt(ct)