def decrypt(text, cipher): maes = MiniAES() bin = BinaryStrings() plain = ascii_to_bin(text) cipher = bin_to_ascii(cipher) cipher = ascii_to_bin(cipher) table = {} for x in range(_sage_const_0, _sage_const_16): for y in range(_sage_const_0, _sage_const_16): for z in range(_sage_const_0, _sage_const_16): for q in range(_sage_const_0, _sage_const_16): key = [x, y, z, q] key = maes.integer_to_binary(key) encryption = maes(plain, key, algorithm="encrypt") table[bin_to_ascii(encryption)] = key # print("First Half Done !!!!!!!!!!!!!!") for x in range(_sage_const_0, _sage_const_16): for y in range(_sage_const_0, _sage_const_16): for z in range(_sage_const_0, _sage_const_16): for q in range(_sage_const_0, _sage_const_16): key = [x, y, z, q] key = maes.integer_to_binary(key) decryption = bin_to_ascii( maes(cipher, key, algorithm="decrypt")) if decryption in table.keys(): k1 = table[decryption] k2 = key print("matched key1 in binary: " + str(table[decryption])) print("matched key2 in binary: " + str(key)) print("key1 in ASCII: " + bin_to_ascii(table[decryption])) print("key2 in ASCII: " + bin_to_ascii(key)) return (k1, k2)
def verify(text, cipher, k1, k2): maes = MiniAES() bin = BinaryStrings() plain = ascii_to_bin(text) cipher = bin_to_ascii(cipher) cipher = ascii_to_bin(cipher) k1 = maes.integer_to_binary(k1) k2 = maes.integer_to_binary(k2) encryption = bin_to_ascii(maes(plain, k1, algorithm="encrypt")) decryption = bin_to_ascii(maes(cipher, k2, algorithm="decrypt")) if encryption == decryption: return true
def n2text(listOfNumb): result = '' for i in listOfNumb: if i == 1: return result b = str(bin(i)[2:]) b = '0' * (8 - len(b)) + b result += bin_to_ascii(b) return result
def get_flag(): # Recover message _, u, v = xgcd(e1, e2) m = pow(ct1, u, modulus) * pow(ct2, v, modulus) # Get binary representation and pad bin = Integer(m).binary() while (len(bin) % 8 != 0): bin = '0' + bin # Return ASCII string print(bin_to_ascii(bin))
def i2s(list_of_blocks,block_size): S = '' for i in list_of_blocks: s = "{0:b}".format(int(i)) S += '0'*(block_size-len(s)) + s return bin_to_ascii(S)