def encrypt(polynomial, seed, input_file, out_encrypted): if check_params(polynomial, seed) is False or len(polynomial) == 0: print("Złe parametry") sys.exit(0) x = readBinFile.read_bin_file_to_bitarray(input_file) x = list(map(int, x)) seed = list(map(int, seed)) # last_index_1_value = polynomial.rfind("1") # first_index_1_value = polynomial.find("1") polynomial = correct_value(polynomial) # (x^4 + x^2 + x^1) polynomial_degree = len(polynomial) x_degree = len(x) shift_table = create_shift_table(x_degree, polynomial_degree) polynomial_array = polynomial reversed_polynomial_array = polynomial_array[::-1] # significant_value = polynomial_degree - (last_index_1_value - first_index_1_value) for row in range(x_degree + 1): xor_sum = 0 for col in range(polynomial_degree): if row == 0: # First row shift_table[row] = seed break else: # Every row despite first one if polynomial_degree == 1: shift_table[row][col] = (x[row - 1] + shift_table[row - 1][col]) % 2 else: if reversed_polynomial_array[col] == 1: xor_sum += shift_table[row - 1][col] if col > 0: shift_table[row][col] = shift_table[row - 1][col - 1] if col == (polynomial_degree - 1): # Last column in every row xor_sum += x[row - 1] shift_table[row][0] = xor_sum % 2 key = bitarray.bitarray() for row in range(x_degree): key.append(shift_table[row + 1][0]) # current_dir = pathlib.Path(__file__).parent # cur_time = time.strftime("%H:%M:%S") readBinFile.write_bin_file(out_encrypted, key)
def decrypt(seed, poly, file, file2): obj = LFSR.LFSR() obj.lfsr(seed, poly) counter = 0 array2 = readBinFile.read_bin_file_to_bitarray(os.getcwd() + "/spr2/" + file) array3 = array2[:] for i in range(len(array2)): counter += 1 x = obj.next() if x is None: return array3[i] = (array2[i] + x) % 2 if counter % 50000 == 0: print(counter) readBinFile.write_bin_file(os.getcwd() + "/spr2/" + file2, array3)
def decrypt(polynomial, seed, input_file, out): y = readBinFile.read_bin_file_to_bitarray(input_file) y = list(map(int, y)) seed = list(map(int, seed)) polynomial = correct_value(polynomial) # (x^4 + x^2 + x^1) polynomial_degree = len(polynomial) y_degree = len(y) shift_table = create_shift_table(y_degree, polynomial_degree) polynomial_array = polynomial reversed_polynomial_array = polynomial_array[::-1] for row in range(y_degree + 1): for col in range(polynomial_degree): if row == 0: # First row shift_table[row] = seed break else: # For every row if col == 0: shift_table[row][col] = y[row - 1] elif col > 0: shift_table[row][col] = shift_table[row - 1][col - 1] x = bitarray.bitarray() for row in range(y_degree): value = 0 for col in range(polynomial_degree): if reversed_polynomial_array[col] == 1: value += shift_table[row][col] value += y[row] value = value % 2 x.append(value) # current_dir = pathlib.Path(__file__).parent # cur_time = time.strftime("%H:%M:%S") readBinFile.write_bin_file(out, x)
def decrypt_file_full_file(input_encrypted_file, out, input_key): message_encrypted_file = readBinFile.read_bin_file_to_bitarray( input_encrypted_file) decrypted_message = bitarray() for block in range(int(len(message_encrypted_file) / 64)): block_message = message_encrypted_file[(block * 64):((block + 1) * 64)] block_decrypted_message = decrypt_block(block_message, input_key) decrypted_message += block_decrypted_message # delete padding decrypted_message.reverse() message_copy = decrypted_message.copy() for num in range(len(decrypted_message)): if decrypted_message[num] == 1: message_copy.pop(0) break message_copy.pop(0) message_copy.reverse() readBinFile.write_bin_file(out, message_copy)
def encrypt_file_full_file(input_file, out, input_key): message_file = readBinFile.read_bin_file_to_bitarray(input_file) # add padding file_lenght = message_file.length() % 64 if file_lenght != 0: to_add = 64 - file_lenght + 63 message_file += bitarray("1") message_file += to_add * bitarray("0") else: message_file += bitarray("1") message_file += 63 * bitarray("0") encrypted_message = bitarray() for block in range(int(len(message_file) / 64)): block_message = message_file[(block * 64):((block + 1) * 64)] block_encrypted_message = encrypt_block(block_message, input_key) encrypted_message += block_encrypted_message readBinFile.write_bin_file(out, encrypted_message)