Skip to content

Instantly share code, notes, and snippets.

@CodingForFuture
Created November 23, 2019 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodingForFuture/6abd35c73b82d73c89bf60d56e32e132 to your computer and use it in GitHub Desktop.
Save CodingForFuture/6abd35c73b82d73c89bf60d56e32e132 to your computer and use it in GitHub Desktop.
Fence cirber - decrypt
def ask_yes_no(question):
"""Zadaje pytanie na które można odpowiedzieć tak lub nie"""
response = input(question)
while response not in ("tak", "nie"):
response = input(question)
if response == "tak":
return True
return False
def decrypt_word(cipher, key):
"""Odszyfrowywuje słowo na podstawie dostarczonego klucza"""
cipher_copy = cipher # Użyjemy kopii do wypełnienia listy *
cipher_len = len(cipher) # Kopia długości szyfru
final_word = "" # Odszyfrowane słowo
cipher_list = [["-" for i in range(cipher_len)] for j in range(key)] # Lista na słowo
# Wypełniamy listę * w miejscach liter
column = 0
row = [i for i in range(key)]
for i in range(key-2, 0, -1):
row.append(i)
while cipher_copy:
for i in row:
if column < cipher_len:
cipher_list[i][column] = "*"
column += 1
cipher_copy = cipher_copy[1:]
# Uzupełnij listę literami
for i in range(key):
for j in range(cipher_len):
if cipher_list[i][j] == "*":
cipher_list[i][j] = cipher[0]
cipher = cipher[1:]
# Uzupełniaj słowo idąc po kolumnach
for i in range(cipher_len):
for j in range(key):
if cipher_list[j][i] != "-":
final_word += cipher_list[j][i]
return final_word
def return_word(cipher, key, down=2, top=10):
"""Funkcja rozpatruje sytuacje kiedy znasz klucz i kiedy go nie znasz"""
if key >= 2:
final_word = decrypt_word(cipher, key)
return final_word
if key == 0:
key_num = 0
list_of_finals = [[] for i in range(down, top)]
for new_key in range(down, top):
list_of_finals[key_num].append(new_key)
list_of_finals[key_num].append(decrypt_word(cipher, new_key))
key_num += 1
return list_of_finals
def give_and_answer():
"""Obsłuż użytkownika"""
word = input("Podaj słowo do odszyfrowania: ")
multiple_decrypt = ask_yes_no("Czy twoje hasło było kilkukrotnie szyfrowane (tak/nie): ")
if multiple_decrypt:
key_con = input("Wprowadź konfigurację klucza: ")
key_con_list = [int(i) for i in key_con]
for i in key_con_list:
new_word = word
word = return_word(new_word, i)
print("Twoje słowo po odszyfrowaniu:", word, "~\nKlucz hasła:", key_con)
else:
cipher_key = int(input("Podaj klucz szyfru (wpisz 0 jeśli go nie znasz): "))
if cipher_key >= 2:
print("Twoje hasło po roszyfrowaniu:", return_word(word, cipher_key) + "~")
if cipher_key == 0:
found = False
while not found:
down_check = int(input("Od jakiego poziomu chcesz sprawdzić: "))
top_check = int(input("Do jakiego poziomu chcesz sprawdzać: "))
final_word_list = return_word(word, cipher_key, down_check, top_check+1)
for i in range(len(final_word_list)):
print("Klucz:", final_word_list[i][0], "->", final_word_list[i][1])
found = ask_yes_no("Znalazłeś hasło (tak/nie): ")
give_and_answer()
input("\n\nAby zakończyć wciśnij Enter.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment