Created
November 23, 2019 12:22
-
-
Save CodingForFuture/6f52700f24f20595a583232c073c0aa1 to your computer and use it in GitHub Desktop.
Fence cirber - encrypt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ecrypt_word(word_to_crypt, key): | |
"""Funkcja przyjmuje słowo i zwraca je zaszyfrowanie według klucza""" | |
word_list = [["-" for i in range(key)] for j in range(len(word_to_crypt))] # Lista na szyfr | |
column = [i for i in range(key)] | |
cipher = "" | |
for i in range(key-2, 0, -1): | |
column.append(i) | |
row = 0 | |
while word_to_crypt: | |
for i in column: | |
if word_to_crypt: | |
word_list[row][i] = word_to_crypt[0] | |
word_to_crypt = word_to_crypt[1:] | |
row += 1 | |
for i in range(key): | |
for j in range(len(word_list)): | |
if word_list[j][i] != "-": | |
cipher += word_list[j][i] | |
cipher += "~" | |
return cipher | |
def give_an_answer(): | |
"""Obsłuż użytkownika""" | |
word = input("Jakie słowo chcesz zaszyfrować: ") | |
multiple_encryption = ask_yes_no("Czy chcesz zaszyfrować hasło wielokotnie (tak/nie): ") | |
if multiple_encryption: | |
key_con = input("Podaj klucz do szyfru: ") | |
for key in key_con: | |
new_word = word | |
word = ecrypt_word(new_word, int(key)) | |
word = word[:len(word)-1] | |
key_con = key_con[::-1] | |
print("Twoje słowo po zaszyfrowaniu", len(key_con), "razy:", word + "~\nKonfiguracja klucza:", key_con) | |
else: | |
cipher_key = int(input("Podaj klucz do szyfru: ")) | |
cipher_word = ecrypt_word(word, cipher_key) | |
print("Twoje słowo po zaszyfrowaniu:", cipher_word) | |
give_an_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