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)
def _get_key(self) -> Key: key_read = None while not key_read: try: key_file = input("Enter the path to the key file: ") if not os.path.isfile(key_file): raise FileNotFoundError(f"Could not find file {key_file}") key_read = Key.import_key(key_file) except Exception as ex: print(ex, file=sys.stderr) return key_read
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(message: str, key_part: KeyPart) -> str: # Create a temporary key since we know this is only relevant for text anyways key = Key(key_part, None, None) clean_msg = apply_sanitizers(message, *VigenereCipher.data_sanitizers) valid_key = apply_modifiers(key, message, *VigenereCipher.key_modifiers) key_data = valid_key.get_text_key().key ciphertext = "" for i in range(len(clean_msg)): clean_msg_int = ord(clean_msg[i]) - ord('a') key_data_int = ord(key_data[i]) - ord('a') char = (clean_msg_int + key_data_int) % 26 char += ord('A') ciphertext += chr(char) return ciphertext
def decrypt(self, key: dict, image: dict): image_file = "ImageToDecrypt.png" output_image_file = "DecryptedImage.png" key_file = "DownloadedKey.key" image = image[list(image.keys())[0]]["content"] with open(image_file, "wb") as f: f.write(image) key = key[list(key.keys())[0]]["content"] with open(key_file, "wb") as f: f.write(key) decrypted_message, decrypted_image = decrypt(image_file, Key.import_key(key_file), self.cipher) cv2.imwrite(output_image_file, decrypted_image) if self.decrypted_text_widget is None: self.add_decryption_output(decrypted_message, output_image_file) else: self.decrypted_text_widget.value = decrypted_message
def encrypt(text: str, image: Union[str, np.ndarray], text_key: KeyPart, cipher: Cipher) -> Tuple[str, np.ndarray, Key]: """ raises: ImageTooSmallException """ # Text Encryption encrypted_text = cipher.encrypt(text, text_key) # Image Steganography if type(image) is str: image = cv2.imread(image, cv2.IMREAD_ANYCOLOR) if (get_minimum_size(encrypted_text) > image.size): raise ImageTooSmallException() embedded_message = embed_message(encrypted_text, image) rows, cols = embedded_message.shape[:2] row_key, col_key = ImageCipher.generate_key_part((rows, cols)) key = Key(text_key, row_key, col_key) encrypted_image = ImageCipher.encrypt(embedded_message, key) return (encrypted_text, encrypted_image, key)
def decrypt(image: np.ndarray, key: Key, iterations: int = 10) -> np.ndarray: # This is the same as encrypting, just do it backwards row_key, col_key = map(lambda o: getattr(o, "key"), key.get_image_keys()) reversed_row_key = ImageCipher.key_flipper(row_key) reversed_col_key = ImageCipher.key_flipper(col_key) rows, cols = image.shape[:2] new_image = np.copy(image) for _ in range(iterations): r = new_image[:, :, 0] g = new_image[:, :, 1] b = new_image[:, :, 2] odd_row_size, even_row_size = r[1::2].shape[0], r[::2].shape[0] odd_col_size, even_col_size = r[:, 1::2].shape[1], r[:, ::2].shape[1] new_image[:, ::2] = np.bitwise_xor( new_image[:, ::2], vector_to_image(row_key, even_col_size)) new_image[:, 1::2] = np.bitwise_xor( new_image[:, 1::2], vector_to_image(reversed_row_key, odd_col_size)) new_image[1::2, :] = np.bitwise_xor( new_image[1::2, :], np.transpose(vector_to_image(col_key, odd_row_size), (1, 0, 2))) new_image[::2, :] = np.bitwise_xor( new_image[::2, :], np.transpose(vector_to_image(reversed_col_key, even_row_size), (1, 0, 2))) col_r_mod = np.sum(r, axis=0) % 2 col_g_mod = np.sum(g, axis=0) % 2 col_b_mod = np.sum(b, axis=0) % 2 for i in range(cols): r[:, i] = np.roll(r[:, i], (2 * int(col_r_mod[i] == 0) - 1) * col_key[i]) g[:, i] = np.roll(g[:, i], (2 * int(col_g_mod[i] == 0) - 1) * col_key[i]) b[:, i] = np.roll(b[:, i], (2 * int(col_b_mod[i] == 0) - 1) * col_key[i]) row_r_mod = np.sum(r, axis=1) % 2 row_g_mod = np.sum(g, axis=1) % 2 row_b_mod = np.sum(b, axis=1) % 2 for i in range(rows): r[i] = np.roll(r[i], (2 * int(row_r_mod[i] == 1) - 1) * row_key[i]) g[i] = np.roll(g[i], (2 * int(row_g_mod[i] == 1) - 1) * row_key[i]) b[i] = np.roll(b[i], (2 * int(row_b_mod[i] == 1) - 1) * row_key[i]) new_image[:, :, 0] = r new_image[:, :, 1] = g new_image[:, :, 2] = b return new_image
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)