Example #1
0
def main():
        if len(sys.argv) < 3:
                key = SAMPLE_KEY
                filename = SAMPLE_FILENAME
        else:
                key = sys.argv[1]
                filename = sys.argv[2]

        f = open(filename, "r")
        ciphertext = ""
        for line in f:
                ciphertext += common.b64decode(line.rstrip())

        print common.aes_cbc_decrypt(ciphertext, key, "\x00" * 16)
        
        
        if False:
                for _ in range(1000):
                        key = common.randbytes(16)
                        pt = common.randbytes(random.randint(1, 64))
                        iv = common.randbytes(16)

                        AFTER = binascii.hexlify(common.aes_cbc_decrypt(common.aes_cbc_encrypt(pt, key, iv), key, iv))

                        PT = binascii.hexlify(common.pkcs7_pad(pt, len(pt) + (16 - (len(pt) % 16))))

                        print PT == AFTER
Example #2
0
def aes_128_cbc_encrypt(data: bytes, key: bytes) -> bytes:
    """Encrypt a stream of bytes with the key using block chaining."""
    # Make sure the data and key are the correct lengths
    assert len(key) == 16
    data = pkcs7_pad(data)

    # Create an output buffer for blocks and add to it one at a time
    # The buffer is initialized with the IV.
    buf = [b'\0' * 16]
    for block in [data[i:i + 16] for i in range(0, len(data), 16)]:
        buf.append(aes_128_ecb_encrypt(xor_bytes(block, buf[-1]), key))

    # Combine encrypted block back together, ignoring the IV
    return b''.join(buf[1:])
Example #3
0
def main():
    # ECB round trip
    plaintext = b"""Now is the time for all good men to come to the aid of their
                    country. Lorem ipsum dolor sit amet."""
    plaintext = pkcs7_pad(plaintext)
    key = b'cryptozoologists'
    ciphertext = aes_128_ecb_encrypt(plaintext, key)
    new_plaintext = aes_128_ecb_decrypt(ciphertext, key)
    assert plaintext == new_plaintext

    # CBC decryption of provided text
    with open('10.txt') as file10:
        ciphertext = base64_to_bytes(file10.read())
    key = b'YELLOW SUBMARINE'
    decipheredtext = aes_128_cbc_decrypt(ciphertext, key)
    assert (decipheredtext.startswith(b"I'm back and I'm ringin' the bell"))

    # CBC round trip
    key = b'cryptozoologists'
    ciphertext = aes_128_cbc_encrypt(plaintext, key)
    new_plaintext = aes_128_cbc_decrypt(ciphertext, key)
    assert plaintext == new_plaintext

    print('Challenge 10 completed successfully.')
Example #4
0
def main():
        if len(sys.argv) != 3:
                usage()

        print common.pkcs7_pad(sys.argv[1], int(sys.argv[2]))