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 [2025-03-31 20:41] – [Verkettung ausprobieren] hof | gf_informatik:verschluesselung:symmetrisch [2025-04-01 11:46] (aktuell) – [Vorgehen] hof | ||
---|---|---|---|
Zeile 27: | Zeile 27: | ||
* Statistische Methoden könnten die Entschlüsselung ermöglichen, | * Statistische Methoden könnten die Entschlüsselung ermöglichen, | ||
* Wiederkehrende Wörter, insbesondere am Anfang der Nachricht, fallen schnell auf. | * Wiederkehrende Wörter, insbesondere am Anfang der Nachricht, fallen schnell auf. | ||
- | * Die Enigma konnte im zweiten Weltkrieg auch deshalb geknackt werden, weil einige Wörter immer wieder auftauchten, | + | * Die [[wpde> |
* Ein Angreifer könnte einen verschlüsselten Block in eine Nachricht einfügen, ohne den genauen Klartext zu kennen. Damit könnte er die Nachricht verfälschen. | * Ein Angreifer könnte einen verschlüsselten Block in eine Nachricht einfügen, ohne den genauen Klartext zu kennen. Damit könnte er die Nachricht verfälschen. | ||
* Fällt dir ein Beispiel ein, wie dies ausgenützt werden könnte? | * Fällt dir ein Beispiel ein, wie dies ausgenützt werden könnte? | ||
Zeile 158: | Zeile 158: | ||
### Verkettung ausprobieren | ### Verkettung ausprobieren | ||
- | Du benötigst ein Bild, z.B. {{: | + | Wende eine einfache Block-Verschlüsselung im ECB bzw. CBC-Modus an, und dokumentieren den Effekt in einem Dokument (OneNote, Word, Latex...). |
+ | * Wie sieht das verschlüsselte Bild aus? | ||
+ | * Was passiert mit anderen (längeren) Schlüsseln? | ||
+ | * Warum ist ein Schlüssel nur aus Grossbuchstaben ungünstig? | ||
- | Zudem den folgenden Code. Damit der läuft, | + | #### Vorgehen |
+ | |||
+ | Du benötigst ein Bild, z.B. {{: | ||
+ | |||
+ | Speichere | ||
+ | |||
+ | * Führe den Code aus - was zeigt das Bild? | ||
+ | * Ändere den Code, um den Effekt von verschiedenen Schlüssellängen und von _Chaining_ auf das Chiffrat auszuprobieren! | ||
+ | * Dokumentiere die Resultate in OneNote / Word / Latex / Jupyter. | ||
<code python block_coder.py> | <code python block_coder.py> | ||
Zeile 166: | Zeile 177: | ||
import numpy as np | import numpy as np | ||
import math | import math | ||
+ | import random | ||
def text_to_bytes(text): | def text_to_bytes(text): | ||
Zeile 196: | Zeile 208: | ||
return result | return result | ||
- | def block_coder(one, two): | + | def block_encrypt(one, two): |
# In reality, this would be a more complex operation, such as a sequence of | # In reality, this would be a more complex operation, such as a sequence of | ||
# [s-boxes](https:// | # [s-boxes](https:// | ||
return xor(one, two) | return xor(one, two) | ||
- | def block_encrypt(plain_bytes, key_bytes, previous_block): | + | def block_decrypt(one, two): |
- | # Chaining Block Cipher: Encrypt a single block but first | + | # In reality, this would be the inverse of block_encrypt. |
- | # xor the previous encrypted block with the plain text. | + | return xor(two, one) |
- | return block_coder(xor(plain_bytes, previous_block), | + | |
- | + | ||
- | def block_decrypt(cipher_bytes, | + | |
- | # Chaining Block Cipher: Encrypt a single block but first | + | |
- | # xor the previous encrypted block with the plain text. | + | |
- | return xor(block_coder(cipher_bytes, | + | |
- | def encrypt(plain_bytes, | + | def encrypt(plain_bytes, |
- | | + | |
- | iv = text_to_bytes(initialization_vector) | + | iv = random.randbytes(block_size) |
# ensure our key material is divisible by block_size | # ensure our key material is divisible by block_size | ||
Zeile 219: | Zeile 225: | ||
first_block = text_to_bytes(' | first_block = text_to_bytes(' | ||
- | cipher_bytes = block_encrypt(first_block, | + | cipher_bytes = block_encrypt(xor(first_block, iv), key_bytes[0: |
previous_block = cipher_bytes | previous_block = cipher_bytes | ||
Zeile 228: | Zeile 234: | ||
if chaining: | if chaining: | ||
plain_block = xor(plain_block, | plain_block = xor(plain_block, | ||
- | cipher_block = block_coder(plain_block, | + | cipher_block = block_encrypt(plain_block, |
cipher_bytes += cipher_block | cipher_bytes += cipher_block | ||
previous_block = cipher_block | previous_block = cipher_block | ||
Zeile 238: | Zeile 244: | ||
key_bytes = key_bytes * block_size | key_bytes = key_bytes * block_size | ||
+ | # the first block is thrown away | ||
previous_block = cipher_bytes[0: | previous_block = cipher_bytes[0: | ||
plain_bytes = [] | plain_bytes = [] | ||
Zeile 246: | Zeile 253: | ||
key_index = i % len(key_bytes) | key_index = i % len(key_bytes) | ||
key_block = key_bytes[key_index: | key_block = key_bytes[key_index: | ||
- | plain_block = block_coder(cipher_block, | + | plain_block = block_decrypt(cipher_block, |
if chaining: | if chaining: | ||
plain_block = xor(plain_block, | plain_block = xor(plain_block, | ||
Zeile 259: | Zeile 266: | ||
return as_np | return as_np | ||
- | # Play with different keys | + | # Play with different keys: |
- | key = " | + | #key = " |
key = " | key = " | ||
key_bytes = binary_to_bytes(key) | key_bytes = binary_to_bytes(key) | ||
+ | #key_bytes = text_to_bytes(" | ||
key_bytes = text_to_bytes(" | key_bytes = text_to_bytes(" | ||
- | # Change between ECB and CBC modes | + | # Change between ECB and CBC modes: |
chaining = False # False: ECB, True: CBC | chaining = False # False: ECB, True: CBC | ||
- | img = cv.imread(' | + | img = cv.imread(' |
img_bytes = img.tobytes() | img_bytes = img.tobytes() | ||
img_encrypted = encrypt(img_bytes, | img_encrypted = encrypt(img_bytes, | ||
- | # We drop the first block as it only used as initialization vector. | + | # We drop the first block as it' |
cv.imshow(" | cv.imshow(" | ||
- | img_decrypted = decrypt(img_encrypted, | ||
cv.waitKey() | cv.waitKey() | ||
# Test if decryption works | # Test if decryption works | ||
+ | # img_decrypted = decrypt(img_encrypted, | ||
# cv.imshow(" | # cv.imshow(" | ||
# cv.waitKey() | # cv.waitKey() |