Szyfr Vigenere - nie działa program

0

Witam uczę się Pythona od 2 tygodni , postanowiłem zrobić taki koder-dekoder szyfru Vigenere'a https://pl.wikipedia.org/wiki/Szyfr_Vigen%C3%A8re%E2%80%99a
po uruchomieniu pokazuje sie błąd IndexError: list index out of range . a oto program:

tab = []

def calfabet(start):  #tworzenie alfabetu
    for litera in range(start, 91):
        alfabet.append(litera)
    for reszta in range(65, start):
        alfabet.append(reszta)


def encrypt(wordlit, keylit):
    calfabet(wordlit)
    tab.append(alfabet[keylit-65])
    alfabet.clear()

def decrypt(wordlit, keylit):
    calfabet(wordlit)
    for test in range(0, 25):
        if alfabet[test] == keylit:
            tab.append(alfabet[0])
        alfabet.clear()

print("Co chcesz zaszyfrowac lub odszyfrowac?")
x = input().upper()
print("podaj slowo klucz:")
y = input().upper()


word = []
key = []
wordi = 0  # numer litery slowa
keyi = 0  #numer litery slowa klucza


for znak in x:
    word.append(ord(znak))
for znak in y:
    key.append(ord(znak))
print("co chcesz zrobic? \n 1) zaszyfrowac:  \n 2) odszyfrowac: ")
wybor =input()

if wybor =="1":
    while len(tab) < len(x):

        if word[wordi] in range(65, 90):
            encrypt(word[wordi], key[keyi])
            wordi +=1
            keyi  +=1
        else:
            tab.append(word[wordi])
            wordi +=1
else:
    pass


print(tab)```
0

No bo zakładasz, że klucz jest przynajmniej taki długi jak to co chcesz szyfrować a pewnie podajesz dane jak z przykładu.

0

teraz działa , dzięki ale jeszcze jeden problem mam
dodałem :

alfabet = []
tab = []



def calfabet(start):
    for litera in range(start, 91):
        alfabet.append(litera)
    for reszta in range(65, start):
        alfabet.append(reszta)


def encrypt(wordlit, keylit):
    calfabet(wordlit)
    tab.append(alfabet[keylit-65])
    alfabet.clear()

def decrypt(wordlit, keylit):
    calfabet(wordlit)
    for test in range(0, 25):
        if alfabet[test] == keylit:
            tab.append(alfabet[0])
        alfabet.clear()

print("Co chcesz zaszyfrowac lub odszyfrowac?")
x = input().upper()
print("podaj slowo klucz:")
y = input().upper()


word = []
key = []
wordi = 0
keyi = 0





for znak in x:
    word.append(ord(znak))
for znak in y:
    key.append(ord(znak))

while len(key) < len(word):
    key = key*2



print("co chcesz zrobic? \n 1) zaszyfrowac:  \n 2) odszyfrowac: ")
wybor =input()




if wybor =="1":
    while len(tab) < len(x):

        if word[wordi] in range(65, 90):
            encrypt(word[wordi], key[keyi])
            wordi +=1
            keyi  +=1
        else:
            tab.append(word[wordi])
            wordi +=1
else:
    pass

wynik = str
print(tab)
for znak in tab :
    wynik="".join(chr(znak))


print(wynik)

a widze > [67, 86, 89, 32, 71, 89] Y

po pierwsze jest tylko jedna litera a po drugie od tyłu .

0

Bo sobie nadpisujesz wynik w pętli a nie dodajesz do niego.

0

jeszcze raz dzięki teraz już wszytko działa tylko dodam dekodowanie i bedzię gotowe :D

wynik2 = str
wynik = []
print(tab)
for znak in tab :
    wynik.append(chr(znak))


wynik2 ="".join(wynik)
print(wynik2)

0

Zrobiłem dekodowanie ale jest kolejny problem :
jeżeli w wyrazie jest litera "z" to niezależnie od litery klucz wypisuje "z" lecz tylko w deszyfrowaniu co moge zrobic ?
oto program :

alfabet = []
tab = []


def calfabet(start):             # <generowanie alfabetu
    for litera in range(start, 91):
        alfabet.append(litera)
    for reszta in range(65, start):
        alfabet.append(reszta)
    print(alfabet)


def encrypt(wordlit, keylit):   # <---------szyfrowanie
    calfabet(wordlit)
    tab.append(alfabet[keylit-65])
    alfabet.clear()


def decrypt(wordlit, keylit):  # <--------- deszyfrowanie 
    calfabet(keylit)
    z = alfabet.index(wordlit)
    alfabet.clear()
    calfabet(65)
    tab.append(alfabet[z])
    alfabet.clear()


print("Co chcesz zaszyfrowac lub odszyfrowac?")
x = input().upper()
print("podaj slowo klucz:")
y = input().upper()


word = []
key = []
wordi = 0
keyi = 0


for znak in x:
    word.append(ord(znak))
for znak in y:
    key.append(ord(znak))

while len(key) < len(word):
    key = key*2


print("co chcesz zrobic? \n 1) zaszyfrowac:  \n 2) odszyfrowac: ")
wybor = input()


if wybor == "1":
    while len(tab) < len(x):
        if word[wordi] in range(65, 90):
            encrypt(word[wordi], key[keyi])
            wordi += 1
            keyi += 1
        else:
            tab.append(word[wordi])
            wordi += 1
else:
    while len(tab) < len(x):
        if word[wordi] in range(65, 90):
            decrypt(word[wordi], key[keyi])
            wordi += 1
            keyi += 1
        else:
            tab.append(word[wordi])

            wordi += 1
wynik2 = str
wynik = []

for znak in tab:
    wynik.append(chr(znak))

wynik2 = "".join(wynik)
print(wynik2)

1 użytkowników online, w tym zalogowanych: 0, gości: 1