Пример #1
0
def challenge20(path: str) -> bool:
    with open(path) as f:
        lines = f.readlines()

    plain_texts = [base64.b64decode(s) for s in lines]

    secrets = []

    for i in range(len(plain_texts)):
        ctr = AESCipher(AESCipher.MODE_CTR, _KEY, nonce=_NONCE)
        secrets.append(ctr.encrypt(plain_texts[i]))

    max_len = max(len(s) for s in secrets)

    keystream = bytearray()

    for i in range(max_len):
        i = len(keystream)
        char_chain = bytes([s[i] for s in secrets if len(s) > i])
        possible_keys = rank_xor_single_byte_key(char_chain)
        key_byte = possible_keys[_FIX_MAP.get(i, 0)]
        keystream += key_byte

    pt = [xor_byte_arrays(s, keystream) for s in secrets]

    if pt == plain_texts:
        return True

    return False
Пример #2
0
def _edit(cipher_text: bytes, offset: int, new_text: bytes) -> bytes:
    ctr = AESCipher(AESCipher.MODE_CTR, _KEY, nonce=_NONCE)
    plain_text = ctr.decrypt(cipher_text)

    plain_text = plain_text[:offset] + new_text + plain_text[offset +
                                                             len(new_text):]

    return ctr.encrypt(plain_text)
Пример #3
0
    def encrypt_msg(self, msg: bytes) -> Tuple[bytes, bytes]:
        key = sha1(
            self._session_key.to_bytes(
                floor(self._session_key.bit_length() / 8) + 1, "big"))[:16]
        iv = gen_random_bytes(16)

        cbc = AESCipher(AESCipher.MODE_CBC, key, iv=iv)

        return (iv, cbc.encrypt(pkcs7_pad(msg)))
Пример #4
0
def _encryption_oracle(bytes_: bytes) -> Tuple[bytes, str]:
    key = gen_random_bytes(16)
    iv = gen_random_bytes(16)
    prefix = gen_random_bytes(random.randint(5, 10))
    suffix = gen_random_bytes(random.randint(5, 10))
    pt = prefix + bytes_ + suffix

    cbc_mode = random.choice([True, False])

    if cbc_mode:
        cbc = AESCipher(AESCipher.MODE_CBC, key, iv=iv)
        ct = cbc.encrypt(pkcs7_pad(pt))
        answer = "cbc"
    else:
        ecb = AESCipher(AESCipher.MODE_ECB, key)
        ct = ecb.encrypt(pkcs7_pad(pt))
        answer = "ecb"

    return ct, answer
Пример #5
0
def _encrypt_it(path: str) -> (bytes, bytes):
    ecb = AESCipher(AESCipher.MODE_ECB, key=b"YELLOW SUBMARINE")

    with open(path) as f:
        cipher_text = base64.b64decode(f.read())

    plain_text = ecb.decrypt(cipher_text)

    ctr = AESCipher(AESCipher.MODE_CTR, _KEY, nonce=_NONCE)

    cipher_text = ctr.encrypt(plain_text)

    return cipher_text, plain_text
    def test_aes_ecb_encrypt(self):
        # Test vectors from NIST Special Publication 800-38A 2001 Edition

        key = bytes.fromhex("2b7e151628aed2a6abf7158809cf4f3c")
        pt = bytes.fromhex("6bc1bee22e409f96e93d7e117393172a"
                           "ae2d8a571e03ac9c9eb76fac45af8e51"
                           "30c81c46a35ce411e5fbc1191a0a52ef"
                           "f69f2445df4f9b17ad2b417be66c3710")
        ct = bytes.fromhex("3ad77bb40d7a3660a89ecaf32466ef97"
                           "f5d3d58503b9699de785895a96fdbaaf"
                           "43b1cd7f598ece23881b00e3ed030688"
                           "7b0c785e27e8ad3f8223207104725dd4")

        ecb = AESCipher(AESCipher.MODE_ECB, key)

        result = ecb.encrypt(pt)

        assert result == ct, "The result does not match the expected value"
    def test_aes_cbc_encrypt(self):
        # Test vectors from NIST Special Publication 800-38A 2001 Edition

        key = bytes.fromhex("2b7e151628aed2a6abf7158809cf4f3c")
        iv = bytes.fromhex("000102030405060708090a0b0c0d0e0f")
        pt = bytes.fromhex("6bc1bee22e409f96e93d7e117393172a"
                           "ae2d8a571e03ac9c9eb76fac45af8e51"
                           "30c81c46a35ce411e5fbc1191a0a52ef"
                           "f69f2445df4f9b17ad2b417be66c3710")
        ct = bytes.fromhex("7649abac8119b246cee98e9b12e9197d"
                           "5086cb9b507219ee95db113a917678b2"
                           "73bed6b8e3c1743b7116e69e22229516"
                           "3ff1caa1681fac09120eca307586e1a7")

        cbc = AESCipher(AESCipher.MODE_CBC, key, iv=iv)

        result = cbc.encrypt(pt)

        assert result == ct, "The result does not match the expected value"
Пример #8
0
def challenge19() -> bool:
    secrets = []

    for i in range(len(_STRINGS)):
        ctr = AESCipher(AESCipher.MODE_CTR, _KEY, nonce=_NONCE)
        secrets.append(ctr.encrypt(_STRINGS[i]))

    max_len = max(len(s) for s in secrets)

    keystream = bytearray()

    for i in range(max_len):
        char_chain = bytes([s[i] for s in secrets if len(s) > i])
        possible_keys = rank_xor_single_byte_key(char_chain)
        key_byte = possible_keys[_FIX_MAP.get(i, 0)]
        keystream += key_byte

    pt = [xor_byte_arrays(s, keystream) for s in secrets]

    if pt == _STRINGS:
        return True

    return False
    def test_aes_ctr_encrypt(self):
        key = bytes.fromhex("2b7e151628aed2a6abf7158809cf4f3c")
        nonce = bytes.fromhex("f0f1f2f3f4f5f6f7")
        counter = 0xF8F9FAFBFCFDFEFF
        pt = bytes.fromhex("6bc1bee22e409f96e93d7e117393172a"
                           "ae2d8a571e03ac9c9eb76fac45af8e51"
                           "30c81c46a35ce411e5fbc1191a0a52ef"
                           "f69f2445df4f9b17ad2b417be66c3710")
        ct = bytes.fromhex("874d6191b620e3261bef6864990db6ce"
                           "9806f66b7970fdff8617187bb9fffdff"
                           "5ae4df3edbd5d35e5b4f09020db03eab"
                           "1e031dda2fbe03d1792170a0f3009cee")

        cbc = AESCipher(
            AESCipher.MODE_CTR,
            key,
            nonce=nonce,
            counter=counter,
            counter_byteorder="big",
        )

        result = cbc.encrypt(pt)

        assert result == ct, "The result does not match the expected value"
Пример #10
0
def _encrypt(pt: bytes) -> (bytes, bytes):
    iv = gen_random_bytes(16)
    cbc = AESCipher(AESCipher.MODE_CBC, _KEY, iv=iv)
    ct = cbc.encrypt(pkcs7_pad(pt))

    return iv, ct