def _get_text_key_str(self) -> KeyPart:
     user_input = input("Desired Key (Press Enter to Generate a Random One): ")
     if user_input:
         key_part = KeyPart(user_input)
     else:
         key_part = self.cipher.generate_key(RANDOM_STR_KEY_SIZE)
     return key_part
Exemplo n.º 2
0
    def modify_key(key: Key, message: str):
        text_key_part = key.get_text_key()
        if (text_key_part.key_type is str):
            text_key_raw = text_key_part.key
            key_len = len(text_key_raw)
            msg_len = len(message)
            len_diff = msg_len - key_len

            # If we have at least as much key as we have message
            if len_diff <= 0:
                # Performing a quick trim to make sure we have exactly
                # as much key as we have message
                new_text_key = text_key_raw[:msg_len]
            # If we don't have enough key
            else:
                new_text_key = text_key_raw + "x" * len_diff
            new_key_part = KeyPart(new_text_key)
            key.set_text_key(new_key_part)
Exemplo n.º 3
0
    def modify_key(key: Key, message: str):
        text_key_part = key.get_text_key()
        # Only do anything if our text key is a string
        if (text_key_part.key_type is str):
            text_key_raw = text_key_part.key
            key_len = len(text_key_raw)
            msg_len = len(message)
            len_diff = msg_len - key_len

            # If we have at least as much key as we have message
            if len_diff <= 0:
                # Performing a quick trim to make sure we have exactly
                # as much key as we have message
                new_text_key = text_key_raw[:msg_len]
            # If we don't have enough key
            else:
                key_repeats = msg_len // key_len
                partial_key_lengths = msg_len % key_len
                new_text_key = text_key_raw * key_repeats + text_key_raw[:
                                                                         partial_key_lengths]
            new_key_part = KeyPart(new_text_key)
            key.set_text_key(new_key_part)
    def encrypt(self, raw_text: str, text_key: str, image: dict):
        input_image_file = "ImageToEncrypt.png"
        key_file = "EncryptionKey.key"
        output_image_file = "EncryptedImage.png"

        image = image[list(image.keys())[0]]["content"]
        with open(input_image_file, "wb") as f:
            f.write(image)

        try:
            encrypted_text, encrypted_image, key = encrypt(raw_text, input_image_file, KeyPart(text_key), self.cipher)
            key.export_key(key_file)
            cv2.imwrite(output_image_file, encrypted_image, [cv2.IMWRITE_PNG_COMPRESSION, 9])

            if self.encrypted_text_widget is None:
                self.add_encryption_output(encrypted_text, output_image_file, key_file)
            else:
                self.encrypted_text_widget.value = encrypted_text
        except ImageTooSmallException:
            self._gui_error("Selected image is not large enough. Please select a larger image and try again")
 def generate_key_part(
         key_size: Tuple[int, int]) -> Tuple[KeyPart, KeyPart]:
     alpha = 10  # 2**alpha = maximum shift
     key1 = np.random.randint(0, 2**alpha, key_size[0], dtype=np.dtype(int))
     key2 = np.random.randint(0, 2**alpha, key_size[1], dtype=np.dtype(int))
     return KeyPart(key1), KeyPart(key2)
Exemplo n.º 6
0
 def modify_key(key: Key, message: str):
     text_key_part = key.get_text_key()
     if (text_key_part.key is str):
         text_key_raw = text_key_part.key
         new_key_part = KeyPart(text_key_raw.lower())
         key.set_text_key(new_key_part)
Exemplo n.º 7
0
 def generate_key_part(key_size: int) -> KeyPart:
     return KeyPart("".join(
         choice(ascii_lowercase) for _ in range(key_size)))