Skip to content

Update caesar_cipher.py #3860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 8 additions & 24 deletions ciphers/caesar_cipher.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from string import ascii_letters
from typing import Dict, Optional


def encrypt(input_string: str, key: int, alphabet=None) -> str:
def encrypt(input_string: str, key: int, alphabet: Optional[str] = None) -> str:
"""
encrypt
=======
Expand Down Expand Up @@ -79,7 +80,7 @@ def encrypt(input_string: str, key: int, alphabet=None) -> str:
return result


def decrypt(input_string: str, key: int, alphabet=None) -> str:
def decrypt(input_string: str, key: int, alphabet: Optional[str] = None) -> str:
"""
decrypt
=======
Expand Down Expand Up @@ -144,7 +145,7 @@ def decrypt(input_string: str, key: int, alphabet=None) -> str:
return encrypt(input_string, key, alphabet)


def brute_force(input_string: str, alphabet=None) -> dict:
def brute_force(input_string: str, alphabet: Optional[str] = None) -> Dict[int, str]:
"""
brute_force
===========
Expand Down Expand Up @@ -193,31 +194,18 @@ def brute_force(input_string: str, alphabet=None) -> dict:
# Set default alphabet to lower and upper case english chars
alpha = alphabet or ascii_letters

# The key during testing (will increase)
key = 1

# The encoded result
result = ""

# To store data on all the combinations
brute_force_data = {}

# Cycle through each combination
while key <= len(alpha):
# Decrypt the message
result = decrypt(input_string, key, alpha)

# Update the data
brute_force_data[key] = result

# Reset result and increase the key
result = ""
key += 1
for key in range(1, len(alpha) + 1):
# Decrypt the message and store the result in the data
brute_force_data[key] = decrypt(input_string, key, alpha)

return brute_force_data


def main():
if __name__ == "__main__":
while True:
print(f'\n{"-" * 10}\n Menu\n{"-" * 10}')
print(*["1.Encrypt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep="\n")
Expand Down Expand Up @@ -248,7 +236,3 @@ def main():
elif choice == "4":
print("Goodbye.")
break


if __name__ == "__main__":
main()