Ejemplo n.º 1
0
def chal6():
    """
    The text in input/6.txt has been base64 encoded after being
    encrypted with repeating-key XOR. Decrypt it.
    """
    f = open('input/6.txt')
    ciphertext = utils.base64_to_bytes(f.read())

    best_english_score = 0
    result = ''

    keysizes = utils.find_possible_keysizes(ciphertext, 3, 2, 40)
    for keysize in keysizes:
        key = [0] * keysize

        # Split the ciphertext into blocks of length keysize
        blocks = [ciphertext[i:i+keysize] for i in range(0,
    len(ciphertext), keysize)]

        # Transpose the blocks
        transposed = [bytes(t) for t in zip_longest(*blocks,
        fillvalue=0)]

        # Treat each block as if it's been encrypted with a
        # repeating-key XOR
        for i in range(0, len(transposed)-1):
            key[i] = utils.get_popular_byte(transposed[i])

        plaintext = _crypto.xor_repeating_key(ciphertext, bytes(key))
        english_score = utils.english_score(plaintext.decode('utf-8'))

        if english_score > best_english_score:
            result = plaintext.decode('utf-8')
Ejemplo n.º 2
0
        res.append(xor(aes_ecb_decode(block, password), blocks[i - 1]))
    return b''.join(res)


def aes_cbc_encode(plaintext: bytes, password: bytes, iv: bytes) -> bytes:
    blocks = split_into_groups(plaintext, 16)
    res = []
    prev_block = iv
    for block in blocks:
        prev_block = aes_ecb_encode(xor(prev_block, block), password)
        res.append(prev_block)
    return b''.join(res)


print('Set 2')
print('Challenge 9')
res9 = pad_with_pkcs7(b'YELLOW SUBMARINE', 20)
assert res9 == b'YELLOW SUBMARINE\x04\x04\x04\x04'
print(res9)

print('Challenge 10')
ciphertext10 = base64_to_bytes(get_file('10.txt'))
password10 = b'YELLOW SUBMARINE'
iv = b'\x00' * 16
res10 = aes_cbc_decode(ciphertext10, password10, iv).decode('ascii')
assert res10.startswith("I'm back and I'm ringin' the bell")
print(res10)
# Check that encrypting is the opposite of decrypting
test_ciphertext10 = aes_cbc_encode(res10.encode('ascii'), password10, iv)
assert test_ciphertext10 == ciphertext10
Ejemplo n.º 3
0
def main():
    res1 = hex_to_base64(
        '49276d206b696c6c696e6720796f757220627261696e206c6'
        '96b65206120706f69736f6e6f7573206d757368726f6f6d')
    print('Task 1')
    print(res1)
    assert res1 == (b'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc'
                    b'29ub3VzIG11c2hyb29t')

    print('Task 2')
    x = hex_to_bytes('1c0111001f010100061a024b53535009181c')
    y = hex_to_bytes('686974207468652062756c6c277320657965')
    res2 = bytes_to_hex(xor(x, y))
    print(res2)
    assert res2 == '746865206b696420646f6e277420706c6179'

    print('Task 3')
    ciphertext = hex_to_bytes('1b37373331363f78151b7f2b783431333d78397828372d'
                              '363c78373e783a393b3736')
    res3 = decode_1_byte_xor(ciphertext)
    print(res3[1])
    assert res3[1] == "Cooking MC's like a pound of bacon"

    print('Task 4')
    ciphertexts = get_file('4.txt').split('\n')
    res4 = find_and_decrypt_ciphertexts(ciphertexts)
    print('Key: {0}\nPlaintext: {1}'.format(*res4))
    assert res4[1] == 'Now that the party is jumping\n'

    print('Task 5')
    plaintext5 = ("Burning 'em, if you ain't quick and nimble\n"
                  "I go crazy when I hear a cymbal""")
    key = "ICE"
    correct_answer = ("0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343"
                      "c2a26226324272765272a282b2f20430a652e2c652a3124333a653e"
                      "2b2027630c692b20283165286326302e27282f")
    res5 = bytes_to_hex(repeating_key_xor(text_to_bytes(plaintext5),
                                          text_to_bytes(key)))
    print(res5)
    assert res5 == correct_answer

    print('Task 6')
    string1 = b'this is a test'
    string2 = b'wokka wokka!!!'
    print('Hamming Distance Check:', hamming_distance(string1, string2))
    ciphertext6 = get_file('6.txt')
    ciphertext6 = base64_to_bytes(ciphertext6)
    res6 = decode_repeating_byte_xor(ciphertext6)
    assert res6[0] == 'Terminator X: Bring the noise'
    print('Key:', res6[0])
    print('Plaintext:')
    print(res6[1])

    print('Task 7')
    ciphertext7 = get_file('7.txt')
    ciphertext7 = base64_to_bytes(ciphertext7)
    password = b"YELLOW SUBMARINE"
    res7 = aes_ecb_decode(ciphertext7, password).decode('ascii')
    assert res7.startswith("I'm back and I'm ringin' the bell ")
    print(res7)

    print('Task 8')
    ciphertexts8 = get_file('8.txt').split('\n')
    ciphertexts8 = [bytes.fromhex(x) for x in ciphertexts8 if x]
    res8 = detect_aes_ecb_encrypted_texts(ciphertexts8)
    assert len(res8[1]) == 1
    print('Most likely string:', bytes_to_hex(res8[1][0]))
    print('Max no. of repeats of a 16byte chunk found:', res8[0])