Ejemplo n.º 1
0
def supply_key():
    randomise()
    while True:
        div_key = random.randint(2, len(ropes.ASCII_CHARACTERS))
        mod_key = random.randint(2, len(ropes.ASCII_CHARACTERS))
        if maths.euclid_gcd(div_key, len(ropes.ASCII_CHARACTERS)) != 1:
            return div_key * len(ropes.ASCII_CHARACTERS) + mod_key
Ejemplo n.º 2
0
def brute_force_affine(ciphertext, key=1):
    # brute-force by looping through every possible key
    for key in range(key, len(ropes.ASCII_CHARACTERS) ** 2):
        div_key = secret_keys(key)[0]
        if maths.euclid_gcd(div_key, len(ropes.ASCII_CHARACTERS)) != 1:
            continue

        decrypt_attempt = decrypt_affine(key, ciphertext)[1]

        if is_English(decrypt_attempt):
            return key, decrypt_attempt
    return None
Ejemplo n.º 3
0
def secure_keys(div_key, mod_key, mode):
    if div_key == 1 and mode == ENCRYPT:
        return False, 'The affine cipher becomes incredibly weak when the div_key is set to 1. Choose a different key.'
    if mod_key == 0 and mode == ENCRYPT:
        return False, 'The affine cipher becomes incredibly weak when the mod_key is set to 0. Choose a different key.'
    if div_key < 0 or mod_key < 0 or mod_key > len(ropes.ASCII_CHARACTERS) - 1:
        return False, 'The div_key must be greater than 0 and the mod_key must be between 0 and %s.' % (
        len(ropes.ASCII_CHARACTERS) - 1)
    if maths.euclid_gcd(div_key, len(ropes.ASCII_CHARACTERS)) != 1:
        return False, 'The div_key (%s) and the character set size (%s) are not relatively prime. Choose a different key.' % (
        div_key, len(ropes.ASCII_CHARACTERS))
    return True, 'The key combination does not comprimise the strength of the affine cipher'