|
1 |
| -def encrypt(strng, key): |
2 |
| - encrypted = "" |
3 |
| - for x in strng: |
4 |
| - indx = (ord(x) + key) % 256 |
5 |
| - if indx > 126: |
6 |
| - indx = indx - 95 |
7 |
| - encrypted = encrypted + chr(indx) |
8 |
| - return encrypted |
| 1 | +def encrypt(input_string: str, key: int) -> str: |
| 2 | + result = '' |
| 3 | + for x in input_string: |
| 4 | + if not x.isalpha(): |
| 5 | + result += x |
| 6 | + elif x.isupper(): |
| 7 | + result += chr((ord(x) + key - 65) % 26 + 65) |
| 8 | + elif x.islower(): |
| 9 | + result += chr((ord(x) + key - 97) % 26 + 97) |
| 10 | + return result |
9 | 11 |
|
10 | 12 |
|
11 |
| -def decrypt(strng, key): |
12 |
| - decrypted = "" |
13 |
| - for x in strng: |
14 |
| - indx = (ord(x) - key) % 256 |
15 |
| - if indx < 32: |
16 |
| - indx = indx + 95 |
17 |
| - decrypted = decrypted + chr(indx) |
18 |
| - return decrypted |
| 13 | +def decrypt(input_string: str, key: int) -> str: |
| 14 | + result = '' |
| 15 | + for x in input_string: |
| 16 | + if not x.isalpha(): |
| 17 | + result += x |
| 18 | + elif x.isupper(): |
| 19 | + result += chr((ord(x) - key - 65) % 26 + 65) |
| 20 | + elif x.islower(): |
| 21 | + result += chr((ord(x) - key - 97) % 26 + 97) |
| 22 | + return result |
19 | 23 |
|
20 | 24 |
|
21 |
| -def brute_force(strng): |
| 25 | +def brute_force(input_string: str) -> None: |
22 | 26 | key = 1
|
23 |
| - decrypted = "" |
| 27 | + result = '' |
24 | 28 | while key <= 94:
|
25 |
| - for x in strng: |
| 29 | + for x in input_string: |
26 | 30 | indx = (ord(x) - key) % 256
|
27 | 31 | if indx < 32:
|
28 | 32 | indx = indx + 95
|
29 |
| - decrypted = decrypted + chr(indx) |
30 |
| - print("Key: {}\t| Message: {}".format(key, decrypted)) |
31 |
| - decrypted = "" |
| 33 | + result = result + chr(indx) |
| 34 | + print(f'Key: {key}\t| Message: {result}') |
| 35 | + result = '' |
32 | 36 | key += 1
|
33 | 37 | return None
|
34 | 38 |
|
35 | 39 |
|
36 | 40 | def main():
|
37 | 41 | while True:
|
38 |
| - print("-" * 10 + "\n**Menu**\n" + "-" * 10) |
39 |
| - print("1.Encrpyt") |
40 |
| - print("2.Decrypt") |
41 |
| - print("3.BruteForce") |
42 |
| - print("4.Quit") |
| 42 | + print(f'{"-" * 10}\n Menu\n{"-", * 10}') |
| 43 | + print(*["1.Encrpyt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep='\n') |
43 | 44 | choice = input("What would you like to do?: ")
|
44 | 45 | if choice not in ["1", "2", "3", "4"]:
|
45 | 46 | print("Invalid choice, please enter a valid choice")
|
46 | 47 | elif choice == "1":
|
47 |
| - strng = input("Please enter the string to be encrypted: ") |
48 |
| - key = int(input("Please enter off-set between 1-94: ")) |
| 48 | + input_string = input("Please enter the string to be encrypted: ") |
| 49 | + key = int(input("Please enter off-set between 0-25: ")) |
49 | 50 | if key in range(1, 95):
|
50 |
| - print(encrypt(strng.lower(), key)) |
| 51 | + print(encrypt(input_string.lower(), key)) |
51 | 52 | elif choice == "2":
|
52 |
| - strng = input("Please enter the string to be decrypted: ") |
| 53 | + input_string = input("Please enter the string to be decrypted: ") |
53 | 54 | key = int(input("Please enter off-set between 1-94: "))
|
54 | 55 | if key in range(1, 95):
|
55 |
| - print(decrypt(strng, key)) |
| 56 | + print(decrypt(input_string, key)) |
56 | 57 | elif choice == "3":
|
57 |
| - strng = input("Please enter the string to be decrypted: ") |
58 |
| - brute_force(strng) |
| 58 | + input_string = input("Please enter the string to be decrypted: ") |
| 59 | + brute_force(input_string) |
59 | 60 | main()
|
60 | 61 | elif choice == "4":
|
61 | 62 | print("Goodbye.")
|
|
0 commit comments