|
1 |
| -from __future__ import print_function |
2 |
| -# The Caesar Cipher Algorithm |
| 1 | +import sys |
| 2 | +def encrypt(strng, key): |
| 3 | + encrypted = '' |
| 4 | + for x in strng: |
| 5 | + indx = (ord(x) + key) % 256 |
| 6 | + if indx > 126: |
| 7 | + indx = indx - 95 |
| 8 | + encrypted = encrypted + chr(indx) |
| 9 | + return encrypted |
3 | 10 |
|
4 |
| -def main(): |
5 |
| - message = input("Enter message: ") |
6 |
| - key = int(input("Key [1-26]: ")) |
7 |
| - mode = input("Encrypt or Decrypt [e/d]: ") |
8 | 11 |
|
9 |
| - if mode.lower().startswith('e'): |
10 |
| - mode = "encrypt" |
11 |
| - elif mode.lower().startswith('d'): |
12 |
| - mode = "decrypt" |
| 12 | +def decrypt(strng, key): |
| 13 | + decrypted = '' |
| 14 | + for x in strng: |
| 15 | + indx = (ord(x) - key) % 256 |
| 16 | + if indx < 32: |
| 17 | + indx = indx + 95 |
| 18 | + decrypted = decrypted + chr(indx) |
| 19 | + return decrypted |
13 | 20 |
|
14 |
| - translated = encdec(message, key, mode) |
15 |
| - if mode == "encrypt": |
16 |
| - print(("Encryption:", translated)) |
17 |
| - elif mode == "decrypt": |
18 |
| - print(("Decryption:", translated)) |
19 |
| - |
20 |
| -def encdec(message, key, mode): |
21 |
| - message = message.upper() |
22 |
| - translated = "" |
23 |
| - LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
24 |
| - for symbol in message: |
25 |
| - if symbol in LETTERS: |
26 |
| - num = LETTERS.find(symbol) |
27 |
| - if mode == "encrypt": |
28 |
| - num = num + key |
29 |
| - elif mode == "decrypt": |
30 |
| - num = num - key |
| 21 | +def brute_force(strng): |
| 22 | + key = 1 |
| 23 | + decrypted = '' |
| 24 | + while key <= 94: |
| 25 | + for x in strng: |
| 26 | + indx = (ord(x) - key) % 256 |
| 27 | + if indx < 32: |
| 28 | + indx = indx + 95 |
| 29 | + decrypted = decrypted + chr(indx) |
| 30 | + print("Key: {}\t| Message: {}".format(key, decrypted)) |
| 31 | + decrypted = '' |
| 32 | + key += 1 |
| 33 | + return None |
31 | 34 |
|
32 |
| - if num >= len(LETTERS): |
33 |
| - num -= len(LETTERS) |
34 |
| - elif num < 0: |
35 |
| - num += len(LETTERS) |
36 | 35 |
|
37 |
| - translated += LETTERS[num] |
38 |
| - else: |
39 |
| - translated += symbol |
40 |
| - return translated |
| 36 | +def main(): |
| 37 | + print('-' * 10 + "\n**Menu**\n" + '-' * 10) |
| 38 | + print("1.Encrpyt") |
| 39 | + print("2.Decrypt") |
| 40 | + print("3.BruteForce") |
| 41 | + print("4.Quit") |
| 42 | + while True: |
| 43 | + choice = input("What would you like to do?: ") |
| 44 | + if choice not in ['1', '2', '3', '4']: |
| 45 | + print ("Invalid choice") |
| 46 | + elif choice == '1': |
| 47 | + strng = input("Please enter the string to be ecrypted: ") |
| 48 | + while True: |
| 49 | + key = int(input("Please enter off-set between 1-94: ")) |
| 50 | + if key in range(1, 95): |
| 51 | + print (encrypt(strng, key)) |
| 52 | + main() |
| 53 | + elif choice == '2': |
| 54 | + strng = input("Please enter the string to be decrypted: ") |
| 55 | + while True: |
| 56 | + key = int(input("Please enter off-set between 1-94: ")) |
| 57 | + if key > 0 and key <= 94: |
| 58 | + print(decrypt(strng, key)) |
| 59 | + main() |
| 60 | + elif choice == '3': |
| 61 | + strng = input("Please enter the string to be decrypted: ") |
| 62 | + brute_force(strng) |
| 63 | + main() |
| 64 | + elif choice == '4': |
| 65 | + print ("Goodbye.") |
| 66 | + sys.exit() |
41 | 67 |
|
42 |
| -if __name__ == '__main__': |
43 |
| - import doctest |
44 |
| - doctest.testmod() |
45 |
| - main() |
| 68 | +main() |
0 commit comments