def cryptanalysis_xshift(ciphertext): # your code here wordList = utilities.text_to_words(ciphertext) single = list() for word in wordList: if len(word) == 1 and word not in single: single.append(word) shiftStringList = utilities.get_shiftStringList() for s in single: for shiftString in shiftStringList: now_c = shiftString.index(s) ori_c = shiftString.index('I') dis = now_c - ori_c if now_c > ori_c else now_c - ori_c + 52 plaintext = d_xshift(ciphertext, (shiftString, dis)) if utilities.is_plaintext(plaintext, 'engmix.txt', 0.90): utilities.text_to_file(plaintext, 'plaintext_Jiayao_Pang_q2.txt') return (shiftString, dis), plaintext key = ('', 0) plaintext = '' return key, plaintext
def cryptanalysis_substitution(ciphertext): key = '''smzokelxrcitbudnhwfpyaqvjg':"!-; ,#?.''' # <--- change this line with your key key = utilities.adjust_key(key) # <--- keep this line # add lines to decrypt the ciphertext plaintext = d_substitution(ciphertext, key) # remember to write your plaintext to file utilities.text_to_file(plaintext, 'plaintext_Jiayao_Pang_q3.txt') return key, plaintext
def cryptanalysis_vigenere(ciphertext): # yoru code here FriedmanL = utilities.getKeyL_friedman(ciphertext) ShiftL = utilities.getKeyL_shift(ciphertext) non_alpha = utilities.get_nonalpha(ciphertext) if ShiftL != 1: # try every possible size start = min(FriedmanL, ShiftL) if start >= 3: start -= 1 end = max(FriedmanL, ShiftL) + 2 modified = utilities.remove_nonalpha(ciphertext) for size in range(start, end): plaintext = '' shiftList = [] blocks = utilities.text_to_blocks(modified, size) baskets = utilities.blocks_to_baskets(blocks) for element in baskets: # get the list of chi_squared for every shift chiList = [round(utilities.get_chiSquared(utilities.d_shift(element, (i, 'l'))), 4) for i in range(26)] shiftList.append(chiList.index(min(chiList))) for i in range(len(baskets)): baskets[i] = utilities.d_shift(baskets[i], (shiftList[i], 'l')) result = utilities.blocks_to_baskets(baskets) for e in result: plaintext += e plaintext = utilities.insert_nonalpha(plaintext, non_alpha) if utilities.is_plaintext(plaintext, 'engmix.txt', 0.90): plain = utilities.remove_nonalpha(plaintext)[:size] key = '' v_square = utilities.get_vigenereSquare() counter = 0 for pChar in plain: pIndex = v_square[0].index(pChar) kIndex = v_square[pIndex].index(blocks[0][counter]) counter += 1 key += v_square[0][kIndex] utilities.text_to_file(plaintext, 'plaintext_Jiayao_Pang_q4.txt') return key, plaintext key = 0 plaintext = '' return key, plaintext
def q4A(ciphertext): # your code here print( 'Provide your description here:\n' 'The ciphertext is all numbers --> the second step would be Polybius.\n' 'After decryption of Polybius, I start to decrypt the new cipher with easier ways.\n' 'First Atbash --> is_plaintext() is False\n' 'Second Shift Cryptanalysis --> False.\n' 'Third Columnar Transposition(only with key \'ba\') --> True\n' 'The result is: Columnar Transposition + Polybius') # <----- edit this plaintext, (key2, key1) = q4.d_type1(ciphertext) utilities.text_to_file(plaintext, 'q4A_plaintext.txt') return plaintext, (key1, key2)
def q4C(ciphertext): # your code here print( 'Provide your description here:\n' 'All the characters of the ciphertext are upper case --> It must be Hill Cipher.\n' 'I have writen a simple cryptanalysis function.\n' 'I constructed all the 2by2 matrix which are invertible. And after I got each new cipher,\n' 'I tried all the possible 3 ways:\n' ' Atbash & Columnar Transposition & Shift\n' ' It can\'t be Polybius since there is no numbers.' 'The cryptanalysis has failed for the first 2 ways but returned the right text for Shift Cipher.\n' 'The result is: Shift + Hill') # <----- edit this plaintext, (key2, key1) = q4.d_type3(ciphertext) utilities.text_to_file(plaintext, 'q4C_plaintext.txt') return plaintext, (key1, key2)
def q4B(ciphertext): # your code here print( 'Provide your description here:\n' 'There are so many \'q\'s in the ciphertext. The only way of encryption with\n' 'padding q is Columnar Transposition.\n' 'The new cipher can not be Polybius (not numbers).\n' 'It can not be Hill cipher since Columnar Transposition does not change cases of letters.\n' 'So, I still start from the easier ways.\n' 'First Atbash --> is_plaintext() is False\n' 'Second Shift Cryptanalysis --> True.\n' 'The result is: Shift + Columnar Transposition') # <----- edit this plaintext, (key2, key1) = q4.d_type2(ciphertext) utilities.text_to_file(plaintext, 'q4B_plaintext.txt') return plaintext, (key1, key2)
def task3(): print('{}'.format('-' * 40)) print("Start of Task 3: Scytale Cipher Testing") print() print('--------- Testing Encryption:') for i in range(3, 7): plainfile = 'plaintext1' + str(i - 2) + '.txt' plaintext = utilities.file_to_text(plainfile) ciphertext = A1_solution.e_scytale(plaintext, i) print('key = {}'.format(i)) print('ciphertext: ') print(ciphertext) utilities.text_to_file(ciphertext, 'ciphertext3' + str(i - 2) + '.txt') print() print('-------- Testing Decryption: ') for i in range(3, 7): ciphertext = utilities.file_to_text('ciphertext3' + str(i - 2) + '.txt') plaintext = A1_solution.d_scytale(ciphertext, i) print('key = {}'.format(i)) print('plaintext: ') print(plaintext) print() print('-------- Testing Cryptanalysis: ') for i in range(5, 7): file = 'ciphertext3' + str(i) + '.txt' ciphertext = utilities.file_to_text(file) key, plaintext = A1_solution.cryptanalysis_scytale(ciphertext) print('key = {}'.format(key), end=' , ') if plaintext == utilities.file_to_text('plaintext31.txt'): print('Plaintext verified') else: print('Plaintext mismatch') print() print('End of Task 3: Scytale Cipher Testing') print('{}'.format('-' * 40)) print() return
def task2(): print('{}'.format('-' * 40)) print("Start of Task 2: Extended Atbash Testing") print() print('-------- Testing Encryption: ') plaintext = utilities.file_to_text('plaintext14.txt') for i in range(5): ciphertext = A1_solution.e_eatbash(plaintext, i + 5) print('key = {}'.format(i)) print('ciphertext: ') print(ciphertext) utilities.text_to_file(ciphertext, 'ciphertext2' + str(i + 1) + '.txt') print() print('-------- Testing Decryption: ') for i in range(5): ciphertext = utilities.file_to_text('ciphertext2' + str(i + 1) + '.txt') plaintext = A1_solution.d_eatbash(ciphertext, i) print('key = {}'.format(i)) print('plaintext: ') print(plaintext) print() print('-------- Testing Cryptanalysis: ') for i in range(5): file = 'ciphertext2' + str(i + 1) + '.txt' ciphertext = utilities.file_to_text(file) key, plaintext = A1_solution.cryptanalysis_eatbash(ciphertext) print('key = {}'.format(key), end=' , ') if plaintext == utilities.file_to_text('plaintext14.txt'): print('Plaintext verified') else: print('Plaintext mismatch') print() print('End of Task 2: Extended Atbash Testing') print('{}'.format('-' * 40)) print() return
def cryptanalysis_xcrypt(ciphertext): # your code here if ciphertext[-1] == 'q': lastIndex = ciphertext.rfind('q') Index = ciphertext.rfind('q', 0, lastIndex) possibleKey = len(ciphertext) / (lastIndex - Index) plaintext = d_xcrypt(ciphertext, possibleKey) if utilities.is_plaintext(plaintext, 'engmix.txt', 0.90): utilities.text_to_file(plaintext, 'plaintext_Jiayao_Pang_q1.txt') return int(possibleKey), plaintext found = False for i in range(1, 501): plaintext = d_xcrypt(ciphertext, i) if utilities.is_plaintext(plaintext, 'engmix.txt', 0.90): found = True break if found is False: return 0, '' utilities.text_to_file(plaintext, 'plaintext_Jiayao_Pang_q1.txt') return i, plaintext
def task4(): print('{}'.format('-' * 40)) print("Start of Task 4: Polybius Square Cipher Testing") print() print('-------- Testing get_polybius_square:') print('start = "A", size = 5: {}'.format( A1_solution.get_polybius_square('A', 5))) print('start = "R", size = 6: {}'.format( A1_solution.get_polybius_square('R', 6))) print('start = "@", size = 7: {}'.format( A1_solution.get_polybius_square('@', 7))) print('start = "A", size = 5: {}'.format( A1_solution.get_polybius_square('A', 5))) print('start = " ", size = 8: {}'.format( A1_solution.get_polybius_square(' ', 8))) print('start = "+", size = 9: {}'.format( A1_solution.get_polybius_square('+', 9))) print('start = " ", size = 10: {}'.format( A1_solution.get_polybius_square(' ', 10))) print() print('-------- Testing Encryption:') start = ['D', 'A', ' ', '+', 'B'] for i in range(1, 6): plainfile = 'plaintext4' + str(i) + '.txt' plaintext = utilities.file_to_text(plainfile) key = (start[i - 1], i + 5) ciphertext = A1_solution.e_polybius(plaintext, key) print('key = {}'.format(key)) print('ciphertext: ') print(ciphertext) utilities.text_to_file(ciphertext, 'ciphertext4' + str(i) + '.txt') print() print('-------- Testing Decryption: ') for i in range(1, 6): ciphertext = utilities.file_to_text('ciphertext4' + str(i) + '.txt') key = (start[i - 1], i + 5) plaintext = A1_solution.d_polybius(ciphertext, key) print('key = {}'.format(key)) print('plaintext: ') print(plaintext) print() print('-------- Testing Cryptanalysis: ') size = [6, 9, 8] for i in range(6, 9): file = 'ciphertext4' + str(i) + '.txt' ciphertext = utilities.file_to_text(file) key, plaintext = A1_solution.cryptanalysis_polybius( ciphertext, size[i - 6]) print('key = {} --> {}'.format( key, A1_solution.get_polybius_square(key[0], key[1])), end=' , ') if plaintext == utilities.file_to_text('plaintext46.txt'): print('Plaintext verified') else: print('Plaintext mismatch') print() print('End of Task 4: Polybius Square Cipher Testing') print('{}'.format('-' * 40)) print() return