def start_single(self, cipher_text, degree): # print(degree) key_alphas = self.pre_analyse(cipher_text, degree) full_plain_text = '' cipher_columns = [] j = 0 while j < degree: i = j nth_cypher_text = '' while i < len(cipher_text): nth_cypher_text += cipher_text[i] i += degree j += 1 # print(nth_cypher_text) cipher_columns.append(nth_cypher_text) pairs = zip(key_alphas, cipher_columns) for each_pair in pairs: decoder = Decode(each_pair[1]) plain_text = decoder.quag_breaker(each_pair[0]) full_plain_text += plain_text + ' | ' self.analyse(full_plain_text, ['A']) print(full_plain_text)
def __init__(self, key_len, cipher_text): self.key_len = key_len self.alphabet = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ] self.cipher_text = cipher_text self.brute = itertools.product(self.alphabet, repeat=key_len) self.decoder = Decode(self.cipher_text) nthMessage = NthMessage(self.cipher_text, key_len) self.ze_pre_analysis = nthMessage.output() self.brute = self.build_key() self.transDecode = None self.wordSearch = WordSearch()
def __init__(self, cipher_text, degree): self.cipher_text = cipher_text self.cipher_columns = [] j = 0 while j < degree: i = j nth_cypher_text = '' while i < len(self.cipher_text): nth_cypher_text += self.cipher_text[i] i += degree j += 1 # print(nth_cypher_text) self.cipher_columns.append(nth_cypher_text) self.decoder = Decode(self.cipher_columns[0]) self.alpha = [x for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'] # self.keys = itertools.product(alpha, repeat=degree) self.degree = degree
def colmber_cipher_worker(self, q): while True: if q.empty(): time.sleep(1) try: cipher_text, each_trans_key = q.get(timeout=1) colmnDecode = Decode(cipher_text) plain_text, each_trans_key = colmnDecode.columnar(each_trans_key) words_len = self.wordSearch.run(plain_text) if words_len > 1: print(each_trans_key) print(words_len) with open('combination_cipher_result.txt', 'a') as results_file: results_file.write('%s | %s | %s\n' % (str(each_trans_key), str(plain_text), str(words_len))) except: # print('[!] run finished') break # print('ending worker') return
def __init__(self, key_len, cipher_text): self.key_len = key_len self.alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] self.cipher_text = cipher_text self.brute = itertools.product(self.alphabet, repeat=key_len) self.decoder = Decode(self.cipher_text) nthMessage = NthMessage(self.cipher_text, key_len) self.ze_pre_analysis = nthMessage.output() self.brute = self.build_key() self.transDecode = None self.wordSearch = WordSearch()
def colmber_cipher_worker(self, q): while True: if q.empty(): time.sleep(1) try: cipher_text, each_trans_key = q.get(timeout=1) colmnDecode = Decode(cipher_text) plain_text, each_trans_key = colmnDecode.columnar( each_trans_key) words_len = self.wordSearch.run(plain_text) if words_len > 1: print(each_trans_key) print(words_len) with open('combination_cipher_result.txt', 'a') as results_file: results_file.write('%s | %s | %s\n' % (str(each_trans_key), str(plain_text), str(words_len))) except: # print('[!] run finished') break # print('ending worker') return
class Running: def __init__(self, cipher_text, degree): self.check_cipher_text = cipher_text[:degree] alpha = [x for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'] self.keys = itertools.product(alpha, repeat=degree) self.decode = Decode(self.check_cipher_text) def start(self): ''' MultiCore ''' q = multiprocessing.Queue(maxsize=50) jobs = [] # Create workers for i in range(0, multiprocessing.cpu_count(), 1): p = multiprocessing.Process(target=self.worker, args=(q, )) p.start() jobs.append(p) # Feed items into the queue for each_item in self.keys: q.put(each_item) def worker(self, q): while True: if q.empty(): time.sleep(1) try: obj = q.get(timeout=1) chiSquareKey = ChiSquare(''.join(obj)) print(''.join(obj)) if chiSquareKey.chi_result < 150: print(chiSquareKey.chi_result) plain_text, key = self.decode.runner(obj) chiSquarePlainText = ChiSquare(plain_text) print(chiSquarePlainText.chi_result) if chiSquarePlainText.chi_result < 150: with open('running_cipher.txt', 'a') as results_file: results_file.write('%s | %s' % (str(key), str(plain_text))) except: break return
class Running: def __init__(self, cipher_text, degree): self.check_cipher_text = cipher_text[:degree] alpha = [x for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'] self.keys = itertools.product(alpha, repeat=degree) self.decode = Decode(self.check_cipher_text) def start(self): ''' MultiCore ''' q = multiprocessing.Queue(maxsize=50) jobs = [] # Create workers for i in range(0, multiprocessing.cpu_count(), 1): p = multiprocessing.Process(target=self.worker, args=(q,)) p.start() jobs.append(p) # Feed items into the queue for each_item in self.keys: q.put(each_item) def worker(self, q): while True: if q.empty(): time.sleep(1) try: obj = q.get(timeout=1) chiSquareKey = ChiSquare(''.join(obj)) print(''.join(obj)) if chiSquareKey.chi_result < 150: print(chiSquareKey.chi_result) plain_text, key = self.decode.runner(obj) chiSquarePlainText = ChiSquare(plain_text) print(chiSquarePlainText.chi_result) if chiSquarePlainText.chi_result < 150: with open('running_cipher.txt', 'a') as results_file: results_file.write('%s | %s' % (str(key), str(plain_text))) except: break return
class Quag: def __init__(self, cipher_text, degree): self.cipher_text = cipher_text self.cipher_columns = [] j = 0 while j < degree: i = j nth_cypher_text = '' while i < len(self.cipher_text): nth_cypher_text += self.cipher_text[i] i += degree j += 1 # print(nth_cypher_text) self.cipher_columns.append(nth_cypher_text) self.decoder = Decode(self.cipher_columns[0]) self.alpha = [x for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'] # self.keys = itertools.product(alpha, repeat=degree) self.degree = degree def pre_analyse(self, cipher_text, degree): result_alpha = [] cipher_columns = [] j = 0 while j < degree: i = j nth_cypher_text = '' while i < len(cipher_text): nth_cypher_text += cipher_text[i] i += degree j += 1 # print(nth_cypher_text) cipher_columns.append(nth_cypher_text) # returns the best guess if it is significantly better objective_plain_text_columns = [] objective_keys = [] # Returns the best guess plain_text_columns = [] for each_column in cipher_columns: quagFrequency = QuagFrequency(each_column) initial_alpha = [0] * 26 # new_decoder = Decode(each_column) # plain_text_columns.append(new_decoder.quag_breaker([getattr(quagFrequency, i)[x].letter for x, i in zip(initial_alpha,self.alpha)])) objective_key = [getattr(quagFrequency, i)[x].letter if (getattr(quagFrequency, i)[x].chi*50 < getattr(quagFrequency, i)[x+1].chi) else '.' for x, i in zip(initial_alpha,self.alpha)] # objective_key = [getattr(quagFrequency, i)[x].letter for x, i in zip(initial_alpha, self.alpha)] objective_keys.append(objective_key) # manually changing the alphabet with letter gleemed from the statistical anaylsis # and anaraming letters close to it objective_keys[0][10] = 't' objective_keys[1][8] = 'h' objective_keys[2][22] = 'e' objective_keys[2][14] = 'a' objective_keys[3][18] = 't' # Statment objective_keys[1][4] = 't' objective_keys[7][18] = 'n' objective_keys[8][13] = 't' # None objective_keys[4][19] = 'n' # good probabioty tells # objective_keys[1][18] = 'o' # objective_keys[4][18] = 'o' # objective_keys[5][21] = 'o' # objective_keys[6][14] = 'e' doubles up # objective_keys[8][22] = 'o' # # gueesing objective_keys[0][18] = 'm' objective_keys[1][24] = 'a' objective_keys[2][16] = 't' objective_keys[3][23] = 'h' objective_keys[4][16] = 'e' objective_keys[5][5] = 'm' objective_keys[6][6] = 'a' objective_keys[7][12] = 't' objective_keys[8][16] = 'i' objective_keys[0][3] = 'c' objective_keys[1][25] = 'i' #a fits objective_keys[3][7] = 'n' objective_keys[3][3] = 'f' objective_keys[4][24] = 'a' objective_keys[5][5] = 'm' objective_keys[6][0] = 'o' objective_keys[7][8] = 'u' objective_keys[8][0] = 's' objective_keys[4][20] = 'f' objective_keys[5][16] = 'r' objective_keys[6][10] = 'e' objective_keys[7][13] = 'e' objective_keys[8][4] = 'm' objective_keys[0][5] = 'a' objective_keys[1][21] = 'n' objective_keys[2][11] = 'd' objective_keys[3][0] = 'y' objective_keys[4][25] = 's' objective_keys[5][15] = 'o' objective_keys[6][25] = 'n' objective_keys[7][15] = 'w' objective_keys[8][2] = 'a' objective_keys[0][23] = 's' # a fits objective_keys[2][3] = 's' objective_keys[3][9] = 'k' # e fits objective_keys[5][11] = 'd' objective_keys[6][21] = 'w' objective_keys[7][6] = 'h' # a fits # t fits # objective_keys[1][23] = 'd' objective_keys[2][15] = 'o' # y fits # objective_keys[8] # objective_keys[3][3] = 'f' # # objective_keys[4][24] = 'i' # objective_keys[5][5] = 'l' # objective_keys[6][0] = 'e' # objective_keys[7][8] = 'h' # objective_keys[5][5] = 'm' # objective_keys[6][6] = 'a' # objective_keys[7][12] = 't' # objective_keys[8][16] = 'h' # objective_keys[0][1] = 's' # objective_keys[1][24] = 't' # objective_keys[2][0] = 'u' # objective_keys[3][22] = 'd' # objective_keys[4][16] = 'e' # objective_keys[5][21] = 'n' # objective_keys[3][3] = 'w' # objective_keys[4][24] = 'i' # objective_keys[5][5] = 'd' # objective_keys[6][0] = 't' # objective_keys[7][8] = 'h' # # objective_keys[8][0] = 'a' # objective_keys[3][19] = 'u' # objective_keys[4][18] = 'n' # objective_keys[5][19] = '' print([str(x) for x in range(0,25,1)]) for objective_key, cipher_text_column in zip(objective_keys, cipher_columns): print(objective_key) quagFrequency = QuagFrequency(each_column) new_decoder = Decode(cipher_text_column) objective_plain_text_columns.append(new_decoder.quag_breaker(objective_key)) plain_text_columns.append(new_decoder.quag_breaker( [getattr(quagFrequency, i)[x].letter for x, i in zip(initial_alpha, self.alpha)])) plain_text = [None] * len(cipher_text) k = -1 for each in objective_plain_text_columns: i = k+1 j = 0 while i < len(cipher_text): plain_text[i] = each[j] i += self.degree j += 1 k += 1 derp = (''.join(plain_text)) print([derp[i:i+9] for i in range(0, len(derp), 9)]) print(derp) exit() @staticmethod def list_duplicates_of(self, seq): location = [] for each_letter in set(seq): start_at = -1 this_letter_location = [] while True: try: loc = seq.index(each_letter, start_at + 1) except ValueError: break else: this_letter_location.append(loc) start_at = loc if len(this_letter_location) > 1: location.extend(this_letter_location) return location @staticmethod def analyse(self, plain_text, key_alpha): chiSquare = ChiSquare(plain_text) chi = chiSquare.output() ic = chiSquare.ic # print('%s | %s | %s' % (str(key_alpha), str(ic), str(chi))) if chi < 200: print('%s | %s | %s' % (str(key_alpha), str(ic), str(chi))) # print(plain_text) with open('quag_result.txt', 'a') as results_file: results_file.write('%s | %s | %s | %s' % (str(key_alpha), str(plain_text), str(ic), str(chi))) def start_single(self, cipher_text, degree): # print(degree) key_alphas = self.pre_analyse(cipher_text, degree) full_plain_text = '' cipher_columns = [] j = 0 while j < degree: i = j nth_cypher_text = '' while i < len(cipher_text): nth_cypher_text += cipher_text[i] i += degree j += 1 # print(nth_cypher_text) cipher_columns.append(nth_cypher_text) pairs = zip(key_alphas, cipher_columns) for each_pair in pairs: decoder = Decode(each_pair[1]) plain_text = decoder.quag_breaker(each_pair[0]) full_plain_text += plain_text + ' | ' self.analyse(full_plain_text, ['A']) print(full_plain_text) # for each_alphabet in itertools.permutations(self.alpha): # # print(each_alphabet) # decoder = Decode(self.cipher_columns[0]) # plain_text = decoder.quag_breaker(each_alphabet) # self.analyse(plain_text, each_alphabet) def start(self): ''' MultiCore ''' q = multiprocessing.Queue(maxsize=50) jobs = [] # Create workers for i in range(0, multiprocessing.cpu_count(), 1): p = multiprocessing.Process(target=self.worker, args=(q,)) p.start() jobs.append(p) # Feed items into the queue for each_alphabet in itertools.permutations(self.alpha): q.put(each_alphabet) def worker(self, q): while True: if q.empty(): time.sleep(1) try: each_alphabet = q.get(timeout=1) print(each_alphabet) plain_text = self.decoder.quag_breaker(self.cipher_columns[0]) self.analyse(plain_text, each_alphabet) except: break return
def pre_analyse(self, cipher_text, degree): result_alpha = [] cipher_columns = [] j = 0 while j < degree: i = j nth_cypher_text = '' while i < len(cipher_text): nth_cypher_text += cipher_text[i] i += degree j += 1 # print(nth_cypher_text) cipher_columns.append(nth_cypher_text) # returns the best guess if it is significantly better objective_plain_text_columns = [] objective_keys = [] # Returns the best guess plain_text_columns = [] for each_column in cipher_columns: quagFrequency = QuagFrequency(each_column) initial_alpha = [0] * 26 # new_decoder = Decode(each_column) # plain_text_columns.append(new_decoder.quag_breaker([getattr(quagFrequency, i)[x].letter for x, i in zip(initial_alpha,self.alpha)])) objective_key = [getattr(quagFrequency, i)[x].letter if (getattr(quagFrequency, i)[x].chi*50 < getattr(quagFrequency, i)[x+1].chi) else '.' for x, i in zip(initial_alpha,self.alpha)] # objective_key = [getattr(quagFrequency, i)[x].letter for x, i in zip(initial_alpha, self.alpha)] objective_keys.append(objective_key) # manually changing the alphabet with letter gleemed from the statistical anaylsis # and anaraming letters close to it objective_keys[0][10] = 't' objective_keys[1][8] = 'h' objective_keys[2][22] = 'e' objective_keys[2][14] = 'a' objective_keys[3][18] = 't' # Statment objective_keys[1][4] = 't' objective_keys[7][18] = 'n' objective_keys[8][13] = 't' # None objective_keys[4][19] = 'n' # good probabioty tells # objective_keys[1][18] = 'o' # objective_keys[4][18] = 'o' # objective_keys[5][21] = 'o' # objective_keys[6][14] = 'e' doubles up # objective_keys[8][22] = 'o' # # gueesing objective_keys[0][18] = 'm' objective_keys[1][24] = 'a' objective_keys[2][16] = 't' objective_keys[3][23] = 'h' objective_keys[4][16] = 'e' objective_keys[5][5] = 'm' objective_keys[6][6] = 'a' objective_keys[7][12] = 't' objective_keys[8][16] = 'i' objective_keys[0][3] = 'c' objective_keys[1][25] = 'i' #a fits objective_keys[3][7] = 'n' objective_keys[3][3] = 'f' objective_keys[4][24] = 'a' objective_keys[5][5] = 'm' objective_keys[6][0] = 'o' objective_keys[7][8] = 'u' objective_keys[8][0] = 's' objective_keys[4][20] = 'f' objective_keys[5][16] = 'r' objective_keys[6][10] = 'e' objective_keys[7][13] = 'e' objective_keys[8][4] = 'm' objective_keys[0][5] = 'a' objective_keys[1][21] = 'n' objective_keys[2][11] = 'd' objective_keys[3][0] = 'y' objective_keys[4][25] = 's' objective_keys[5][15] = 'o' objective_keys[6][25] = 'n' objective_keys[7][15] = 'w' objective_keys[8][2] = 'a' objective_keys[0][23] = 's' # a fits objective_keys[2][3] = 's' objective_keys[3][9] = 'k' # e fits objective_keys[5][11] = 'd' objective_keys[6][21] = 'w' objective_keys[7][6] = 'h' # a fits # t fits # objective_keys[1][23] = 'd' objective_keys[2][15] = 'o' # y fits # objective_keys[8] # objective_keys[3][3] = 'f' # # objective_keys[4][24] = 'i' # objective_keys[5][5] = 'l' # objective_keys[6][0] = 'e' # objective_keys[7][8] = 'h' # objective_keys[5][5] = 'm' # objective_keys[6][6] = 'a' # objective_keys[7][12] = 't' # objective_keys[8][16] = 'h' # objective_keys[0][1] = 's' # objective_keys[1][24] = 't' # objective_keys[2][0] = 'u' # objective_keys[3][22] = 'd' # objective_keys[4][16] = 'e' # objective_keys[5][21] = 'n' # objective_keys[3][3] = 'w' # objective_keys[4][24] = 'i' # objective_keys[5][5] = 'd' # objective_keys[6][0] = 't' # objective_keys[7][8] = 'h' # # objective_keys[8][0] = 'a' # objective_keys[3][19] = 'u' # objective_keys[4][18] = 'n' # objective_keys[5][19] = '' print([str(x) for x in range(0,25,1)]) for objective_key, cipher_text_column in zip(objective_keys, cipher_columns): print(objective_key) quagFrequency = QuagFrequency(each_column) new_decoder = Decode(cipher_text_column) objective_plain_text_columns.append(new_decoder.quag_breaker(objective_key)) plain_text_columns.append(new_decoder.quag_breaker( [getattr(quagFrequency, i)[x].letter for x, i in zip(initial_alpha, self.alpha)])) plain_text = [None] * len(cipher_text) k = -1 for each in objective_plain_text_columns: i = k+1 j = 0 while i < len(cipher_text): plain_text[i] = each[j] i += self.degree j += 1 k += 1 derp = (''.join(plain_text)) print([derp[i:i+9] for i in range(0, len(derp), 9)]) print(derp) exit()
class Run: def __init__(self, key_len, cipher_text): self.key_len = key_len self.alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] self.cipher_text = cipher_text self.brute = itertools.product(self.alphabet, repeat=key_len) self.decoder = Decode(self.cipher_text) nthMessage = NthMessage(self.cipher_text, key_len) self.ze_pre_analysis = nthMessage.output() self.brute = self.build_key() self.transDecode = None self.wordSearch = WordSearch() def build_key(self): # KRYMQYGSPZRDWLAZAV possible key ''' Builds key from pre analysis output ''' pre_key = itertools.product([0], repeat=self.key_len) # for each in [0,1,2,3,4,5,6]: # print(self.ze_pre_analysis[0][each].shift) # exit() for each in pre_key: # print(each) j = 0 key = '' for each_char in each: if each_char is None: key += '.' else: # print(each_char) key += self.ze_pre_analysis[j][each_char].shift j += 1 # print(key) yield key def pre_analysis(self): # Build the de-shifted text that is the best gueess from chi-squares messages = [] j = 0 while j < self.key_len: i = j nth_cypher_text = '' while i < len(self.cipher_text): nth_cypher_text += self.cipher_text[i] i += self.key_len messages.append(NthMessage(nth_cypher_text, self.key_len)) j += 1 return messages def start_combination(self): '''single thread''' for each in itertools.product([0], repeat=self.key_len): self.combination_cipher(each) ''' Multi Thread ''' # q = multiprocessing.Queue(maxsize=50) # jobs = [] # # # Create workers # for i in range(0, multiprocessing.cpu_count(), 1): # p = multiprocessing.Process(target=self.combination_cipher_worker, args=(q,)) # p.start() # jobs.append(p) # # # Feed items into the queue # for each_item in itertools.product([0], repeat=self.key_len): # q.put(each_item) # # # Wait for each worker to finish before continueing # for each_job in jobs: # each_job.join() def combination_cipher_worker(self, q): while True: if q.empty(): time.sleep(1) try: obj = q.get(timeout=1) self.combination_cipher(obj) except: # print('[!] run finished') break # print('ending worker') return def colmber_cipher_worker(self, q): while True: if q.empty(): time.sleep(1) try: cipher_text, each_trans_key = q.get(timeout=1) colmnDecode = Decode(cipher_text) plain_text, each_trans_key = colmnDecode.columnar(each_trans_key) words_len = self.wordSearch.run(plain_text) if words_len > 1: print(each_trans_key) print(words_len) with open('combination_cipher_result.txt', 'a') as results_file: results_file.write('%s | %s | %s\n' % (str(each_trans_key), str(plain_text), str(words_len))) except: # print('[!] run finished') break # print('ending worker') return def combination_cipher(self, pre_key): ''' combination cipher ''' j = 0 key = '' for each_char in pre_key: if each_char is None: key += '.' else: key += self.ze_pre_analysis[j].plain_texts[each_char].shift j += 1 # using each key test it each_key = key print(each_key) plain_text, key = self.decoder.runner(each_key) chiSquare = ChiSquare(plain_text) ic = chiSquare.ic # print('[-] IC: %s' % str(ic)) print('[-] CHI: %s' % str(chiSquare.chi_result)) if chiSquare.chi_result < 200: ''' Colmner cipher test''' for each_key_size in range(9,10,1): ''' multi thread code''' q = multiprocessing.Queue(maxsize=50) jobs = [] # Create workers for i in range(0, multiprocessing.cpu_count(), 1): p = multiprocessing.Process(target=self.colmber_cipher_worker, args=(q,)) p.start() jobs.append(p) # Feed items into the queue for each_item in itertools.permutations(range(1, each_key_size+1, 1)): q.put((plain_text, each_item)) # Wait for each worker to finish before continueing for each_job in jobs: each_job.join() # print(each_trans_key) # colmnDecode = Decode(plain_text) # trans_plain_text, each_trans_key = colmnDecode.columnar(each_trans_key) # words_len = self.wordSearch.run(trans_plain_text) # if words_len < 1: # print(words_len) # with open('combination_cipher_result.txt', 'a') as results_file: # results_file.write('%s | %s | %s\n' % (str(each_trans_key), str(plain_text), str(words_len))) ''' Permutation cipher test''' # print(key) # print('[+] Found a possible key running diagram analysis') # # # test the resulting plain text as a transposition cipher # self.transDecode = Decode(plain_text) # for each_degree in range(len(plain_text), len(plain_text), 1): # # print('[-] Running %d degree' % each_degree) # dia = Dia(plain_text, each_degree) # dia.permutation() # dia.run() # if dia.key is not None: # print(dia.key) # trans_plain_text, trans_key = self.transDecode.permutation(dia.key) # # # Look For words # wordSearch = WordSearch() # words_len = wordSearch.run(trans_plain_text) # print(key) # print(trans_plain_text) # print(words_len) # if words_len > 1: # print(words_len) # with open('combination_cipher_result.txt', 'a') as results_file: # results_file.write('%s | %s | %s\n' % (str(key), str(plain_text), str(words_len))) def start_simple_substitution(self): ''' MultiCore ''' q = multiprocessing.Queue(maxsize=50) jobs = [] # Create workers for i in range(0, multiprocessing.cpu_count(), 1): p = multiprocessing.Process(target=self.worker, args=(q,)) p.start() jobs.append(p) # Feed items into the queue for each_item in self.brute: q.put(each_item) # Wait for each worker to finish before continueing for each_job in jobs: each_job.join() def worker(self, q): while True: if q.empty(): time.sleep(1) try: obj = q.get(timeout=1) plain_text, key = self.decoder.runner(obj) # plain_text, key = self.decoder.beaufort_decrypt(obj) chiSquare = ChiSquare(plain_text) chi = chiSquare.output() ic = chiSquare.ic # print(plain_text) print('%s | %s | %s' % (str(key), str(ic), str(chi))) if ic > 0.06: print(ic) # print(plain_text) with open('vigenere.txt', 'a') as results_file: results_file.write('%s | %s | %s | %s' % (str(key), str(plain_text), str(ic), str(chi)), 'a') except: # print('[!] run finished') break # print('ending worker') return
def __init__(self, cipher_text, degree): self.check_cipher_text = cipher_text[:degree] alpha = [x for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'] self.keys = itertools.product(alpha, repeat=degree) self.decode = Decode(self.check_cipher_text)
class Run: def __init__(self, key_len, cipher_text): self.key_len = key_len self.alphabet = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ] self.cipher_text = cipher_text self.brute = itertools.product(self.alphabet, repeat=key_len) self.decoder = Decode(self.cipher_text) nthMessage = NthMessage(self.cipher_text, key_len) self.ze_pre_analysis = nthMessage.output() self.brute = self.build_key() self.transDecode = None self.wordSearch = WordSearch() def build_key(self): # KRYMQYGSPZRDWLAZAV possible key ''' Builds key from pre analysis output ''' pre_key = itertools.product([0], repeat=self.key_len) # for each in [0,1,2,3,4,5,6]: # print(self.ze_pre_analysis[0][each].shift) # exit() for each in pre_key: # print(each) j = 0 key = '' for each_char in each: if each_char is None: key += '.' else: # print(each_char) key += self.ze_pre_analysis[j][each_char].shift j += 1 # print(key) yield key def pre_analysis(self): # Build the de-shifted text that is the best gueess from chi-squares messages = [] j = 0 while j < self.key_len: i = j nth_cypher_text = '' while i < len(self.cipher_text): nth_cypher_text += self.cipher_text[i] i += self.key_len messages.append(NthMessage(nth_cypher_text, self.key_len)) j += 1 return messages def start_combination(self): '''single thread''' for each in itertools.product([0], repeat=self.key_len): self.combination_cipher(each) ''' Multi Thread ''' # q = multiprocessing.Queue(maxsize=50) # jobs = [] # # # Create workers # for i in range(0, multiprocessing.cpu_count(), 1): # p = multiprocessing.Process(target=self.combination_cipher_worker, args=(q,)) # p.start() # jobs.append(p) # # # Feed items into the queue # for each_item in itertools.product([0], repeat=self.key_len): # q.put(each_item) # # # Wait for each worker to finish before continueing # for each_job in jobs: # each_job.join() def combination_cipher_worker(self, q): while True: if q.empty(): time.sleep(1) try: obj = q.get(timeout=1) self.combination_cipher(obj) except: # print('[!] run finished') break # print('ending worker') return def colmber_cipher_worker(self, q): while True: if q.empty(): time.sleep(1) try: cipher_text, each_trans_key = q.get(timeout=1) colmnDecode = Decode(cipher_text) plain_text, each_trans_key = colmnDecode.columnar( each_trans_key) words_len = self.wordSearch.run(plain_text) if words_len > 1: print(each_trans_key) print(words_len) with open('combination_cipher_result.txt', 'a') as results_file: results_file.write('%s | %s | %s\n' % (str(each_trans_key), str(plain_text), str(words_len))) except: # print('[!] run finished') break # print('ending worker') return def combination_cipher(self, pre_key): ''' combination cipher ''' j = 0 key = '' for each_char in pre_key: if each_char is None: key += '.' else: key += self.ze_pre_analysis[j].plain_texts[each_char].shift j += 1 # using each key test it each_key = key print(each_key) plain_text, key = self.decoder.runner(each_key) chiSquare = ChiSquare(plain_text) ic = chiSquare.ic # print('[-] IC: %s' % str(ic)) print('[-] CHI: %s' % str(chiSquare.chi_result)) if chiSquare.chi_result < 200: ''' Colmner cipher test''' for each_key_size in range(9, 10, 1): ''' multi thread code''' q = multiprocessing.Queue(maxsize=50) jobs = [] # Create workers for i in range(0, multiprocessing.cpu_count(), 1): p = multiprocessing.Process( target=self.colmber_cipher_worker, args=(q, )) p.start() jobs.append(p) # Feed items into the queue for each_item in itertools.permutations( range(1, each_key_size + 1, 1)): q.put((plain_text, each_item)) # Wait for each worker to finish before continueing for each_job in jobs: each_job.join() # print(each_trans_key) # colmnDecode = Decode(plain_text) # trans_plain_text, each_trans_key = colmnDecode.columnar(each_trans_key) # words_len = self.wordSearch.run(trans_plain_text) # if words_len < 1: # print(words_len) # with open('combination_cipher_result.txt', 'a') as results_file: # results_file.write('%s | %s | %s\n' % (str(each_trans_key), str(plain_text), str(words_len))) ''' Permutation cipher test''' # print(key) # print('[+] Found a possible key running diagram analysis') # # # test the resulting plain text as a transposition cipher # self.transDecode = Decode(plain_text) # for each_degree in range(len(plain_text), len(plain_text), 1): # # print('[-] Running %d degree' % each_degree) # dia = Dia(plain_text, each_degree) # dia.permutation() # dia.run() # if dia.key is not None: # print(dia.key) # trans_plain_text, trans_key = self.transDecode.permutation(dia.key) # # # Look For words # wordSearch = WordSearch() # words_len = wordSearch.run(trans_plain_text) # print(key) # print(trans_plain_text) # print(words_len) # if words_len > 1: # print(words_len) # with open('combination_cipher_result.txt', 'a') as results_file: # results_file.write('%s | %s | %s\n' % (str(key), str(plain_text), str(words_len))) def start_simple_substitution(self): ''' MultiCore ''' q = multiprocessing.Queue(maxsize=50) jobs = [] # Create workers for i in range(0, multiprocessing.cpu_count(), 1): p = multiprocessing.Process(target=self.worker, args=(q, )) p.start() jobs.append(p) # Feed items into the queue for each_item in self.brute: q.put(each_item) # Wait for each worker to finish before continueing for each_job in jobs: each_job.join() def worker(self, q): while True: if q.empty(): time.sleep(1) try: obj = q.get(timeout=1) plain_text, key = self.decoder.runner(obj) # plain_text, key = self.decoder.beaufort_decrypt(obj) chiSquare = ChiSquare(plain_text) chi = chiSquare.output() ic = chiSquare.ic # print(plain_text) print('%s | %s | %s' % (str(key), str(ic), str(chi))) if ic > 0.06: print(ic) # print(plain_text) with open('vigenere.txt', 'a') as results_file: results_file.write( '%s | %s | %s | %s' % (str(key), str(plain_text), str(ic), str(chi)), 'a') except: # print('[!] run finished') break # print('ending worker') return