Ejemplo n.º 1
0
 def generate_secret_key(self):
     p, q = self.__generate_prime_numbers()
     self.__compute_modulo_number(p, q)
     K_matrix, k1_matrix = self.__create_K_matrix()
     k1_inverse = EncryptionHelper.compute_modular_inverse(
         k1_matrix, EncryptionHelper.get_N_squared())
     K_inverse = EncryptionHelper.compute_modular_inverse(
         K_matrix, EncryptionHelper.get_N_squared())
     return (k1_matrix, k1_inverse, K_matrix, K_inverse)
 def __check_result(self, M, M2, verifiable):
     m = M.real % EncryptionHelper.get_N_squared()
     if (verifiable):
         m1 = M2.real % EncryptionHelper.get_N_squared()
         if (m == m1):
             return m
         else:
             return None
     return m
 def encrypt_message(self, secret_key, message):
     converted_message = self.__convert_message(message)
     M_1, M_2 = self.__create_message_sub_matrices(secret_key,
                                                   converted_message)
     R = EncryptionHelper.generate_random_square_matrix(
         EncryptionHelper.get_N_squared())
     M_final = np.matrix(np.bmat([[M_1, R], [np.zeros([2, 2]), M_2]]))
     return np.matrix((secret_key[2].dot(M_final)).dot(secret_key[3]))
Ejemplo n.º 4
0
 def __create_K_matrix(self):
     while True:
         sub_matrices = self.__generate_sub_matrices()
         K_matrix = np.bmat([[sub_matrices[0], sub_matrices[1]],
                             [sub_matrices[2], sub_matrices[3]]])
         if (EncryptionHelper.has_modular_inverse(
                 K_matrix, EncryptionHelper.get_N_squared())):
             return np.matrix(K_matrix,
                              dtype='longlong'), np.matrix(sub_matrices[0],
                                                           dtype='longlong')
Ejemplo n.º 5
0
            decrypted_message = MatrixDecryption().decrypt_message(secret_key, encrypted_matrix, False)

            if (decrypted_message != message_scaled):
                secret_key = key_gen.generate_secret_key() # If the key is regenerated every iteration, the chances of a correct encryption/decryption rises
                count += 1
            elif (decrypted_message == message_scaled):
                print("The message {0} was recovered after {1} iterations out of 1000".format(decrypted_message/factor, count))
                break
            if(count > 1000):
                not_found_message()

        count = 0
        while True:
            encrypted_matrix_1 = MatrixEncryption().encrypt_message(secret_key, message_scaled)
            encrypted_matrix_2 = MatrixEncryption().encrypt_message(secret_key, message1_scaled)
            N_squared = EncryptionHelper.get_N_squared()
            encrypted_matrix_2 = EncryptionHelper().mod_quaternion_matrix(encrypted_matrix_2, N_squared)
            sum_matrix = np.add(encrypted_matrix_1, encrypted_matrix_2)
            decrypted_message = MatrixDecryption().decrypt_message(secret_key, sum_matrix, False)

            if (decrypted_message != (message_scaled + message1_scaled)):
                count += 1
            elif (decrypted_message == (message_scaled + message1_scaled)):
                print("The sum {0} was recovered after {1} iterations out of 1000".format(decrypted_message/factor, count))
                break
            if (count > 1000):
                not_found_message()

        is_not_found = False

    except:
 def __convert_message(self, message):
     return EncryptionHelper.signed_modulo(message,
                                           EncryptionHelper.get_N_squared())
 def __create_second_message_matrix(self, m_1):
     r1_1 = random.randint(1, 1000) % EncryptionHelper.get_N_squared()
     return np.array([[m_1, r1_1], [0, 0]])
 def __create_first_message_matrix(self, m):
     r1, r2 = (random.randint(1, 1000) % EncryptionHelper.get_N_squared()
               for i in range(2))
     return np.array([[m, r1], [0, r2]])
Ejemplo n.º 9
0
 def __generate_sub_matrices(self):
     k1 = EncryptionHelper.create_modular_inverse_matrix(
         EncryptionHelper.get_N_squared())
     k2, k3, k4 = (EncryptionHelper.generate_random_square_matrix(
         EncryptionHelper.get_N_squared()) for i in range(3))
     return k1, k2, k3, k4