|
| 1 | +# By @ofirisakov |
| 2 | +try: |
| 3 | + from langdetect import detect |
| 4 | +except ImportError: |
| 5 | + print('Please pip install langdetect') |
| 6 | + |
| 7 | +SMALL_ALPHABEIT = 'abcdefghijklmnopqrstuvwxyz' |
| 8 | +CAPITAL_ALPHABEIT = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 9 | +NUMBERS = '0123456789' |
| 10 | +SPECIAL = '~!@#$%^&*()_=-+`/.,\\<>?:"{}|[];\' ' |
| 11 | + |
| 12 | +ANSWERS = { |
| 13 | + 0: SMALL_ALPHABEIT, |
| 14 | + 1: CAPITAL_ALPHABEIT, |
| 15 | + 2: NUMBERS, |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +def decrypt(sentence: str, key: int, ignore: str): |
| 20 | + """ |
| 21 | + Deciphers the given sentence. |
| 22 | + :param sentence: the sentence to decipher. |
| 23 | + :param key: The key to use in the deciphering. |
| 24 | + :param ignore: A string of characters to ignore. |
| 25 | + :return: the deciphered sentence. |
| 26 | + :rtype: str |
| 27 | + """ |
| 28 | + new_sentance = '' |
| 29 | + |
| 30 | + for char in sentence: |
| 31 | + if char.isdigit(): # if its a digit |
| 32 | + if char not in ignore: |
| 33 | + new_sentance += chr((ord(char) - key - ord('0')) % len(NUMBERS) + ord('0')) |
| 34 | + else: |
| 35 | + new_sentance += char |
| 36 | + elif char.isupper(): # if its a capital letter |
| 37 | + if char not in ignore: |
| 38 | + new_sentance += chr((ord(char) - key - ord('A')) % len(CAPITAL_ALPHABEIT) + ord('A')) |
| 39 | + else: |
| 40 | + new_sentance += char |
| 41 | + elif char.islower(): # if its a small letter |
| 42 | + if char not in ignore: |
| 43 | + new_sentance += chr((ord(char) - key - ord('a')) % len(SMALL_ALPHABEIT) + ord('a')) |
| 44 | + else: |
| 45 | + new_sentance += char |
| 46 | + else: # if its a symbol |
| 47 | + new_sentance += char |
| 48 | + |
| 49 | + return new_sentance |
| 50 | + |
| 51 | + |
| 52 | +def main(): |
| 53 | + sentences = {} |
| 54 | + ignores = '' |
| 55 | + |
| 56 | + ciphered_sentance = input('Enter the ciphered sentence: ') |
| 57 | + |
| 58 | + answer = 0 |
| 59 | + while answer is not 4: |
| 60 | + try: |
| 61 | + print( |
| 62 | + f''' |
| 63 | +Enter the numbers you want to ignore in the deciphering process: |
| 64 | +0. Ignore small letters. {SMALL_ALPHABEIT} |
| 65 | +1. Ignore capital letters. {CAPITAL_ALPHABEIT} |
| 66 | +2. Ignore numbers. {NUMBERS} |
| 67 | +3. Custom symbols. |
| 68 | +Currently ignoring: {''.join(set(ignores))} |
| 69 | +
|
| 70 | +Press Return to continue. |
| 71 | +''' |
| 72 | + ) |
| 73 | + answer = input('Enter your choice: ') |
| 74 | + if answer is '': # Check if its the exit sign(Nothing) |
| 75 | + print('Deciphering...') |
| 76 | + break |
| 77 | + ignores += ANSWERS[int(answer)] |
| 78 | + except ValueError: # If its not an integer |
| 79 | + print('Invalid choice.') |
| 80 | + except KeyError as e: # If its not form the ANSWERS dictionary |
| 81 | + if e.args[0] == 3: # Check if its a custom ignore |
| 82 | + custom_ignore = input('Enter the symbols to ignore: ') |
| 83 | + ignores += custom_ignore |
| 84 | + else: |
| 85 | + print('Invalid choice.') |
| 86 | + |
| 87 | + ignore = ''.join(set([SPECIAL, ignores])) |
| 88 | + |
| 89 | + # Get all the possible combinations |
| 90 | + for key in range(1, 26): |
| 91 | + sentences[key] = decrypt(ciphered_sentance, key, ignore) |
| 92 | + |
| 93 | + # Check if it thinks its English or not, print if it thinks |
| 94 | + found = 0 |
| 95 | + for key in sentences.keys(): |
| 96 | + try: |
| 97 | + if detect(sentences[key]) == 'en': # Check to see if it thinks its English |
| 98 | + found += 1 |
| 99 | + print(f'Maybe {key} is a possibility because I got: "{sentences[key]}"') |
| 100 | + except Exception: |
| 101 | + pass |
| 102 | + |
| 103 | + # Tell the user if it didn't find anything |
| 104 | + if found == 0: |
| 105 | + print('I didn\'t find anything...') |
| 106 | + |
| 107 | + print('Do you want to see every possibility? [y/n]') |
| 108 | + answer = input('') |
| 109 | + if answer == 'y': |
| 110 | + for key in sentences.keys(): |
| 111 | + print(f'{key}: {sentences[key]}') |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments