ich bin immernoch dabei eine playfair Verschlüsselung zu programmieren.
Mein Problem ist, dass ich keine Möglichkeit finde alle 3 möglichen Fälle für die Verschlüssselung nacheinander zu durchlaufen und erst dann die Variablen zu erhöhen. (siehe Unterprogramm)
Bitte nehmt keine Rücksicht auf die Textformatierung, diese läuft noch nicht einwandfrei, was aber nicht mein Hauptproblem ist
Schonmal vielen vielen Dank,
Erichmo
Code: Alles auswählen
from string import ascii_uppercase as asc
#input("Ihr Schlüsselwort: ") #Schlüsselwort Eingabe
#text = input("Ihr Text: ") #Text Eingabe
#############################################################
text = "hacd kommtt der Klaartext!"
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(text3) % 2:
text5 = text3 + "X"
else:
text5 = text3
t = ()
key_ABC = []
key1 = lambda x: x.upper().replace("J","I")
#Doppelbuchstaben durch X ersetzen -- Fehlerhaft (ersetzt beide Buchstaben durch X)
f=0
g=1
while True:
if text5[f] == text5[g]:
text6 = text5[:g] + "X" + text5[g+1:]
text5 = text6
f = 0
g = 1
else:
f = f + 1
g = g + 1
if g == len(text5):
break
#Kontrollliste erstellen und ausgeben
x = len(text5)
h = 2
e = 2
b = 0
Liste = list()
while h < x + 1 :
if h < x + 1:
text7 = (text6[b:e])
Liste.append(text7)
h = h + 2
e = e + 2
b = b + 2
print (Liste)
#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
T_Matrix = []
for i in zip(*Matrix):
T_Matrix.append((list(i)))
final = list()
e = 1
d = 0
def Verschlüsseln():
a = 0
r = 0
t = 0
z = 0
o = 0
end = 0
d = 0
e = 1
final = list()
while True:
#beide Buchstaben in einer Zeile
if text5[d] in Matrix[a] and text5[e] in Matrix[a] and e <= len(text5):
b = Matrix[a].index(text5[d])+1
c = Matrix[a].index(text5[e])+1
if c == 5:
c = 0
if b == 5:
b = 0
if b != c and text5[d] in Matrix[a] and text5[e] in Matrix[a]:
print (text5[d],text5[e],"-->",Matrix[a][b],Matrix[a][c])
final.append(Matrix[a][b])
final.append(Matrix[a][c])
d = d + 2
e = e + 2
a = 0
end = end + 2
if c == b:
a = a + 1
if a > 4:
a= 0
else:
a = a + 1
if a > 4:
a = 0
if text5[d] in T_Matrix[a] and text5[e] in T_Matrix[a] and e <= len(text5):
b = T_Matrix[a].index(text5[d])+1
c = T_Matrix[a].index(text5[e])+1
if c == 5:
c = 0
if b == 5:
b = 0
if b != c and text5[d] in T_Matrix[a] and text5[e] in T_Matrix[a]:
print (text5[d],text5[e],"-->",T_Matrix[a][b],T_Matrix[a][c])
final.append(T_Matrix[a][b])
final.append(T_Matrix[a][c])
d = d + 2
e = e + 2
a = 0
end = end + 2
if c == b:
a = a + 1
if a > 4:
a= 0
else:
a = a + 1
if a > 4:
if text5[d] in Matrix[r][t]:
if text5[e] in Matrix[z][o] and o != t and r != z:
if t < o:
enc1 = o
enc2 = t
print(text5[d],text5[e],"-->",Matrix[r][enc1],Matrix[z][enc2])
final.append(Matrix[r][enc1] + Matrix[z][enc2])
end = end + 2
d = d + 2
e = e + 2
if e > len(text5) - 1:
break
if t > o:
enc1 = o
enc2 = t
print(text5[d],text5[e],"-->",Matrix[r][enc1],Matrix[z][enc2])
final.append(Matrix[r][enc1] + Matrix[z][enc2])
end = end + 2
d = d + 2
e = e + 2
if e > len(text5) - 1:
break
else:
o = o + 1
if o > 4:
o = 0
z = z + 1
if z > 4:
o = 0
z = 0
else:
t = t + 1
if t > 4:
t = 0
r = r + 1
if r > 4:
t = 0
r = 0
if end == len(text5) - 1 or e > len(text5) - 1:
break
Verschlüsseln()