|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# created by : C0SM0 |
| 4 | + |
| 5 | +# imports |
| 6 | +import enchant |
| 7 | +import string |
| 8 | + |
| 9 | +# help menu for cipheringing process |
| 10 | +help_menu = """ |
| 11 | +USAGE: |
| 12 | + key ct [FLAGS] [OPTIONS] |
| 13 | +
|
| 14 | +FLAGS: |
| 15 | + -b, --bruteforce Encrypt input text or file |
| 16 | +
|
| 17 | +OPTIONS: |
| 18 | + -i, --inputFile <input file> Input file to encrypt or decrypt |
| 19 | + -o, --output <output file> Output file for encrypted or decrypted text |
| 20 | + -t, --text <text> Input text to encrypt or decrypt |
| 21 | + -lingo, --lingo <dictionary> Set language for words to pull out |
| 22 | + -words, --words Pull out words from decoded transcripts |
| 23 | +
|
| 24 | +NOTES: |
| 25 | + lowercase text can be used to escape the code transcript |
| 26 | + so if we knew the first two letters were "b" and "a", we |
| 27 | + could filter as such: |
| 28 | +
|
| 29 | + key ct -b -t "baAAs!" |
| 30 | +
|
| 31 | +EXAMPLES: |
| 32 | + key ct -b -t "ABCC" |
| 33 | + key ct -b -t "ABCC" -w -l "en_US" |
| 34 | +""" |
| 35 | + |
| 36 | +# -a, --alphabet <sequence> Input custom alphabet to iterate through # add me |
| 37 | + |
| 38 | +# decode function [!] Each Cipher Must Have This <---------- [!] |
| 39 | +def encode(args): |
| 40 | + # Getting text from all passed in args |
| 41 | + # All other args can be grabbed the same way |
| 42 | + # Example key = input.key | range = input.range |
| 43 | + text = args.text |
| 44 | + |
| 45 | + return ['This cipher does not have encoding capabilites', False] |
| 46 | + |
| 47 | +# decode function [!] Each Cipher Must Have This <---------- [!] |
| 48 | +def decode(args): |
| 49 | + # Getting text from all passed in args |
| 50 | + # All other args can be grabbed the same way |
| 51 | + # Example key = input.key | range = input.range |
| 52 | + text = args.text |
| 53 | + |
| 54 | + return ['This cipher does not have decoding capabilites', False] |
| 55 | + |
| 56 | + |
| 57 | +# dictionary detection |
| 58 | +def dictionary(transcripts, language): |
| 59 | + |
| 60 | + # set language for dictionary detection |
| 61 | + language = enchant.Dict(language) |
| 62 | + output = [] |
| 63 | + |
| 64 | + for transcript in transcripts: |
| 65 | + |
| 66 | + # show transcript if it is a word |
| 67 | + if language.check(transcript.lower()): |
| 68 | + output.append(transcript.lower()) |
| 69 | + |
| 70 | + return output |
| 71 | + |
| 72 | +# brute force code transcript |
| 73 | +# def decode_transcripts(transcript_list, alphabet, count=1): |
| 74 | +def decode_transcripts(transcript_list, alphabet=string.ascii_uppercase, count=1): |
| 75 | + output_transcripts = [] |
| 76 | + global TRANSCRIPT_SET |
| 77 | + global TRANSCRIPT |
| 78 | + |
| 79 | + for transcript in transcript_list: |
| 80 | + |
| 81 | + # ignore lowercase and special characters |
| 82 | + remove_lower = str.maketrans('', '', string.ascii_lowercase) |
| 83 | + remove_punctuation = str.maketrans('', '', string.punctuation) |
| 84 | + format_transcript = transcript.translate(remove_lower) |
| 85 | + new_transcript = format_transcript.translate(remove_punctuation) |
| 86 | + |
| 87 | + transcript_set = set(new_transcript) |
| 88 | + |
| 89 | + # get characters that are shared in both transcripts |
| 90 | + for character in (TRANSCRIPT_SET & transcript_set): |
| 91 | + |
| 92 | + for letter in (alphabet): |
| 93 | + # for letter in alphabet: |
| 94 | + # ignore repeated characters |
| 95 | + |
| 96 | + # TODO: fix theses goofy ahh checks |
| 97 | + if (len(transcript_set) == 1) and (letter in transcript_set) and ((new_transcript == transcript)): |
| 98 | + continue |
| 99 | + if (len(transcript_set) > 1) and (letter in transcript_set): |
| 100 | + continue |
| 101 | + |
| 102 | + plaintext = transcript.replace(character, letter) |
| 103 | + output_transcripts.append(plaintext) |
| 104 | + |
| 105 | + output = list(set(output_transcripts)) |
| 106 | + |
| 107 | + if count == len(TRANSCRIPT): |
| 108 | + return output |
| 109 | + |
| 110 | + return decode_transcripts(output, alphabet, (count+1)) |
| 111 | + |
| 112 | +# brute function [!] Optional Per Cipher <----------------- [!] |
| 113 | +def brute(args): |
| 114 | + # Getting text from all passed in args |
| 115 | + # All other args can be grabbed the same way |
| 116 | + # Example key = input.key | range = input.range |
| 117 | + text = args.text |
| 118 | + word = args.words |
| 119 | + lang = args.lingo |
| 120 | + # alphabet = args.alphabet |
| 121 | + |
| 122 | + if text: |
| 123 | + # Run Decode |
| 124 | + output = f'Bruteforcing | {text}' |
| 125 | + |
| 126 | + global TRANSCRIPT_SET |
| 127 | + global TRANSCRIPT |
| 128 | + TRANSCRIPT_SET = set(text) |
| 129 | + TRANSCRIPT = text |
| 130 | + |
| 131 | + # transcripts = decode_transcripts([text], alphabet) |
| 132 | + transcripts = decode_transcripts([text]) |
| 133 | + |
| 134 | + # filter out words |
| 135 | + if word: |
| 136 | + print(word) |
| 137 | + words = dictionary(transcripts, lang) |
| 138 | + return [f'{output}\n{transcripts}\n\nWords:\n{words}',True] |
| 139 | + |
| 140 | + # Output content as string for main.py to print |
| 141 | + # Pass True if Success Message |
| 142 | + return [f'{output}\n\n{transcripts}',True] |
| 143 | + else: |
| 144 | + # Pass False if Fail Message |
| 145 | + # Return Nothing to have no output |
| 146 | + return ['An error occured, please try again', False] |
0 commit comments