ich versuche aktuell eine Playfair Verschlüsselung zu programmieren.
Mein bisheriger Code sieht wie folgt aus:
from string import ascii_uppercase as asc
Code: Alles auswählen
import sys
from string import ascii_uppercase as asc
#input("Ihr Schlüsselwort: ") #Schlüsselwort Eingabe
#text = input("Ihr Text: ") #Text Eingabe
#############################################################
text = "hier LOmmt der Klartext!"
key = "Hallo"
text2 = text.replace(" ","") #Leerzeichen entfernen
text3 = text2.upper() #Text groß schreiben
for i in text3: #Alle Sonderzeichen filtern
if i in text3 not in asc:
text4 = text3.replace(i,"")
if len(text4) % 2:
text5 = text4 + "X"
else:
text5 = text4
x = len(text5)
h = 2
e = 2
b = 0
Liste = list()
while h < x + 1 :
if h < x + 1:
#print (text43[b:e])
text6 = (text5[b:e])
Liste.append(text6)
h = h + 2
e = e + 2
b = b + 2
print (Liste)
t = ()
key_ABC = []
key1 = lambda x: x.upper().replace("J","I")
#Buchstabenfolge für Matrix generieren
for i in key1(key + asc):
if i not in key_ABC and i in asc:
key_ABC.append(i)
#Matrix erzeugen
Matrix = [key_ABC[i:i+5] for i in range(0, len(key_ABC),5)]
print("\n___________\n",*Matrix[0],"\n",*Matrix[1],"\n",*Matrix[2],"\n",*Matrix[3],#Matrix formatiert asugeben
"\n",*Matrix[4],"\n___________")
#Veschlüsseln wenn beide Buchstaben in einer Zeile liegen
d = 0
e = 1
a = 0
while True:
if text[d] in Matrix[a] and text[e] in Matrix[a]:
b = Matrix[a].index(text5[d])+1
if e < 4:
c = Matrix[a].index(text5[e])+1
else:
c = Matrix[a].index(text5[e-5])
print (text5[d],text5[e],"-->",Matrix[a][b],Matrix[a][c])
d = d + 2
e = e + 2
else:
d = d + 2
e = e + 2
if e > 5:
a = a + 1
d = d - 4
e = e - 4
Für die Verschlüsselung bin ich gerade noch an dem ersten Fall (Beide Buchstaben liegen in einer Zeile der Matrix) dran.
Leider bekomme ich die Verschlüsselten Buchstaben nicht ausgegeben.
Ich finde leider noch ziemlicher Anfänger und kann meinen Fehler nicht finden:(
Die Ausgabe ist dann :
Code: Alles auswählen
if text[d] in Matrix[a] and text[e] in Matrix[a]:
IndexError: list index out of range