|
1 | 1 | from string import ascii_letters
|
| 2 | +from typing import Dict, Optional |
2 | 3 |
|
3 | 4 |
|
4 |
| -def encrypt(input_string: str, key: int, alphabet=None) -> str: |
| 5 | +def encrypt(input_string: str, key: int, alphabet: Optional[str] = None) -> str: |
5 | 6 | """
|
6 | 7 | encrypt
|
7 | 8 | =======
|
@@ -79,7 +80,7 @@ def encrypt(input_string: str, key: int, alphabet=None) -> str:
|
79 | 80 | return result
|
80 | 81 |
|
81 | 82 |
|
82 |
| -def decrypt(input_string: str, key: int, alphabet=None) -> str: |
| 83 | +def decrypt(input_string: str, key: int, alphabet: Optional[str] = None) -> str: |
83 | 84 | """
|
84 | 85 | decrypt
|
85 | 86 | =======
|
@@ -144,7 +145,7 @@ def decrypt(input_string: str, key: int, alphabet=None) -> str:
|
144 | 145 | return encrypt(input_string, key, alphabet)
|
145 | 146 |
|
146 | 147 |
|
147 |
| -def brute_force(input_string: str, alphabet=None) -> dict: |
| 148 | +def brute_force(input_string: str, alphabet: Optional[str] = None) -> Dict[int, str]: |
148 | 149 | """
|
149 | 150 | brute_force
|
150 | 151 | ===========
|
@@ -193,31 +194,18 @@ def brute_force(input_string: str, alphabet=None) -> dict:
|
193 | 194 | # Set default alphabet to lower and upper case english chars
|
194 | 195 | alpha = alphabet or ascii_letters
|
195 | 196 |
|
196 |
| - # The key during testing (will increase) |
197 |
| - key = 1 |
198 |
| - |
199 |
| - # The encoded result |
200 |
| - result = "" |
201 |
| - |
202 | 197 | # To store data on all the combinations
|
203 | 198 | brute_force_data = {}
|
204 | 199 |
|
205 | 200 | # Cycle through each combination
|
206 |
| - while key <= len(alpha): |
207 |
| - # Decrypt the message |
208 |
| - result = decrypt(input_string, key, alpha) |
209 |
| - |
210 |
| - # Update the data |
211 |
| - brute_force_data[key] = result |
212 |
| - |
213 |
| - # Reset result and increase the key |
214 |
| - result = "" |
215 |
| - key += 1 |
| 201 | + for key in range(1, len(alpha) + 1): |
| 202 | + # Decrypt the message and store the result in the data |
| 203 | + brute_force_data[key] = decrypt(input_string, key, alpha) |
216 | 204 |
|
217 | 205 | return brute_force_data
|
218 | 206 |
|
219 | 207 |
|
220 |
| -def main(): |
| 208 | +if __name__ == "__main__": |
221 | 209 | while True:
|
222 | 210 | print(f'\n{"-" * 10}\n Menu\n{"-" * 10}')
|
223 | 211 | print(*["1.Encrypt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep="\n")
|
@@ -248,7 +236,3 @@ def main():
|
248 | 236 | elif choice == "4":
|
249 | 237 | print("Goodbye.")
|
250 | 238 | break
|
251 |
| - |
252 |
| - |
253 |
| -if __name__ == "__main__": |
254 |
| - main() |
0 commit comments