Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.

Link zu der Vergleichsansicht

Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung
Nächste Überarbeitung
Vorherige Überarbeitung
gf_informatik:verschluesselung:symmetrisch [2025-04-01 11:46] – [Vorgehen] hofgf_informatik:verschluesselung:symmetrisch [2026-04-02 20:20] (aktuell) hof
Zeile 1: Zeile 1:
 ## Symmetrische Verschlüsselung ## Symmetrische Verschlüsselung
 +<html><script type="module" src="https://bottom.ch/editor/stable/bottom-editor.js"></script></html>
  
 Wir können nun beliebige Zeichenfolgen [[gf_informatik:verschluesselung:codierung|codieren]] und mit einer XOR-Operation verschlüsseln. Die Verschlüsselung erfolgt dabei blockweise, wobei jeder Block genau die Länge des Schlüssels hat. Die Verschlüsselung folgt diesem Schema: Wir können nun beliebige Zeichenfolgen [[gf_informatik:verschluesselung:codierung|codieren]] und mit einer XOR-Operation verschlüsseln. Die Verschlüsselung erfolgt dabei blockweise, wobei jeder Block genau die Länge des Schlüssels hat. Die Verschlüsselung folgt diesem Schema:
Zeile 51: Zeile 52:
  
 {{ :gf_informatik:verschluesselung:illustrated_ecb_cbc.jpg?nolink&693 |}} {{ :gf_informatik:verschluesselung:illustrated_ecb_cbc.jpg?nolink&693 |}}
 +
 ### Herausforderung: Verkettung ### Herausforderung: Verkettung
  
Zeile 156: Zeile 158:
 </code> </code>
 ++++ ++++
 +
 ### Verkettung ausprobieren ### Verkettung ausprobieren
  
Zeile 162: Zeile 165:
   * Was passiert mit anderen (längeren) Schlüsseln?   * Was passiert mit anderen (längeren) Schlüsseln?
   * Warum ist ein Schlüssel nur aus Grossbuchstaben ungünstig?   * Warum ist ein Schlüssel nur aus Grossbuchstaben ungünstig?
 +
  
 #### Vorgehen #### Vorgehen
Zeile 173: Zeile 177:
   * Dokumentiere die Resultate in OneNote / Word / Latex / Jupyter.   * Dokumentiere die Resultate in OneNote / Word / Latex / Jupyter.
  
-<code python block_coder.py>+<html><bottom-editor layout="split"> 
 +# In normal python: pip install opencv-python numpy 
 +import micropip 
 +await micropip.install(["numpy", "opencv-python"]); 
 import cv2 as cv import cv2 as cv
 import numpy as np import numpy as np
Zeile 196: Zeile 204:
  
 def binary_to_bytes(binstring): def binary_to_bytes(binstring):
 +    """Returns the list of bytes corresponding to the binary-interpreted sequences of 1s and 0s."""
     binstring *= 8  # ensure we are byte-aligned     binstring *= 8  # ensure we are byte-aligned
     result = []     result = []
Zeile 218: Zeile 227:
  
 def encrypt(plain_bytes, key_bytes, chaining=True, block_size=8): def encrypt(plain_bytes, key_bytes, chaining=True, block_size=8):
 +    """Block-encrypt a sequence of plaintext bytes with key.
 +      * chaining=True: CBC mode
 +      * chaining=False: ECB mode
 +      The returned ciphertext starts with the IV block.
 +    """
     # random initialization vector     # random initialization vector
-    iv = random.randbytes(block_size)+    iv = list(random.randbytes(block_size))
  
     # ensure our key material is divisible by block_size     # ensure our key material is divisible by block_size
     key_bytes = key_bytes * block_size     key_bytes = key_bytes * block_size
  
-    first_block text_to_bytes('a'*block_size) +    previous_block iv 
-    cipher_bytes = block_encrypt(xor(first_block, iv), key_bytes[0:block_size]) +    cipher_bytes = iv
-    previous_block = cipher_bytes+
  
     for i in range(0, len(plain_bytes), block_size):     for i in range(0, len(plain_bytes), block_size):
         plain_block = plain_bytes[i:i+block_size]         plain_block = plain_bytes[i:i+block_size]
-        key_index = (i+block_size) % len(key_bytes)+        key_index = i % len(key_bytes)
         key_block = key_bytes[key_index:key_index+block_size]         key_block = key_bytes[key_index:key_index+block_size]
         if chaining:         if chaining:
Zeile 241: Zeile 254:
  
 def decrypt(cipher_bytes, key_bytes, chaining=True, block_size = 8): def decrypt(cipher_bytes, key_bytes, chaining=True, block_size = 8):
 +    """Block-decrypt a sequence of ciphertext bytes with key.
 +      * chaining=True: CBC mode
 +      * chaining=False: ECB mode
 +      The ciphertext is expected to start with the IV block, even in ECB mode.
 +    """
 +
     # ensure our key material is divisible by block_size     # ensure our key material is divisible by block_size
     key_bytes = key_bytes * block_size     key_bytes = key_bytes * block_size
  
-    # the first block is thrown away+    # recover the IV
     previous_block = cipher_bytes[0:block_size]     previous_block = cipher_bytes[0:block_size]
     plain_bytes = []     plain_bytes = []
Zeile 251: Zeile 270:
     for i in range(block_size, len(cipher_bytes), block_size):     for i in range(block_size, len(cipher_bytes), block_size):
         cipher_block = cipher_bytes[i:i+block_size]         cipher_block = cipher_bytes[i:i+block_size]
-        key_index = i % len(key_bytes)+        key_index = (i-block_size) % len(key_bytes)
         key_block = key_bytes[key_index:key_index+block_size]         key_block = key_bytes[key_index:key_index+block_size]
         plain_block = block_decrypt(cipher_block, key_block)         plain_block = block_decrypt(cipher_block, key_block)
Zeile 276: Zeile 295:
 chaining = False  # False: ECB, True: CBC chaining = False  # False: ECB, True: CBC
  
-img cv.imread('penguin.png')+img_url = 'https://sca.ksr.ch/lib/exe/fetch.php?media=gf_informatik:verschluesselung:penguin.png' 
 +# In vanilla python, use urllib.request.urlopen(img_url) 
 +from pyodide.http import pyfetch 
 +response = await pyfetch(img_url) 
 +data = await response.bytes() 
 +arr = np.asarray(bytearray(data), dtype=np.uint8) 
 +img = cv.imdecode(arr, -1) # 'Load it as it is'
 img_bytes = img.tobytes() img_bytes = img.tobytes()
  
Zeile 289: Zeile 314:
 # cv.imshow("image", bytes_to_image(img_decrypted, img.shape)) # cv.imshow("image", bytes_to_image(img_decrypted, img.shape))
 # cv.waitKey() # cv.waitKey()
-</code>+</bottom-editor></html>
  
  
  • gf_informatik/verschluesselung/symmetrisch.1743507982.txt.gz
  • Zuletzt geändert: 2025-04-01 11:46
  • von hof