def crack_ciphertext(public_key, ciphertext): matrix = liblll.create_matrix_from_knapsack(public_key, ciphertext) reduced_basis = liblll.lll_reduction(matrix) guess = liblll.best_vect_knapsack(reduced_basis) if 1 in guess: return util.convert_bits_to_string(''.join([str(x) for x in guess]))
def reduce_basis(p1,p2): mat_basis = liblll.create_matrix([[p1[0],p2[0]],[p1[1],p2[1]]]) mat_reduced = liblll.lll_reduction(mat_basis) a0 = mat_reduced[0][0].numerator # lll library creates fractions with denominator 1, so grab numerator a1 = mat_reduced[0][1].numerator b0 = mat_reduced[1][0].numerator b1 = mat_reduced[1][1].numerator return [[a0,b0],[a1,b1]]
def reduce_basis(p1, p2): mat_basis = liblll.create_matrix([[p1[0], p2[0]], [p1[1], p2[1]]]) mat_reduced = liblll.lll_reduction(mat_basis) a0 = mat_reduced[0][ 0].numerator # lll library creates fractions with denominator 1, so grab numerator a1 = mat_reduced[0][1].numerator b0 = mat_reduced[1][0].numerator b1 = mat_reduced[1][1].numerator return [[a0, b0], [a1, b1]]
def solve_knapsack_liblll(a, m): """ a is the list of elements m is the expected subset sum """ for curr_m in (m, sum(a) - m): matrix = create_matrix_from_knapsack(a, curr_m) reduced = lll_reduction(matrix) try: return (find_solution_vector(reduced, a, curr_m), int(m == curr_m)) except SolutionNotFound: pass raise SolutionNotFound()
def attack_by_lll(msg_cipher, public_key_vector): text_clear = "" for i in range(0, len(msg_cipher)): element_message_cipher = msg_cipher[i] base_vector_list = attacking.create_base_vector_list(public_key_vector,element_message_cipher) matrix_to_lll_reduction = liblll.create_matrix(base_vector_list) reduced_matrix = liblll.lll_reduction(matrix_to_lll_reduction) deciphered_bit_sequence = attacking.get_first_column_as_bit_sequence(reduced_matrix) bit_to_text = utility.convert_bit_to_text(deciphered_bit_sequence, len(public_key_vector)) text_clear += bit_to_text print("clear-text/cracked-text : => " +str(text_clear)) write_in_file("text_clear.txt",text_clear) return True
def decipher_as_attacker(ciphered_vector, public_key_vector): t = time.process_time() print("\nAs an " + constants.bold_attribute + "ATTACKER" + constants.attribute_default + ", you own the ciphered vector, public key vector. \n" + constants.foreground_colorant_green + "So, you can decipher the cipher text with using lattice reduction." + constants.attribute_default + "\n") print("Deciphering part is about to start...\n") deciphered_text = "" print("LLL basis lattice reduction algorithm is about to start...\n") for i in range(0, len(ciphered_vector)): ciphered_message = ciphered_vector[i] base_vector_list = attacking.create_base_vector_list(public_key_vector, ciphered_message) if utility.log_enabled: print("base_vector_list: " + str(base_vector_list) + "\n") matrix_to_lll_reduction = liblll.create_matrix(base_vector_list) if utility.log_enabled: print("matrix_to_lll_reduction:\n") liblll.print_mat(matrix_to_lll_reduction) print("\n") reduced_matrix = liblll.lll_reduction(matrix_to_lll_reduction) if utility.log_enabled: print("reduced_matrix:\n") liblll.print_mat(reduced_matrix) print("\n") deciphered_bit_sequence = attacking.get_first_column_as_bit_sequence(reduced_matrix) if utility.log_enabled: print("deciphered_bit_sequence:\n") liblll.print_mat(deciphered_bit_sequence) print("\n") bit_to_text = utility.convert_bit_to_text(deciphered_bit_sequence, len(public_key_vector)) deciphered_text += bit_to_text print("Lattice reduction algorithm is over.\n") print("Finished to decipher the text in " + str(time.process_time()-t) + " ms as an attacker.\n\n" + "Original text: " + str(deciphered_text)) return True
for i, vi in enumerate(v): row = [k * vi] + ([0] * i) + [1] + ([0] * (len(v) - i - 1)) row = map(lambda x: Fraction(x), row) M.append(row) ### LLL time : bitches ### def print_matrix(m, name): print name for r in m: print r from liblll import lll_reduction print_matrix(M, 'M:') Mr = lll_reduction(M) print_matrix(Mr, 'Mr:') Ms = [x[1:] for x in Mr] print_matrix(Ms, 'Ms:') k1 = 74385634756347465439475374765172563 # 716238129874897247894615317635178653875363187536781350385107359713876
from liblll import lll_reduction vecs = [ (1, -1, 3), (1, 0, 5), (1, 2, 6) ] vecs = [ (2, 2), (-1, 4), ] out = lll_reduction(vecs) import matplotlib.pyplot as plt def scale(n, m): x = vecs[0][0] * n + vecs[1][0] * m y = vecs[0][1] * n + vecs[1][1] * m return (x, y) v1 = scale(6, 7) v2 = scale(13, 6) vecs = [v1, v2] xs = [] ys = [] for n in range(0, 25):
from liblll import lll_reduction vecs = [(1, -1, 3), (1, 0, 5), (1, 2, 6)] vecs = [ (2, 2), (-1, 4), ] out = lll_reduction(vecs) import matplotlib.pyplot as plt def scale(n, m): x = vecs[0][0] * n + vecs[1][0] * m y = vecs[0][1] * n + vecs[1][1] * m return (x, y) v1 = scale(6, 7) v2 = scale(13, 6) vecs = [v1, v2] xs = [] ys = [] for n in range(0, 25): for m in range(0, 25): x = vecs[0][0] * n + vecs[1][0] * m y = vecs[0][1] * n + vecs[1][1] * m xs.append(x)