Reformatting code
This commit is contained in:
parent
80ebb362a5
commit
f13b667e6c
|
@ -18,8 +18,8 @@ def encrypt_text(cleartext: str, key: int):
|
||||||
|
|
||||||
for char in cleartext:
|
for char in cleartext:
|
||||||
index = au.get_index_of_letter(char)
|
index = au.get_index_of_letter(char)
|
||||||
newIndex = (index * key) % 26
|
new_index = (index * key) % 26
|
||||||
resulting += au.get_letter_at_index(newIndex)
|
resulting += au.get_letter_at_index(new_index)
|
||||||
|
|
||||||
return resulting
|
return resulting
|
||||||
|
|
||||||
|
@ -41,8 +41,8 @@ def decrypt_text(ciphertext: str, key: int):
|
||||||
|
|
||||||
for char in ciphertext:
|
for char in ciphertext:
|
||||||
index = au.get_index_of_letter(char)
|
index = au.get_index_of_letter(char)
|
||||||
newIndex = (index * decrypt_key) % 26
|
new_index = (index * decrypt_key) % 26
|
||||||
resulting += au.get_letter_at_index(newIndex)
|
resulting += au.get_letter_at_index(new_index)
|
||||||
|
|
||||||
return resulting
|
return resulting
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,10 @@ def encrypt_text(cleartext: str, key: [int]):
|
||||||
resulting = ''
|
resulting = ''
|
||||||
|
|
||||||
for char in cleartext:
|
for char in cleartext:
|
||||||
charIndex = au.get_index_of_letter(char)
|
char_index = au.get_index_of_letter(char)
|
||||||
cipherIndex = key.index(charIndex)
|
cipher_index = key.index(char_index)
|
||||||
cipherChar = au.get_letter_at_index(cipherIndex)
|
cipher_char = au.get_letter_at_index(cipher_index)
|
||||||
resulting += cipherChar
|
resulting += cipher_char
|
||||||
|
|
||||||
return resulting
|
return resulting
|
||||||
|
|
||||||
|
@ -31,10 +31,10 @@ def decrypt_text(ciphertext: str, key: [int]):
|
||||||
resulting = ''
|
resulting = ''
|
||||||
|
|
||||||
for char in ciphertext:
|
for char in ciphertext:
|
||||||
charIndex = au.get_index_of_letter(char)
|
char_index = au.get_index_of_letter(char)
|
||||||
clearIndex = key[charIndex]
|
clear_index = key[char_index]
|
||||||
clearChar = au.get_letter_at_index(clearIndex)
|
clear_char = au.get_letter_at_index(clear_index)
|
||||||
resulting += clearChar
|
resulting += clear_char
|
||||||
|
|
||||||
return resulting
|
return resulting
|
||||||
|
|
||||||
|
@ -50,20 +50,22 @@ def generate_key():
|
||||||
|
|
||||||
return indices
|
return indices
|
||||||
|
|
||||||
|
|
||||||
def generate_key_with_keyword(keyword: str):
|
def generate_key_with_keyword(keyword: str):
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
for char in keyword:
|
for char in keyword:
|
||||||
charIndex = au.get_index_of_letter(char)
|
char_index = au.get_index_of_letter(char)
|
||||||
if charIndex not in result:
|
if char_index not in result:
|
||||||
result.append(charIndex)
|
result.append(char_index)
|
||||||
|
|
||||||
remainingIndices = [i for i in range(26-len(result))]
|
remaining_indices = [i for i in range(26 - len(result))]
|
||||||
|
|
||||||
result.extend(remainingIndices)
|
result.extend(remaining_indices)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
key = generate_key_with_keyword('patrick')
|
key = generate_key_with_keyword('patrick')
|
||||||
print(key)
|
print(key)
|
||||||
|
|
|
@ -23,8 +23,8 @@ def encrypt_text(cleartext: str, key1: int, key2: int):
|
||||||
|
|
||||||
for char in cleartext:
|
for char in cleartext:
|
||||||
index = au.get_index_of_letter(char)
|
index = au.get_index_of_letter(char)
|
||||||
newIndex = (index * key1 + key2) % 26
|
new_index = (index * key1 + key2) % 26
|
||||||
resulting += au.get_letter_at_index(newIndex)
|
resulting += au.get_letter_at_index(new_index)
|
||||||
|
|
||||||
return resulting
|
return resulting
|
||||||
|
|
||||||
|
@ -51,8 +51,8 @@ def decrypt_text(ciphertext: str, key1: int, key2: int):
|
||||||
|
|
||||||
for char in ciphertext:
|
for char in ciphertext:
|
||||||
index = au.get_index_of_letter(char)
|
index = au.get_index_of_letter(char)
|
||||||
newIndex = ((index - key2) * decrypt_key1) % 26
|
new_index = ((index - key2) * decrypt_key1) % 26
|
||||||
resulting += au.get_letter_at_index(newIndex)
|
resulting += au.get_letter_at_index(new_index)
|
||||||
|
|
||||||
return resulting
|
return resulting
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user