Esempio n. 1
0
def verify_cipher(cipher_name, key_word=''):
    """ Main method to test ciphers """
    # Instantiate cipher
    cipher = None
    if cipher_name == 'caesar':
        cipher = Caesar()
    elif cipher_name == 'multiplication':
        cipher = Multiplication()
    elif cipher_name == 'affine':
        cipher = Affine()
    elif cipher_name == 'unbreakable':
        cipher = Unbreakable()
    elif cipher_name == 'rsa':
        cipher = RSA()
    else:
        raise Exception('Cipher name not recognised')

    print(('\nTesting %s cipher:\n' % cipher_name).upper())

    # Define sender and receiver
    sender = Sender(cipher)
    receiver = Receiver(cipher)

    # Distribute key(s)
    if cipher_name == 'unbreakable':
        encryption_key, decryption_key = cipher.generate_keys(key_word)
    else:
        encryption_key, decryption_key = cipher.generate_keys()
    sender.set_key(encryption_key)
    receiver.set_key(decryption_key)

    # Create message and send it
    message = "Hello world"
    # message = "aaaaaaaaaaa"
    sender.send(message, receiver)

    print("\nSender message:  ", sender.message)
    print("Sender encoded:  ", sender.encoded_message)
    print("Receiver encoded:", receiver.encoded_message)
    print("Receiver decoded:", receiver.message)

    hack = input('\nDo you want to try and hack this message? (y/n): ')
    if hack == 'y':
        hacker = Hacker(cipher)
        if cipher_name in ('caesar', 'multiplication'):
            hacker.hack_caesar_or_multiplication(sender.encoded_message)
        elif cipher_name == 'affine':
            hacker.hack_affine(sender.encoded_message)
        elif cipher_name == 'unbreakable':
            hacker.hack_unbreakable(sender.encoded_message)
        else:
            print('No can do :P')
Esempio n. 2
0
class Affine:
    """ Affine cipher """
    def __init__(self):
        self.caesar = Caesar()
        self.multiplication = Multiplication()

    def generate_keys(self):
        """ Generates encryption and decryption key """
        multiplication_encrypt, multiplication_decrypt = self.multiplication.generate_keys(
        )
        caesar_encrypt, caesar_decrypt = self.caesar.generate_keys()
        encryption_key = (multiplication_encrypt, caesar_encrypt)  # (48, 90)
        decryption_key = (multiplication_decrypt, caesar_decrypt)  # (2, 5)
        return encryption_key, decryption_key

    def encode(self, message, key):
        """ Encode message with given key """
        multiplication_key = key[0]
        caesar_key = key[1]

        # Encode with Multiplication then Caesar
        partially_encoded = self.multiplication.encode(message,
                                                       multiplication_key)
        encoded_message = self.caesar.encode(partially_encoded, caesar_key)

        return encoded_message

    def decode(self, encoded_message, decryption_keys):
        """ Decode the encoded message with the decryption key """
        multiplication_key = decryption_keys[0]
        caesar_key = decryption_keys[1]

        # Decode with Caesar then Multiplication
        partially_decoded = self.caesar.decode(encoded_message, caesar_key)
        decoded_message = self.multiplication.decode(partially_decoded,
                                                     multiplication_key)

        return decoded_message
Esempio n. 3
0
class Hacker(Receiver):
    '''Hacker class'''
    def __init__(self, cipher):
        self.cipher = cipher

    def english_words(self):
        '''List of the txt file'''
        file = open('english_words.txt')
        return file.read().splitlines()

    def brute_force(self, cipher, crypt_key):
        '''Brute force method'''
        words = self.english_words()
        for key in cipher.possible_keys():
            if isinstance(cipher, Affine):
                for key_two in cipher.possible_keys():
                    self.set_key([key, key_two])
                    if self.operate_cipher(cipher, crypt_key) in words:
                        return self.operate_cipher(cipher, crypt_key)
            else:
                self.set_key(key)
                if words.__contains__(self.operate_cipher(cipher, crypt_key)):
                    return self.operate_cipher(cipher, crypt_key)

        return "None of the possible keys could decrypt the encrypted word"


c = Caesar()
h = Hacker(c)
print(h.brute_force(c, c.encode("hello", c.generate_keys())))