Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
| Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
| gf_informatik:verschluesselung:symmetrisch [2026-03-30 18:08] – [Vorgehen] hof | gf_informatik:verschluesselung:symmetrisch [2026-04-02 20:20] (aktuell) – hof | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| ## Symmetrische Verschlüsselung | ## Symmetrische Verschlüsselung | ||
| + | < | ||
| Wir können nun beliebige Zeichenfolgen [[gf_informatik: | Wir können nun beliebige Zeichenfolgen [[gf_informatik: | ||
| Zeile 51: | Zeile 52: | ||
| {{ : | {{ : | ||
| + | |||
| ### Herausforderung: | ### Herausforderung: | ||
| Zeile 156: | Zeile 158: | ||
| </ | </ | ||
| ++++ | ++++ | ||
| + | |||
| ### 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 172: | Zeile 177: | ||
| * Dokumentiere die Resultate in OneNote / Word / Latex / Jupyter. | * Dokumentiere die Resultate in OneNote / Word / Latex / Jupyter. | ||
| - | <code python | + | <html>< |
| + | # In normal | ||
| + | import micropip | ||
| + | await micropip.install([" | ||
| import cv2 as cv | import cv2 as cv | ||
| import numpy as np | import numpy as np | ||
| Zeile 195: | Zeile 204: | ||
| def binary_to_bytes(binstring): | def binary_to_bytes(binstring): | ||
| + | """ | ||
| binstring *= 8 # ensure we are byte-aligned | binstring *= 8 # ensure we are byte-aligned | ||
| result = [] | result = [] | ||
| Zeile 217: | Zeile 227: | ||
| def encrypt(plain_bytes, | def encrypt(plain_bytes, | ||
| + | """ | ||
| + | * chaining=True: | ||
| + | * chaining=False: | ||
| + | The returned ciphertext starts with the IV block. | ||
| + | """ | ||
| # random initialization vector | # random initialization vector | ||
| iv = list(random.randbytes(block_size)) | iv = list(random.randbytes(block_size)) | ||
| Zeile 239: | Zeile 254: | ||
| def decrypt(cipher_bytes, | def decrypt(cipher_bytes, | ||
| + | """ | ||
| + | * chaining=True: | ||
| + | * chaining=False: | ||
| + | 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 | ||
| Zeile 274: | Zeile 295: | ||
| chaining = False # False: ECB, True: CBC | chaining = False # False: ECB, True: CBC | ||
| - | img = cv.imread('encryption/ | + | img_url |
| + | # 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), | ||
| + | img = cv.imdecode(arr, | ||
| img_bytes = img.tobytes() | img_bytes = img.tobytes() | ||
| Zeile 287: | Zeile 314: | ||
| # cv.imshow(" | # cv.imshow(" | ||
| # cv.waitKey() | # cv.waitKey() | ||
| - | </code> | + | </bottom-editor></ |