Commit 147e2ee0 authored by Markus Esmann's avatar Markus Esmann

Implementation of Encryption of Images and XOR-Operation

parent ff4644e8
import cv2 as cv
import numpy as np
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
def encrypt_image(in_path, out_path, key, nonce):
Verschlüsselung und Speicherung eines Bildes
in_path (string): Dateipfad des Bildes
out_path (string): Ausgabepfad
key (bytes): Schlüssel zur Verschlüsselung mit AES
nonce (bytes): Initialisierung des Zählers im GCM
bool: Angabe, ob die Speicherung erfolgriech war
if not(isinstance(key, bytes) and isinstance(nonce, bytes)):
raise TypeError("key and nonce must be bytes")
image = cv.imread(in_path, 1)
if image is None:
raise ValueError("image not found")
image_bytes = image.tobytes()
aesgcm = AESGCM(key)
image_cipher = aesgcm.encrypt(nonce, image_bytes, None)
image_cipher = np.frombuffer(image_cipher, dtype=image.dtype)[:len(image_bytes)]
image_cipher = image_cipher.reshape(image.shape)
return cv.imwrite(out_path, image_cipher)
def xor_images(image_left_path, image_right_path, out_path):
Verknüpfung und Speicherung von zwei Bildern mithilfe der XOR-Operation
image_left_path (string): Pfad des ersten Bildes
image_right_path (string): Pfad des zweiten Bildes
out_path (string): Ausgabepfad
string: Angabe, ob die Speicherung erfolgreich war
image_left = cv.imread(image_left_path, 1)
image_right = cv.imread(image_right_path, 1)
if image_left is None or image_right is None:
raise ValueError("image not found")
image_xor = cv.bitwise_xor(image_left, image_right, mask=None)
return cv.imwrite(out_path, image_xor)
import os
import Tools.image_encryption as image_crypt
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
def expand_filename(path, extension):
Erweiterung des Dateinamens bei einem Pfad
path (string): Dateipfad
extension (string): Erweiterung des Dateinamens
bool: modifizierter Dateipfad
directory, file = os.path.split(path)
filename, file_format = os.path.splitext(file)
new_file = "{}_{}{}".format(filename, extension, file_format)
return os.path.join(directory, new_file)
def image_cipher_xor(image_left_path, image_right_path, out_path):
Verschlüsselung zweier Bilder mit anschließender XOR-Operation bei Verwendung derselben Nonce
image_left_path (string): Dateipfad des ersten Bildes
image_right_path (string): Dateipfad des zweiten Bildes
out_path (string): Ausgabepfad
key = AESGCM.generate_key(256)
nonce = os.urandom(16)
cipher_left_path = expand_filename(image_left_path, "Cipher")
cipher_right_path = expand_filename(image_right_path, "Cipher")
image_crypt.encrypt_image(image_left_path, cipher_left_path, key, nonce)
image_crypt.encrypt_image(image_right_path, cipher_right_path, key, nonce)
image_crypt.xor_images(cipher_left_path, cipher_right_path, out_path)
if __name__ == "__main__":
image_cipher_xor(r'Data\GCM.png', r'Data\Kryptografie.png', r'Data\XOR_Result.png')
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment