def cryptanalysis_affine(ciphertext): # your code here baseString = utilities_A4.get_baseString() length = len(baseString) dictList = utilities_A4.load_dictionary('engmix.txt') sub_baseString = [] for j in range(25, length): sub_baseString.append(baseString[:j + 1]) attempts = 0 for n_s in sub_baseString: m_i_table = mod.mul_inv_table(len(n_s)) for mi in m_i_table[0]: if m_i_table[1][mi] != 'NA': for beta in range(len(n_s)): k = [mi, beta] key = (n_s, k) plaintext = d_affine(ciphertext, key) attempts += 1 if len(utilities_A4.remove_nonalpha( plaintext)) < len(plaintext) / 2: continue if utilities_A4.is_plaintext(plaintext, dictList, 0.90): print('key found after ' + str(attempts) + ' attempts') return plaintext, key return '', ''
def cryptanalysis_decimation(ciphertext): text = utilities_A4.get_baseString() text_len = len(text) # num = [3,5,7,9,11,15,17,19,21,23,25] # pas = [] attempts = 0 for x in range(26, len(text)): testing_text = text[:x] testing_len = len(testing_text) for y in range(1,testing_len): inv = mod.mul_inv(y,testing_len) if inv != 'NA': check = d_decimation(ciphertext,(testing_text,y)) attempts+=1 is_plain = utilities_A4.is_plaintext(check, utilities_A4.load_dictionary('engmix.txt'), 0.9) if is_plain: print("Key found after ", attempts, "attempts") return check, (testing_text,y)
def cryptanalysis_decimation(ciphertext): plaintext = '' baseString = utilities_A4.get_baseString() dictList = utilities_A4.load_dictionary('engmix.txt') count = 0 for i in range(0, len(baseString) - 26 + 1): base = baseString[:26 + i] table = mod.mul_inv_table(26 + i) for x in table[1]: if x != 'NA': key = (base, table[0][x]) plaintext = d_decimation(ciphertext, key) count += 1 if utilities_A4.is_plaintext(plaintext.lower(), dictList, 0.9): print("Key found after:", count, "attemps") return plaintext, key[1] return '', ''
def test_q3(): print("-------------------------------------------") print("Testing Q3: Affine Cipher") print() alphabet = utilities_A4.get_lower() baseStr = alphabet k = [5, 8] plaintext = 'AFFINE CIPHER' key = (baseStr, k) print('key = ', key) print('plaintext = ', plaintext) ciphertext = solution_A4.e_affine(plaintext, key) print('ciphertext= ', ciphertext) plaintext2 = solution_A4.d_affine(ciphertext, key) print('plaintext2= ', plaintext2) print() baseStr = alphabet + ' ?!' k = [45, 3] plaintext = 'Is mission accomplished?' key = (baseStr, k) print('key = ', key) print('plaintext = ', plaintext) ciphertext = solution_A4.e_affine(plaintext, key) print('ciphertext= ', ciphertext) plaintext2 = solution_A4.d_affine(ciphertext, key) print('plaintext2= ', plaintext2) print() baseStr = alphabet + ' ?!.' k = [10, 19] plaintext = 'Plan B ... initiated' key = (baseStr, k) print('key = ', key) print('plaintext = ', plaintext) print('ciphertext= ', end='') solution_A4.e_affine(plaintext, key) print('plaintext2= ', end='') solution_A4.d_affine(plaintext, key) print() print('Testing cryptanalysis:') plaintext = 'There are two kinds of cryptography in this world: cryptography that will stop your kid sister ' plaintext += 'from reading your files, and cryptography that will stop major governments from reading your files.' baseString = utilities_A4.get_baseString()[:26] key = (baseString, [3, 9]) ciphertext = solution_A4.e_affine(plaintext, key) plaintext, key = solution_A4.cryptanalysis_affine(ciphertext) if plaintext != '': print('Key found = ', key) print('plaintext = ', plaintext) print() baseString = utilities_A4.get_baseString()[:27] key = (baseString, [17, 11]) ciphertext = solution_A4.e_affine(plaintext, key) plaintext, key = solution_A4.cryptanalysis_affine(ciphertext) if plaintext != '': print('Key found = ', key) print('plaintext = ', plaintext) print("-------------------------------------------") print() return
def test_q2(): print("-------------------------------------------") print("Testing Q2: Decimation Cipher") print() alphabet = utilities_A4.get_lower() baseStr = alphabet k = 5 plaintext = 'Strike in progress' key = (baseStr, k) print('key = ', key) print('plaintext = ', plaintext) ciphertext = solution_A4.e_decimation(plaintext, key) print('ciphertext= ', ciphertext) plaintext2 = solution_A4.d_decimation(ciphertext, key) print('plaintext2= ', plaintext2) print() baseStr = alphabet + ' ?!' k = 46 plaintext = 'Is mission accomplished?' key = (baseStr, k) print('key = ', key) print('plaintext = ', plaintext) ciphertext = solution_A4.e_decimation(plaintext, key) print('ciphertext= ', ciphertext) plaintext2 = solution_A4.d_decimation(ciphertext, key) print('plaintext2= ', plaintext2) print() baseStr = alphabet + ' ?!.' k = 10 plaintext = 'Plan B ... initiated' key = (baseStr, k) print('key = ', key) print('plaintext = ', plaintext) print('ciphertext= ', end='') solution_A4.e_decimation(plaintext, key) print('plaintext2= ', end='') solution_A4.d_decimation(plaintext, key) print() print('Testing cryptanalysis:') ciphertext = 'Mrhoyu on xhsehumm' plaintext, key = solution_A4.cryptanalysis_decimation(ciphertext) if plaintext != '': print('Key found = ', key) print('plaintext = ', plaintext) print() plaintext = 'There are two kinds of cryptography in this world: cryptography that will stop your kid sister ' plaintext += 'from reading your files, and cryptography that will stop major governments from reading your files.' baseString = utilities_A4.get_baseString()[:35] key = (baseString, 33) ciphertext = solution_A4.e_decimation(plaintext, key) plaintext, key = solution_A4.cryptanalysis_decimation(ciphertext) if plaintext != '': print('Key found = ', key) print('plaintext = ', plaintext) print("-------------------------------------------") print() return