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:caesar [2025-03-09 16:47] – [Aufgabe B2] hofgf_informatik:verschluesselung:caesar [2025-03-18 15:34] (aktuell) hof
Zeile 58: Zeile 58:
 </code> </code>
  
-<nodisp 2>+<nodisp 1>
 ++++Lösung:| ++++Lösung:|
 <code python caesar.py> <code python caesar.py>
-import string 
  
-def caesar(textkey): +def caesar(klartextn): 
-    ciphertext "" +    alphabet 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .,!?' 
-    for letter in text.upper()+    decrypted_text = '' 
-        index string.ascii_uppercase.find(letter+    # Jeden Buchstaben b im Klartext durchgehen. for-Schleife 
-        if index != -1:  # -1 means not found +    for b in klartext: 
-            index index key +    # Mit jedem Buchstaben: 
-            index index % len(string.ascii_uppercase) +    #   - Position im Alphabet finden alphabet.find 
-            letter = string.ascii_uppercase[index] +        alphabet.find(b
-        ciphertext ciphertext letter +      Position um n verschieben (Addition oder Subtraktion) 
-    return ciphertext+        n 
 +    #   - Resultat eingrenzen auf 0...len(alphabet) 
 +        p % len(alphabet
 +        decrypted_text decrypted_text alphabet[p] 
 +    return decrypted_text
  
 key = 17 key = 17
Zeile 81: Zeile 84:
 ++++ ++++
 </nodisp> </nodisp>
 +
 #### Aufgabe 2 #### Aufgabe 2
  
Zeile 115: Zeile 119:
    1. Eine mit der Caesar-Methode verschlüsselte Nachricht kann man problemlos mit Brute-Force entschlüsseln (alle Möglichkeiten ausprobieren). Funktioniert dies bei der monoalphabetische Verschlüsselung auch? Gibt es bessere Möglichkeiten, eine solche zu entziffern?    1. Eine mit der Caesar-Methode verschlüsselte Nachricht kann man problemlos mit Brute-Force entschlüsseln (alle Möglichkeiten ausprobieren). Funktioniert dies bei der monoalphabetische Verschlüsselung auch? Gibt es bessere Möglichkeiten, eine solche zu entziffern?
  
-<nodisp 2>+<nodisp 1>
 ++++Lösung| ++++Lösung|
 Für den ersten Buchstaben A können wir unter 26 Möglichkeiten auswählen, um ihn umzuplatzieren. Für B bleiben noch 25, für C 24 Möglichkeiten, etc. Für den ersten Buchstaben A können wir unter 26 Möglichkeiten auswählen, um ihn umzuplatzieren. Für B bleiben noch 25, für C 24 Möglichkeiten, etc.
Zeile 134: Zeile 138:
 ++++ ++++
  
-<nodisp 2>+<nodisp 1>
 ++++Mehr Tipps| ++++Mehr Tipps|
 <code python> <code python>
Zeile 159: Zeile 163:
 </nodisp> </nodisp>
  
-<nodisp 2>+<nodisp 1>
 ++++Lösung| ++++Lösung|
 <code python> <code python>
Zeile 252: Zeile 256:
 ``` ```
 ++++ ++++
-<nodisp 2>+<nodisp 1>
 ++++Python Lösung| ++++Python Lösung|
 <code python> <code python>
Zeile 265: Zeile 269:
     return text     return text
          
- 
 def count_letters(text): def count_letters(text):
     """Computes relative frequency of lower-case ASCII letters in text."""     """Computes relative frequency of lower-case ASCII letters in text."""
Zeile 296: Zeile 299:
         # Print the letter with width 2         # Print the letter with width 2
         # Print the frequency with width 6 and precision 2, in percent format.         # Print the frequency with width 6 and precision 2, in percent format.
-        print("{0:2}{1:6.2%}".format(letter, percent))+        print(f"{letter:2}{percent:6.2%}")
  
 print_percentages(count_letters(faust)) print_percentages(count_letters(faust))
Zeile 309: Zeile 312:
  
 <code python> <code python>
-import urllib2 +from urllib.request import urlopen 
- +count = 0 
-data = urllib2.urlopen(<Pfad zu File als String>) +data = urlopen(<Pfad zur Datei>) 
-line_first ... # erste Zeile des Texts +text = data.read().decode('utf-8' reads all downloaded bytesconvert to text
-line_last  = ... # letzte Zeile des Texts +
-for line in data+
-    line = line.replace('\n', '').decode('utf-8') # replace(...): entfernt nervige Zeilenumbruechedecode(): Umlaute usw richtig anzeigen +
-    if line_first <= count <= line_last: +
-        print(line) # Achtung: keine gute Idee, wenn File sehr viele Zeilen beinhaltet! Baue z.B. Counter ein, damit nach z.B. 100 Ausgaben abbricht+
 </code> </code>
- 
-Beachte, dass du noch die erste und letzte Zeilennummer im Buch angeben musst: Der Text am Anfang und Ende des Files gehört nicht zum Buch und soll ignoriert werden. 
  
 Kontrolle: am häufigsten vorkommen sollte $E$ (gut $15\%$), gefolgt von $N$ (ca. $10\%$) und $S$. Die letzten Ränge machen typischerweise $Y$, $Q$ und $X$ unter sich aus. Kontrolle: am häufigsten vorkommen sollte $E$ (gut $15\%$), gefolgt von $N$ (ca. $10\%$) und $S$. Die letzten Ränge machen typischerweise $Y$, $Q$ und $X$ unter sich aus.
  
-<nodisp 2>+<nodisp 1>
 ++++Lösung mit Gutenberg| ++++Lösung mit Gutenberg|
 <code python> <code python>
 # Rest as above. # Rest as above.
-# Trick: urlopen() returns a file-like object, which iterates over lines of text +from urllib.request import urlopen 
-# itertools.chain.from_iterable() will create an iterator that takes those lines +  
-# and iterates over each of them (creating an iterator over the characters of the +faust = urlopen('http://www.gutenberg.org/files/21000/21000-0.txt') 
-# entire book). +s= faust.read().decode('utf-8') 
- +print_percentages(count_letters(s))
-# Caution: this solution will not properly decode UTF-8 characters. +
- +
-import urllib2 +
-import itertools +
- +
-faust = urllib2.urlopen('https://www.gutenberg.org/files/21000/21000-0.txt'+
-print_percentages(count_letters(itertools.chain.from_iterable(faust)))+
 </code> </code>
 ++++ ++++
  • gf_informatik/verschluesselung/caesar.1741538878.txt.gz
  • Zuletzt geändert: 2025-03-09 16:47
  • von hof