Ejemplo n.º 1
0
class Affine(Cipher):
    """Implements cipher via the affine method"""
    def __init__(self, key, alphabet_size):
        super().__init__("Affine", alphabet_size, key)
        mkey, ckey = key
        self.multiplication = Multiplication(mkey, alphabet_size)
        self.caesar = Caesar(ckey, alphabet_size)

    def encode(self, message):
        caesared = self.caesar.encode(message)
        multiplied = self.multiplication.encode(caesared)
        return multiplied

    def decode(self, encrypted):
        multiplied = self.multiplication.decode(encrypted)
        caesared = self.caesar.decode(multiplied)
        return caesared

    def candidate_keys(self):
        caesar_candidates = self.caesar.candidate_keys()
        multiplication_candidates = self.multiplication.candidate_keys()

        return product(multiplication_candidates, caesar_candidates)

    @staticmethod
    def generate_key(alphabet_size):
        return Multiplication.generate_key(alphabet_size), Caesar.generate_key(
            alphabet_size)
Ejemplo n.º 2
0
def main():
    """Main program for testing ciphers"""
    key = Affine.generate_key(ALPHABET_SIZE)
    cipher = Affine(key, ALPHABET_SIZE)

    key = Multiplication.generate_key(ALPHABET_SIZE)
    cipher2 = Multiplication(key, ALPHABET_SIZE)

    key = Caesar.generate_key(ALPHABET_SIZE)
    cipher3 = Caesar(key, ALPHABET_SIZE)

    key = Unbreakable.generate_key(ALPHABET_SIZE)
    cipher4 = Unbreakable(key, ALPHABET_SIZE)

    # run_normal_cipher(cipher, "Test of affine")
    # run_normal_cipher(cipher2, "Test of multiplication")
    # run_normal_cipher(cipher3, "Test of caesar")
    run_normal_cipher(cipher4, "Test of unbreakable")

    run_rsa("Test of RSA")
Ejemplo n.º 3
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')
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def create_dset():
    if args.task == 'add':
        dset = Addition()
    elif args.task == 'mul':
        dset = Multiplication()
    elif args.task == 'mem':
        dset = NoiselessMemorization(sequence_len=args.sequence_len)
    elif args.task == 'xor':
        dset = XOR()
    elif args.task == 'bball':
        dset = BouncingBall(vectorize=True)
    elif args.task == 'seqmnist':
        dset = SequentialMNIST()
    else:
        raise Exception
    return dset
Ejemplo n.º 6
0
 def __init__(self):
     self.caesar = Caesar()
     self.multiplication = Multiplication()
Ejemplo n.º 7
0
from addition import Addition
from multiplication import Multiplication

while True:
    print("Enter the number 01 :  ")
    num1 = int(input())
    print("Enter the number 02 :  ")
    num2 = int(input())
    print("Addition       -->  1")
    print("Multiplication -->  2")
    print("Exit           -->  3")
    choice = int(input("Choice please :-->  "))

    if (choice == 1):
        result = Addition.add(num1, num2)

    elif (choice == 2):
        result = Multiplication.multiply(num1, num2)

    elif (choice == 3):
        break

    else:
        result = "Enter a valid input"

    print(result)
    while True:
        try:
            letter = input(
                "Would you like to make another calculation (Y,N): ")
            if letter != 'Y' and letter != 'y' and letter != 'N' and letter != 'n':
                raise ValueError
            break
        except ValueError:
            print("Oops!  That was no valid symbol.  Try again...")

    return letter


while True:

    symbol = operant()

    if symbol == '+':
        print("Result: " + str(afronden(Addition(num_input(), num_input()))))
    elif symbol == '-':
        print("Result: " +
              str(afronden(Subtraction(num_input(), num_input()))))
    elif symbol == '*':
        print("Result: " +
              str(afronden(Multiplication(num_input(), num_input()))))
    elif symbol == '/':
        print("Result: " + str(afronden(Division(num_input(), num_input()))))

    antwoord = yes_no()
    if antwoord == 'N' or antwoord == 'n':
        break
Ejemplo n.º 9
0
 def generate_key(alphabet_size):
     return Multiplication.generate_key(alphabet_size), Caesar.generate_key(
         alphabet_size)
Ejemplo n.º 10
0
 def __init__(self, key, alphabet_size):
     super().__init__("Affine", alphabet_size, key)
     mkey, ckey = key
     self.multiplication = Multiplication(mkey, alphabet_size)
     self.caesar = Caesar(ckey, alphabet_size)