def main():
    text = open('ecb_text').read()

    ctr = RandomAccessCTR()
    ciphertext = ctr.encrypt(text)

    new_ciphertext = ctr.edit(ciphertext, 0, 'a' * len(ciphertext))

    keystream = util.string_xor(new_ciphertext, 'a' * len(ciphertext))

    print util.string_xor(keystream, ciphertext)
def main():
    text = open('ecb_text').read()

    ctr = RandomAccessCTR()
    ciphertext = ctr.encrypt(text)

    new_ciphertext = ctr.edit(ciphertext, 0, 'a'*len(ciphertext))

    keystream = util.string_xor(new_ciphertext, 'a'*len(ciphertext))

    print util.string_xor(keystream, ciphertext)
Ejemplo n.º 3
0
def cbc_encrypt(text, key, iv):
    ''' CBC encrypt text with initialization vector iv and key '''
    block_length = len(iv)
    text = pkcs_padding(text, block_length)
    blocks = util.blocks(text, block_length)
    blocks[0] = util.string_xor(blocks[0], iv)
    blocks[0] = util.ecb_encrypt(blocks[0], key)

    for i in xrange(1, len(blocks)):
        blocks[i] = util.string_xor(blocks[i], blocks[i - 1])
        blocks[i] = util.ecb_encrypt(blocks[i], key)

    return ''.join(blocks)
Ejemplo n.º 4
0
def cbc_decrypt(text, key, iv):
    ''' CBC decrypt text with initialization vector iv and key '''
    block_length = len(iv)
    blocks = util.blocks(text, block_length)

    decoded_blocks = [0] * len(blocks)

    decoded_blocks[0] = util.ecb_decrypt(blocks[0], key)
    decoded_blocks[0] = util.string_xor(decoded_blocks[0], iv)
    for i in xrange(1, len(blocks)):
        decoded_blocks[i] = util.ecb_decrypt(blocks[i], key)
        decoded_blocks[i] = util.string_xor(decoded_blocks[i], blocks[i - 1])

    return ''.join(decoded_blocks)
Ejemplo n.º 5
0
def cbc_decrypt(text, key, iv):
    ''' CBC decrypt text with initialization vector iv and key '''
    block_length = len(iv)
    blocks = util.blocks(text, block_length)

    decoded_blocks = [0] * len(blocks)

    decoded_blocks[0] = util.ecb_decrypt(blocks[0], key)
    decoded_blocks[0] = util.string_xor(decoded_blocks[0], iv)
    for i in xrange(1, len(blocks)):
        decoded_blocks[i] = util.ecb_decrypt(blocks[i], key)
        decoded_blocks[i] = util.string_xor(decoded_blocks[i], blocks[i-1])

    return ''.join(decoded_blocks)
Ejemplo n.º 6
0
def cbc_encrypt(text, key, iv):
    ''' CBC encrypt text with initialization vector iv and key '''
    block_length = len(iv)
    text = pkcs_padding(text, block_length)
    blocks = util.blocks(text, block_length)

    blocks[0] = util.string_xor(blocks[0], iv)
    blocks[0] = util.ecb_encrypt(blocks[0], key)

    for i in xrange(1, len(blocks)):
        blocks[i] = util.string_xor(blocks[i], blocks[i-1])
        blocks[i] = util.ecb_encrypt(blocks[i], key)

    return ''.join(blocks)
Ejemplo n.º 7
0
def recover_seed(ciphertext, known_text):
    known_length = len(known_text)
    padding = len(ciphertext) - known_length

    rounded_padding = padding % 4
    if rounded_padding != 0:
        rounded_padding = 4 - rounded_padding

    # Start of our known text
    known_start = padding + rounded_padding
    known_end = padding + rounded_padding + 8
    known = ciphertext[known_start:known_end]

    # Convert them to numbers which were generated by rng
    t = util.string_xor(known, known_text[-8:])
    known_a = struct.unpack(">L", t[:4])[0]
    known_b = struct.unpack(">L", t[4:])[0]

    for i in xrange(65536):
        update_progress(i, 65536)
        mt = MersenneTwister(i)
        for ignore in xrange(padding/4):
            mt.next()
        for check in xrange(known_length + 5):
            if mt.next() == known_a:
                if mt.next() == known_b:
                    return i

    return "FAILED"
Ejemplo n.º 8
0
def ctr_decrypt(text, key='YELLOW SUBMARINE', nonce=0):
    ''' CTR decrypt a text using key and nonce '''
    block_size = len(key)
    blocks = util.get_blocks(text, block_size)

    c = Counter()
    decrypted = ''
    for block in blocks:
        keystring = util.ecb_encrypt(c.next(), key)
        decrypted += util.string_xor(keystring, block)

    return decrypted
def try_ngrams(ciphertexts, length, ngram_frequency):
    ''' Match most repeated ngram with most common possible
        ngrams (from the list declared above), going down to least
        possible occurence
    '''
    global trigrams
    global keystream
    most_common = repeated_ngrams(ciphertexts, length)

    # Start with most occured ngram
    for repeated_ngram in most_common:
        # ngram reference frequency
        for ngram in ngram_frequency:
            possible_keystream = util.string_xor(repeated_ngram['content'], ngram)
            start = repeated_ngram['start']
            s = ''
            # Go through every ciphertext, and apply keystream at that position
            for ciphertext in ciphertexts:
                s += util.string_xor(possible_keystream, ciphertext[start:start+length]) + '-'
            if len(s.strip(allowed_letters)) == 0:
                if keystream.available(start, possible_keystream):
                    keystream.assign(start, possible_keystream)
Ejemplo n.º 10
0
def main():
    aes = AES()
    text = 'a' * 16 * 3

    encrypted = aes.encrypt(text)

    blocks_encrypted = util.get_blocks(encrypted, 16)
    blocks_encrypted[0] = blocks_encrypted[2] = blocks_encrypted[1]
    blocks_encrypted[1] = '\0' * 16

    modified_encrypted = ''.join(blocks_encrypted)

    try:
        decrypted = aes.decrypt(modified_encrypted)
    except InvalidString as e:
        decrypted = str(e)

    blocks_decrypted = util.get_blocks(decrypted, 16)
    print 'Recovered key/IV: ', repr(util.string_xor(blocks_decrypted[0], blocks_decrypted[2]))
Ejemplo n.º 11
0
def main():
    aes = AES()
    text = 'a' * 16 * 3

    encrypted = aes.encrypt(text)

    blocks_encrypted = util.get_blocks(encrypted, 16)
    blocks_encrypted[0] = blocks_encrypted[2] = blocks_encrypted[1]
    blocks_encrypted[1] = '\0' * 16

    modified_encrypted = ''.join(blocks_encrypted)

    try:
        decrypted = aes.decrypt(modified_encrypted)
    except InvalidString as e:
        decrypted = str(e)

    blocks_decrypted = util.get_blocks(decrypted, 16)
    print 'Recovered key/IV: ', repr(
        util.string_xor(blocks_decrypted[0], blocks_decrypted[2]))
def print_decoded(ciphertexts, keystream):
    for ciphertext in ciphertexts:
        print repr(util.string_xor(ciphertext, str(keystream)))