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 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)