Commit b701aa49 authored by Markus Esmann's avatar Markus Esmann

text encryption

parent d206c3d0
Data/GCM_Cipher.png

1.84 MB | W: | H:

Data/GCM_Cipher.png

1.84 MB | W: | H:

Data/GCM_Cipher.png
Data/GCM_Cipher.png
Data/GCM_Cipher.png
Data/GCM_Cipher.png
  • 2-up
  • Swipe
  • Onion skin
Data/Kryptografie_Cipher.png

1.84 MB | W: | H:

Data/Kryptografie_Cipher.png

1.84 MB | W: | H:

Data/Kryptografie_Cipher.png
Data/Kryptografie_Cipher.png
Data/Kryptografie_Cipher.png
Data/Kryptografie_Cipher.png
  • 2-up
  • Swipe
  • Onion skin
import os
import secrets
import cv2 as cv
import numpy as np
from abc import ABC, abstractmethod
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
class Server_Base(ABC):
"""
Abstrakte Basisklasse für den Mock eines Sevrers.
"""
@abstractmethod
def get_text_message(self):
pass
@abstractmethod
def get_image(self, image_path):
pass
class GCMServer_Nonce_Reuse(Server_Base):
"""
Mock eines anfäliggen Servers, der mit AES-GCM verschlüsselt
und für jeden Datensatz dieselbe Nonce verwendet. (GCM Nonce Reuse)
"""
def __init__(self, session_key):
self.__nonce = os.urandom(16)
self.__aesgcm = AESGCM(session_key)
def get_text_message(self):
"""
Simulation einer Textnachricht vom Server. Hierfür wird ein zufälliger Klartext erzeugt,
mit AES-GCM verschlüsselt und authentifiziert.
Returns:
bytes, bytes: Chiffrat, Authentisierungstag
"""
plain_text = secrets.token_bytes(32)
encryption = self.__aesgcm.encrypt(self.__nonce, plain_text, None)
cipher_text = encryption[:len(plain_text)]
auth_tag = encryption[len(plain_text):]
return cipher_text, auth_tag
def get_image(self, image_path):
"""
Simulation einer Bildnachricht vom Server. Hierfür wird ein übergebenes Bild eingelesen,
die zugehörigen Pixel verschlüsselt und authentifiziert.
Hinweis: Das Chiffrat wird wieder in die vorherige Dimension zurücktransformiert.
Parameters:
image_path (string): Pfad des Bildes
Returns:
bytes, bytes: Chiffrat, Authentisierungstag
"""
image = cv.imread(image_path, 1)
if image is None:
raise ValueError("image not found")
image_bytes = image.tobytes()
encryption = self.__aesgcm.encrypt(self.__nonce, image.tobytes(), None)
image_cipher = np.frombuffer(encryption[:len(image_bytes)], dtype=image.dtype)
image_cipher = image_cipher.reshape(image.shape)
auth_tag = encryption[len(image_bytes):]
return image_cipher, auth_tag
import cv2 as cv
import Server.server as server
def image_xor(server, image_left_path, image_right_path, out_path):
"""
Verknüpfung von zwei verschlüsselten Bildern mithilfe der XOR-Operation
und anschließender Speicherung.
Parameters:
server (Server_Base) : Server zur Verschlüsselung der Bilder
image_left_path (string): Pfad des ersten Bildes
image_right_path (string): Pfad des zweiten Bildes
out_path (string): Ausgabepfad
Returns:
bool: Angabe, ob die Speicherung des kombinierten Bildes erfolgreich war
"""
image_left_cipher = server.get_image(image_left_path)[0]
image_right_cipher = server.get_image(image_right_path)[0]
image_xor = cv.bitwise_xor(image_left_cipher, image_right_cipher, mask=None)
return cv.imwrite(out_path, image_xor)
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
Parameters:
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
Returns:
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
Parameters:
image_left_path (string): Pfad des ersten Bildes
image_right_path (string): Pfad des zweiten Bildes
out_path (string): Ausgabepfad
Returns:
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
Parameters:
path (string): Dateipfad
extension (string): Erweiterung des Dateinamens
Returns:
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
Parameters:
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)
import Server.server as server
import Tools.decryption as decryption
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
key = os.urandom(32)
server = server.GCMServer_Nonce_Reuse(key)
#1. Visualisierung der Entschlüsselung bei einer GCM Nonce Reuse
decryption.image_xor(server, r'Data\GCM.png', r'Data\Kryptografie.png', r'Data\XOR_Result.png')
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