Example #1
0
def break_ctr(ciphertext_bytearr, key):
    attack_text = bytearray([97] * len(ciphertext_bytearr))
    original_ciphertext_bytearr = copy.copy(ciphertext_bytearr)
    attack_ciphertext = seek(ciphertext_bytearr, key, 0, attack_text)
    keystream = xor(attack_ciphertext, attack_text)
    plaintext_bytearr = xor(original_ciphertext_bytearr, keystream)
    return plaintext_bytearr
Example #2
0
def attack(ciphertexts, reference_letter_frequency):
    guessed_keystream = guess_keystream(ciphertexts, reference_letter_frequency) 
    plaintexts = []
    for ciphertext in ciphertexts:
        plaintexts.append(xor(ciphertext, guessed_keystream))
    
    return plaintexts
Example #3
0
def attack_ciphertext(ciphertext_bytearr, blocksize, key):
    ciphertext_bytearr = modify_ciphertext(ciphertext_bytearr, blocksize)
    is_successful_decrypt, plaintext_bytearr = decrypt(ciphertext_bytearr, key)
    if is_successful_decrypt:
        raise Exception("No ASCII error occurred - impossible to find key!")
    else:
        key = xor(plaintext_bytearr[:blocksize], plaintext_bytearr[2*blocksize:3*blocksize])
        return key
Example #4
0
def ctr(target, key):
    cipher = AES.new(key, AES.MODE_ECB)
    result = bytearray()
    for i in range(math.ceil(len(target) / len(key))):
        ctr_block = i.to_bytes(16, "little")
        encrypted_ctr_block = cipher.encrypt(ctr_block)

        start = i * len(key)
        stop = min(start + len(key), len(target))
        result = result + xor(target[start:stop], encrypted_ctr_block)

    return result
Example #5
0
def seek(ciphertext_bytearr, key, offset, new_text):
    cipher = AES.new(key, AES.MODE_ECB)
    for i in range(math.ceil(offset + len(new_text) / len(key))):
        ctr_block = i.to_bytes(16, "little")
        encrypted_ctr_block = cipher.encrypt(ctr_block)
        start = i * len(key)
        stop = min(start + len(key), offset + len(new_text))
        if start >= offset:
            ciphertext_bytearr[start:stop] = xor(
                new_text[start - offset:stop - offset], encrypted_ctr_block)

    return ciphertext_bytearr