Beispiel #1
0
    def decode_text(self, cipher, encoded_text):
        """method to decode the text without knowing the key"""
        if isinstance(cipher, Ceasar):
            keys = [x for x in range(0, 95)]
            return self.find_the_decoded(cipher, encoded_text, keys)

        elif isinstance(cipher, Multiplicative):
            keys = []
            for i in range(95):
                if modular_inverse(i, 95):
                    keys.append(i)
            return self.find_the_decoded(cipher, encoded_text, keys)

        elif isinstance(cipher, Affine):
            keys = []
            for i in range(95):
                for j in range(95):
                    if modular_inverse(j, 95):
                        this_key = (j, i)
                        keys.append(this_key)
            return self.find_the_decoded(cipher, encoded_text, keys)

        elif isinstance(cipher, Unbreakable):
            keys = self.libary
            return self.find_the_decoded(cipher, encoded_text, keys)
Beispiel #2
0
 def generate_keys(self):
     sender_key = randint(0, 95)
     receiver_key = crypto_utils.modular_inverse(sender_key, 95)
     while (sender_key * receiver_key) % 95 != 1 or sender_key == receiver_key:
         sender_key = randint(0, 95)
         receiver_key = crypto_utils.modular_inverse(sender_key, 95)
     return [sender_key, receiver_key]
Beispiel #3
0
    def generate_keys(self):
        """ Generates encryption and decryption key """
        # Number of bits needed to represent ASCII
        num_of_bits = 8

        # Make random primes until they are not equal
        prime1 = generate_random_prime(num_of_bits)
        prime2 = generate_random_prime(num_of_bits)
        while prime1 == prime2:
            prime1 = generate_random_prime(num_of_bits)
            prime2 = generate_random_prime(num_of_bits)

        prime_product = prime1 * prime2
        phi = (prime1 - 1) * (prime2 - 1)

        rand_num = random.randint(3, phi - 1)

        inverse_rand_num = modular_inverse(rand_num, phi)

        # Have not found an inverse:
        while not inverse_rand_num:
            rand_num = random.randint(3, phi - 1)
            inverse_rand_num = modular_inverse(rand_num, phi)

        public_key = (prime_product, rand_num)
        secret_key = (prime_product, inverse_rand_num)

        print('Valid key pair found!')
        print('Public key:', public_key)
        print('Secret key:', secret_key)

        return secret_key, public_key
Beispiel #4
0
 def generate_keys(self):
     """metode som genererer krypteringsnøkler"""
     senderkey = randint(0, 95)
     recieverkey = crypto_utils.modular_inverse(senderkey, 95)
     while not recieverkey:
         senderkey = randint(0, 95)
         recieverkey = crypto_utils.modular_inverse(senderkey, 95)
     return [senderkey, recieverkey]
Beispiel #5
0
 def generate_keys(self):
     '''Generating keys for Multiplication'''
     key1 = random.randint(1, self.alphabet_size)
     key2 = crypto_utils.modular_inverse(key1, self.alphabet_size)
     while not key2:
         key1 = random.randint(1, self.alphabet_size - 1)
         key2 = crypto_utils.modular_inverse(key1, self.alphabet_size)
     return key1, key2
    def generate_key(alphabet_size):
        potential_key = randint(0, alphabet_size)
        mod_inverse = modular_inverse(potential_key, alphabet_size)

        while (mod_inverse * potential_key) % alphabet_size != 1:
            potential_key = randint(0, alphabet_size)
            mod_inverse = modular_inverse(potential_key, alphabet_size)

        return potential_key
Beispiel #7
0
 def generate_keys(self):
     sender_add_key = randint(0, 95)
     receiver_add_key = 95 - sender_add_key
     sender_mult_key = randint(0, 95)
     receiver_mult_key = crypto_utils.modular_inverse(sender_mult_key, 95)
     while not receiver_mult_key:
         sender_mult_key = randint(0, 95)
         receiver_mult_key = crypto_utils.modular_inverse(sender_mult_key, 95)
     return [(sender_mult_key, sender_add_key), (receiver_mult_key, receiver_add_key)]
Beispiel #8
0
 def generate_keys(self):
     """metode for å generere krypteringsnøkler for klassen Affine"""
     addkey = randint(0, 95)
     sendermultkey = randint(0, 95)
     recievermultkey = crypto_utils.modular_inverse(sendermultkey, 95)
     while not recievermultkey:
         sendermultkey = randint(0, 95)
         recievermultkey = crypto_utils.modular_inverse(sendermultkey, 95)
     keys = [addkey, sendermultkey, recievermultkey]
     return keys
Beispiel #9
0
 def generate_keys(self):
     """Genererer et sett med public og private nøkkler"""
     rsa_p = crypto_utils.generate_random_prime(self.rsa_b)
     rsa_q = crypto_utils.generate_random_prime(self.rsa_b)
     while rsa_p == rsa_q:
         rsa_q = crypto_utils.generate_random_prime(self.rsa_b)
     rsa_n = rsa_p * rsa_q
     rsa_phi = (rsa_p - 1) * (rsa_q - 1)
     rsa_e = random.randint(3, rsa_phi - 1)
     rsa_d = crypto_utils.modular_inverse(rsa_e, rsa_phi)
     while not rsa_d:
         rsa_e = random.randint(3, rsa_phi - 1)
         rsa_d = crypto_utils.modular_inverse(rsa_e, rsa_phi)
     print("Public key: (" + str(rsa_n) + ", " + str(rsa_e) + ")\n"
           "Private key: (" + str(rsa_n) + ", " + str(rsa_d) + ")")
Beispiel #10
0
    def generate_key(self):
        key = 0

        while True:
            key = random.randint(2, 94)
            if modular_inverse(key, 95):
                return key
Beispiel #11
0
    def is_hacked(self, message, cipher):
        """hackemetode"""

        if isinstance(cipher, Caesar):
            for i in range(0, 95):
                word = cipher.decode(i, message)
                if self.is_word(word):
                    return True

        elif isinstance(cipher, Multiplicative):
            for i in range(0, 95):
                invers = crypto_utils.modular_inverse(i, 95)
                key = [i, invers]
                word = cipher.decode(key, message)
                if self.is_word(word):
                    return True

        elif isinstance(cipher, Affine):
            for i in range(0, 95):
                for j in range(0, 95):
                    key = [i, j]
                    word = cipher.decode(key, message)
                    if self.is_word(word):
                        return True

        elif isinstance(cipher, Unbreakable):
            for word in self.englishwords:
                hacked = cipher.decode(word, message)
                if self.is_word(hacked):
                    return True
        return False
 def candidate_keys(self):
     keys = []
     for i in range(self.alphabet_size):
         if (modular_inverse(i, self.alphabet_size) *
                 i) % self.alphabet_size == 1:
             keys.append(i)
     return keys
Beispiel #13
0
    def generate_keys(self):
        '''Generating two keys'''
        rsa_p = crypto_utils.generate_random_prime(8)
        rsa_q = crypto_utils.generate_random_prime(8)
        while rsa_p == rsa_q:
            rsa_p = crypto_utils.generate_random_prime(8)
            rsa_q = crypto_utils.generate_random_prime(8)

        rsa_n = rsa_p * rsa_q
        phi = (rsa_p - 1) * (rsa_q - 1)
        rsa_e = random.randint(3, phi - 1)
        rsa_d = crypto_utils.modular_inverse(rsa_e, phi)
        while not rsa_d:
            rsa_e = random.randint(3, phi - 1)
            rsa_d = crypto_utils.modular_inverse(rsa_e, phi)
        return rsa_n, rsa_d, rsa_e
 def generate_keys(self):
     n = random.randint(1, 999)
     while True:
         if not modular_inverse(n, 95):
             n = random.randint(1, 999)
         else:
             return n
Beispiel #15
0
 def possible_keys(self):
     k_list = []
     for i in range(self.alph_size):
         k_possibility = crypto_utils.modular_inverse(i, self.alph_size)
         if k_possibility:
             k_list.append(k_possibility)
     return k_list
Beispiel #16
0
 def get_possible_keys(self):
     '''Returning possible keys for this Cipher'''
     key_list = []
     for i in range(self.alphabet_size):
         possible_key = crypto_utils.modular_inverse(i, self.alphabet_size)
         if possible_key:
             key_list.append(possible_key)
     return key_list
Beispiel #17
0
 def decode(self, message, key):
     super().decode(message, key)
     decoding_key = crypto_utils.modular_inverse(key, 95)
     decrypted_message = ""
     for sign in message:
         decrypted_message += self.alphabet[(self.alphabet.index(sign) *
                                             decoding_key) % 95]
     return decrypted_message
Beispiel #18
0
    def possible_numbers():

        possible_numbers = []
        for i in range(95):
            if modular_inverse(i, 95):
                possible_numbers.append(i)

        return possible_numbers
Beispiel #19
0
 def generate_keys(self):
     """Generate valid keys"""
     while True:
         contestant = randint(0, self._alphabet_size)
         if modular_inverse(contestant, self._alphabet_size):
             break
     print("Key: ", contestant)
     return contestant
Beispiel #20
0
    def generate_rsa_keys(self):
        """public key used by senders"""
        p_prime = cu.generate_random_prime(8)
        q_prime = cu.generate_random_prime(8)
        while p_prime == q_prime:
            q_prime = cu.generate_random_prime(8)

        n_prime = p_prime * q_prime
        _phi = (p_prime - 1) * (q_prime - 1)

        e_prime = cu.random.randint(3, _phi - 1)
        d_prime = cu.modular_inverse(e_prime, _phi)
        while not d_prime:  # It is important that e has a modulo inverse based on _phi
            e_prime = cu.random.randint(3, _phi - 1)
            d_prime = cu.modular_inverse(e_prime, _phi)

        self.key = (n_prime, d_prime)
        self.public_key = (n_prime, e_prime)
Beispiel #21
0
 def decode(self, text: str, key: int) -> str:
     '''
     For each char in <text>, find it's index in our alphabet
     (ord(c) - FIRST), then multiply the modular inverse of the key
     and wrap around using mod of the length of tha alphabet (<ALPHLEN>).
     '''
     dec = [((ord(c) - FIRST) * cu.modular_inverse(key, ALPHLEN)) % ALPHLEN
            for c in text]
     return self.translate_to_text(dec)
Beispiel #22
0
    def verify(self, text, key):
        test_text = self.encode(text, key)
        print(test_text)
        test_text = self.decode(
            test_text, crypto_utils.modular_inverse(key,
                                                    self._alphabet_length))
        print(test_text)

        return text == test_text
    def decode(self, text):
        decoded_text = ""
        for character in text:
            for letter in range(len(self.alphabet)):
                if character == self.alphabet[letter]:
                    index = (letter * crypto_utils.modular_inverse(self.integer,
                                                                   len(self.alphabet))) % len(self.alphabet)
                    decoded_text += self.alphabet[index]

        return decoded_text
 def decode(self, message, key):
     decrypted = ''
     decrypt_key = modular_inverse(key, 95)
     #print('Using multiplicative decryption key: ',decrypt_key)
     for char in message:
         char_to_number = ((ord(char) - 32) * decrypt_key) % 95
         decrypted += Cipher().dictionary[char_to_number]
     print('Receiver is decrypting multiplicative cipher')
     #print(decrypted)
     return decrypted
Beispiel #25
0
 def decode(self, tekst, key):
     caesar_str = ''
     decoded_str = ''
     m2 = cu.modular_inverse(key[0], self.alphabet_size)
     a2 = self.alphabet_size - key[1]
     for chr in tekst:
         caesar_str += self.legal_alphabet[(((self.legal_alphabet.index(chr) + a2) % self.alphabet_size))]
     for char in caesar_str:
         decoded_str += self.legal_alphabet[(((self.legal_alphabet.index(char) * m2) % self.alphabet_size))]
     return decoded_str
Beispiel #26
0
 def decode(self, msg, key1):
     decoded = ''
     m = modular_inverse(key1, 95)
     for char in msg:
         char_num = ((ord(char) - self.__key2) * m - 32) % 95
         decoded += Cipher.dictionary[char_num]
     sleep(1)
     print("\nReceiver is decrypting...")
     sleep(0.5)
     return decoded
Beispiel #27
0
    def verify(self, text, key):
        test_text = self.encode(text, key)
        print(test_text)
        key = (crypto_utils.modular_inverse(key[0], self._alphabet_length),
               self._alphabet_length - key[1])
        print(key)
        test_text = self.decode(test_text, key)
        print(test_text)

        return text == test_text
 def generate_key(self):
     p = crypto_utils.generate_random_prime(5)
     q = crypto_utils.generate_random_prime(5)
     while p == q:
         q = crypto_utils.generate_random_prime(5)
     self.n = p * q
     o = (p-1) * (q-1)
     self.e = random.randint(3, o-1)
     self.d = crypto_utils.modular_inverse(self.e, o)
     return self.n, self.e, self.d
Beispiel #29
0
    def generate_keys(self, sender, receiver):
        prime_1 = self.primes[random.randint(0, len(self.primes))]
        prime_2 = self.primes[random.randint(0, len(self.primes))]
        n_element = prime_1 * prime_2
        phi = (prime_1 - 1) * (prime_2 - 1)
        e_element = random.randint(3, phi - 1)

        d_element = modular_inverse(e_element, phi)

        sender.set_key([n_element, e_element])
        receiver.set_key(([n_element, d_element]))
Beispiel #30
0
    def generate_keys(self, sender, receiver):
        sender_key = 0
        receiver_key = 0

        while receiver_key == 0:
            sender_key = random.randint(1, self._alphabet_length)
            receiver_key = crypto_utils.modular_inverse(
                sender_key, self._alphabet_length)

        sender.set_key(sender_key)
        receiver.set_key(receiver_key)