예제 #1
0
def c5():
    PLAINTEXT = ("Burning 'em, if you ain't quick and nimble\n" +
                 "I go crazy when I hear a cymbal")
    KEY = "ICE"
    EXPECTED = ('0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c' +
                '2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b' +
                '2027630c692b20283165286326302e27282f')
    result = crypto.bytes_to_hex(
        crypto.crypt_xor(crypto.str_to_bytes(PLAINTEXT),
                         crypto.str_to_bytes(KEY)))
    print(result)
    expect(result, EXPECTED)
예제 #2
0
def c5():
    PLAINTEXT = ("Burning 'em, if you ain't quick and nimble\n" +
                 "I go crazy when I hear a cymbal")
    KEY = "ICE"
    EXPECTED = ('0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c' +
                '2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b' +
                '2027630c692b20283165286326302e27282f')
    result = crypto.bytes_to_hex(crypto.crypt_xor(
                                          crypto.str_to_bytes(PLAINTEXT),
                                          crypto.str_to_bytes(KEY)))
    print(result)
    expect(result, EXPECTED)
def c12():
    # Find block size
    length = crypto.detect_cipher_block_size(crypto.encrypt_append_secret_ECB)
    print('Determined block size to be {0}.'.format(length))

    # Check the cipher uses ECB mode
    ecb = crypto.detect_ECB(crypto.encrypt_append_secret_ECB)
    if ecb:
        print('The cipher uses ECB mode')
    else:
        raise ValueError('The cipher does not use ECB mode')

    # Find number of blocks to crack for the secret
    num_blocks = int(len(crypto.encrypt_append_secret_ECB(b'')) / length)
    print ('There are {0} blocks to crack'.format(num_blocks))

    print('Finding the secret...')
    print()
    secret = bytes(0)
    for j in range(num_blocks):
        for i in range(length):
            block = {}
            for b in range(0, 255):
                plain = crypto.str_to_bytes('A' * (length - (i + 1))) + secret + b.to_bytes(1, byteorder='big')
                block[b] = crypto.encrypt_append_secret_ECB(plain)[j*length:(j+1)*length]
            match = crypto.encrypt_append_secret_ECB(b'A' * (length - (i + 1)))[j*length:(j+1)*length]
            byte = [k for k,v in block.items() if v == match]
            if not byte:
                # Done - or failed to find a match (i.e padding)
                break
            secret += byte[0].to_bytes(1, byteorder='big')
    print(secret.decode())
def c10():
    INPUT_KEY = 'YELLOW SUBMARINE'
    with open('inputs/10.txt') as f:
        cipher = f.read()
        cipher = crypto.base64_to_bytes(cipher)
        key = crypto.str_to_bytes(INPUT_KEY)
        plain = crypto.decrypt_AES_CBC(cipher, key)
        print(plain)
def c7():
    KEY = 'YELLOW SUBMARINE'
    with open('inputs/7.txt') as f:
        cipher = f.read()
        cipher = crypto.base64_to_bytes(cipher)
        key = crypto.str_to_bytes(KEY)
        plain = crypto.decrypt_AES_ECB(cipher, key)
        print('Plain: {0}'.format(plain.decode('utf-8')))
예제 #6
0
def encrypt(encoded):
    '''
    Encrypt a k=v encoded string using AES ECB mode.
    Returns the ciphertext.
    '''
    global PROFILE_SECRET_KEY
    if not PROFILE_SECRET_KEY:
        PROFILE_SECRET_KEY = crypto.random_AES_key()

    plain = crypto.str_to_bytes(encoded)
    plain = crypto.plaintext_pad_PKCS7(plain)
    return crypto.encrypt_AES_ECB(plain, PROFILE_SECRET_KEY)
def c6():
    HAMMING_INPUTS = ('this is a test','wokka wokka!!!')
    distance = textutils.hamming_distance(*map(crypto.str_to_bytes, HAMMING_INPUTS))
    expect(distance, 37)

    with open('inputs/6.txt') as f:
        cipher = f.read()
        cipher = crypto.base64_to_bytes(cipher)
        
        keysize = crypto.brute_xor_keysize(cipher)
        print('Guessing the key size is {0}'.format(keysize))

        key = crypto.brute_xor_key(cipher, keysize)
        print('Key: {0}'.format(key))

        plain = crypto.fixed_xor(cipher, crypto.str_to_bytes(key))
        print ('Plain: {0}'.format(plain.decode('utf-8')))
예제 #8
0
def c10():
    # My own test data
    message = 'The crow flies from the chicken coop at dawn.'
    key = b'YeLlOw SuBmArInE'
    blocksize = len(key)
    iv = bytes([randrange(0, 256) for i in range(0, blocksize)])
    cryptbytes = crypto.encrypt_aes_cbc(crypto.str_to_bytes(message), iv, key)
    print('Encrypted: %r' % cryptbytes)
    plainbytes = crypto.decrypt_aes_cbc(cryptbytes, iv, key)
    print('Decrypted: %r' % plainbytes.decode())
    expect(plainbytes.decode(), message)

    # Decrypt the example from Cryptopals
    key = b'YELLOW SUBMARINE'
    blocksize = len(key)
    iv = bytes([0] * blocksize)
    cryptbytes = crypto.base64_to_bytes(open('inputs/10.txt').read())
    decrypted = crypto.decrypt_aes_cbc(cryptbytes, iv, key)
    print(decrypted.decode())
예제 #9
0
def c10():
    # My own test data
    message = 'The crow flies from the chicken coop at dawn.'
    key = b'YeLlOw SuBmArInE'
    blocksize = len(key)
    iv = bytes([randrange(0, 256) for i in range(0, blocksize)])
    cryptbytes = crypto.encrypt_aes_cbc(crypto.str_to_bytes(message), iv, key)
    print('Encrypted: %r' % cryptbytes)
    plainbytes = crypto.decrypt_aes_cbc(cryptbytes, iv, key)
    print('Decrypted: %r' % plainbytes.decode())
    expect(plainbytes.decode(), message)

    # Decrypt the example from Cryptopals
    key = b'YELLOW SUBMARINE'
    blocksize = len(key)
    iv = bytes([0]*blocksize)
    cryptbytes = crypto.base64_to_bytes(open('inputs/10.txt').read())
    decrypted = crypto.decrypt_aes_cbc(cryptbytes, iv, key)
    print(decrypted.decode())
예제 #10
0
파일: util.py 프로젝트: mmueller/cryptopals
def encrypted_profile_for(email):
    return crypto.encrypt_aes_ecb(
        crypto.pkcs7_pad(
            crypto.str_to_bytes(encode_profile(profile_for(email))),
            len(PROFILE_KEY)), PROFILE_KEY)
예제 #11
0
파일: util.py 프로젝트: mmueller/cryptopals
def encrypted_profile_for(email):
    return crypto.encrypt_aes_ecb(
             crypto.pkcs7_pad(
               crypto.str_to_bytes(encode_profile(profile_for(email))),
               len(PROFILE_KEY)),
             PROFILE_KEY)
def c9():
    INPUT_BLOCK = 'YELLOW SUBMARINE'
    OUTPUT_BLOCK = b'YELLOW SUBMARINE\x04\x04\x04\x04'
    result = crypto.pad_PKCS7(crypto.str_to_bytes(INPUT_BLOCK), block_length=20)
    print(result)
    expect(result, OUTPUT_BLOCK)
예제 #13
0
def c9():
    INPUT = 'YELLOW SUBMARINE'
    EXPECT = b'YELLOW SUBMARINE\x04\x04\x04\x04'
    result = crypto.pkcs7_pad(crypto.str_to_bytes(INPUT), 20)
    print(result)
    expect(result, EXPECT)
예제 #14
0
def c9():
    INPUT = 'YELLOW SUBMARINE'
    EXPECT = b'YELLOW SUBMARINE\x04\x04\x04\x04'
    result = crypto.pkcs7_pad(crypto.str_to_bytes(INPUT), 20)
    print(result)
    expect(result, EXPECT)