def crypt(plaintext, seed): mt = MersenneTwister(seed) keystream = b"" for i in range(0, len(plaintext), 4): keystream += mt.extract_number().to_bytes(length=4, byteorder="little") return xor(plaintext, keystream)
def extract_key(ciphertext, decrypter): ciphertext = list(ciphertext) ciphertext[16:32] = [0] * 16 ciphertext = bytes(ciphertext) try: decrypter(ciphertext) except InvalidASCII as e: plaintext = e.args[0] return xor(plaintext[:16], plaintext[32:48])
def cbc_decrypt(ciphertext, key, iv=(b"\x00" * 16)): cipher = AES.new(key, mode=AES.MODE_ECB) plaintext = b"" prevblock = iv for i in range(0, len(ciphertext), 16): cipherblock = ciphertext[i:i + 16] plainblock = xor(cipher.decrypt(cipherblock), prevblock) plaintext += plainblock prevblock = cipherblock return plaintext
def cbc_encrypt(plaintext, key, iv=(b"\x00" * 16)): cipher = AES.new(key, mode=AES.MODE_ECB) ciphertext = b"" prevblock = iv for i in range(0, len(plaintext), 16): plainblock = pkcs7_pad(plaintext[i:i + 16]) cipherblock = cipher.encrypt(xor(prevblock, plainblock)) ciphertext += cipherblock prevblock = cipherblock return ciphertext
def solve_ciphertexts(ciphertexts): ct_length, trunc_ciphertexts = truncate_ciphertexts(ciphertexts) transposed_cts = transpose_blocks(trunc_ciphertexts, ct_length) keystream = bytes( select_option(get_options(block))[0] for block in transposed_cts) return [xor(cycle(keystream), ct) for ct in trunc_ciphertexts]
def ctr_crypt(text, key, nonce): return xor(text, get_keystream(key, nonce, len(text)))