Example #1
0
def main():
    validation_message = utility.validate_initial_parameters()
    if validation_message != "":
        print(validation_message)
        sys.exit()

    print(constants.terminal_delimiter)
    print("\n" + constants.foreground_colorant_yellow + "The application started" + constants.attribute_default)
    private_key_vector = utility.generate_super_increasing_vector()
    modulo = utility.determine_modulo_acc_to_random_key_vector(private_key_vector)
    multiplicative_to_mask = utility.determine_element_to_mask(modulo)

    if utility.log_enabled:
        print("\nprivate_key_vector: " + str(private_key_vector))
        print("modulo: " + str(modulo))
        print("multiplicative_to_mask: " + str(multiplicative_to_mask))

    public_key_vector = ciphering.generate_public_key_vector(private_key_vector, modulo, multiplicative_to_mask)
    print("\nReceiver's public key vector is: \n" +
          str(public_key_vector) + " \n" +
          constants.foreground_colorant_green +
          "This vector will be used to cipher the message you text.\n" + constants.attribute_default)

    input_text = ""
    if not utility.random_text_test:
        input_text = utility.user_input("Please enter text to cipher", "")
        print("input text: " + str(input_text) + "\n")
    else:
        input_text = utility.generate_random_text(utility.length_of_random_text)
        print("generated input text: " + str(input_text) + "\n")
        utility.press_enter_to_continue()

    bit_converted_text = utility.convert_text_to_bit(input_text, len(public_key_vector))
    bit_grouped_sequences = utility.group_on_sequence(bit_converted_text, len(private_key_vector))

    print("Ciphering part of the application is starting...\n")

    if utility.log_enabled:
        print("bit_converted_text: " + str(bit_converted_text) + "\n")
        print("bit_grouped_sequences: " + str(bit_grouped_sequences) + "\n")

    ciphered_vector = ciphering.cipher_with_bit_sequences(public_key_vector, bit_grouped_sequences)

    print("Ciphering part of the application is over. You just sent this cipher: \n" + str(ciphered_vector) + " \n" +
          constants.foreground_colorant_green +
          "It can't be deciphered by anybody except the one who generated the public key vector." +
          constants.attribute_default + "\n")

    input_text = utility.user_input("Which one do you want to continue as? \n" +
                                    "For receiver: R, For man-in-the-middle attacker: A",
                                    constants.regex_pattern_decipher_side_choice)

    if input_text.upper() == "R":
        decipher_as_receiver(ciphered_vector, modulo, multiplicative_to_mask, private_key_vector)
    elif input_text.upper() == "A":
        decipher_as_attacker(ciphered_vector, public_key_vector)

    print("\n" + constants.foreground_colorant_yellow + "The application ended" + constants.attribute_default)
    print(constants.terminal_delimiter)
Example #2
0
def pipeline_carscom():
    """
    crawling pipeline for cars.com
    """
    maker, model, zipcode, radius, condition, car_json_file, directory = user_input(
    )
    page_num = 1
    num_per_page = 100
    start_url = generate_url(maker, model, zipcode, radius, car_json_file,
                             condition, page_num, num_per_page)
    csv_name = "{}-{}-{:d}-{:d}-{:s}.csv".format(maker, model, zipcode, radius,
                                                 condition)
    directory = os.path.dirname(os.path.realpath(__file__))
    csv_name = os.path.join(directory, csv_name)
    print("crawling {} {} {}...".format(condition, maker, model))
    craw_from_url(start_url, csv_name)
    print("finish crawling...")
    df = load_csvfile(csv_name)
    car_info = extract_info_from_csvfilename(csv_name)
    price_info = analyze_price(df)
    print_price_info(price_info, car_info)
def pipeline_carscom(directory='./'):
    """
    crawling pipeline for cars.com
    """
    maker, model, zipcode, radius, condition, car_json_file, directory = user_input(
    )
    page_num = 1
    num_per_page = 100
    start_url = generate_url(maker, model, zipcode, radius, car_json_file,
                             condition, page_num, num_per_page)
    csv_name = "{}-{}-{:d}-{:d}-{:s}.csv".format(maker, model, zipcode, radius,
                                                 condition)
    csv_name = os.path.join(directory, csv_name)
    print("crawling...")
    craw_from_url(start_url, csv_name)
    print("finish crawling...")
    df = load_csvfile(csv_name)
    if '/' in csv_name:
        csv_name = csv_name[csv_name.rfind('/') + 1:]
    maker, model = csv_name.split('-')[:2]
    maker = maker.upper()
    model = model.upper()
    analyze_price(df, maker, model)
Example #4
0
def main():
    validation_message = utility.validate_initial_parameters()
    if validation_message != "":
        print(validation_message)
        sys.exit()
    print(constants.terminal_delimiter)
    print("\n" + "The application started")
    #the generation of the super-increasing sequence (private key)
    private_key_vector = utility.generate_super_increasing_vector()
    #generation of madulo
    modulo = utility.determine_modulo_acc_to_random_key_vector(
        private_key_vector)
    multiplicative_to_mask = utility.determine_element_to_mask(modulo)

    print("\n private key: " + str(private_key_vector))
    # store the private key
    write_in_file("keys/private_key/private_key.txt", private_key_vector)

    print("modulo (m) : " + str(modulo))
    # store the public key
    write_in_file("keys/public_key/modulo.txt", modulo)

    print("multiplicative_to_mask (n) : " + str(multiplicative_to_mask))
    write_in_file("keys/public_key/multiplicative_to_mask.txt",
                  multiplicative_to_mask)

    #the generation of the public key from private key, modulo and multiplicative
    public_key_vector = ciphering.generate_public_key_vector(
        private_key_vector, modulo, multiplicative_to_mask)
    print("\n public key : " + str(public_key_vector))

    write_in_file("keys/public_key/public_key.txt", public_key_vector)

    input_text = ""
    if not utility.random_text_test:
        # taking text input
        input_text = utility.user_input("enter the text to encrypt ", "")
        print("input text: " + str(input_text) + "\n")
        write_in_file("result/input_msg_clear.txt", input_text)

    else:
        input_text = utility.generate_random_text(
            utility.length_of_random_text)
        print("generated input text: " + str(input_text) + "\n")
        utility.press_enter_to_continue()

    bit_converted_text = utility.convert_text_to_bit(input_text,
                                                     len(public_key_vector))
    bit_grouped_sequences = utility.group_on_sequence(bit_converted_text,
                                                      len(private_key_vector))

    print(" Start of Encryption ...\n")

    if utility.log_enabled:
        print("bit_converted_text: " + str(bit_converted_text) + "\n")
        print("bit_grouped_sequences: " + str(bit_grouped_sequences) + "\n")
# encrypt clear text
    ciphered_vector = ciphering.cipher_with_bit_sequences(
        public_key_vector, bit_grouped_sequences)
    # store the text encrypt
    print("encrypted text : " + str(ciphered_vector))
    write_in_file("result/output_msg_crypt.txt", ciphered_vector)
def pipeline_market_check(directory='./'):
    """
    the whole crawling pipeline for market check

    Args:
        directory: output directory
    """
    maker, model, zipcode, radius, condition, market_key_file, directory = user_input(
    )

    # convert zipcode to latitude and longitude
    zipSearch = ZipcodeSearchEngine()
    zipinfo = zipSearch.by_zipcode(str(zipcode))
    latitude, longtitude = str(zipinfo["Latitude"]), str(zipinfo["Longitude"])

    # read assess key for api
    with open(market_key_file, "r") as key:
        api_key = key.read()

    car_market_url = "http://api.marketcheck.com/v1/search"

    # start with index = 0, rows = 50 (max cars per request)
    max_rows_per_request = 50
    querystring = {
        "api_key": api_key,
        "make": maker,
        "latitude": latitude,
        "longitude": longtitude,
        "radius": str(radius),
        "car_type": condition,
        "seller_type": "dealer",
        "start": "0",
        "rows": str(max_rows_per_request),
    }
    if model != "all":
        querystring["model"] = model
    headers = {'Host': 'marketcheck-prod.apigee.net'}

    response = requests.request("GET",
                                car_market_url,
                                headers=headers,
                                params=querystring)

    cars_json = json.loads(response.text)
    count = (cars_json["num_found"])
    num_of_requests = (count + max_rows_per_request -
                       1) // max_rows_per_request
    print("Total number of request is {:d}".format(num_of_requests))

    short_csv_header = [
        "name", "VIN", "price", "miles", "Exterior Color", "Interior Color"
    ]
    dict_header = [
        "heading", "vin", "price", "miles", "exterior_color", "interior_color"
    ]  # names from API response
    long_csv_header = [
        "name", "VIN", "make", "model", "year", "price", "miles",
        "Exterior Color", "Interior Color", "Seller Name", "Seller Phone",
        "Transmission", "Drivetrain"
    ]  # car attributes stored in csv table
    csv_rows = []  # stores each crawled car as an dictionary

    for ite in range(num_of_requests):
        print("Sending the {:d}th request".format(ite))
        # query the API with new offset json
        if ite != 0:
            querystring["start"] = str(ite * max_rows_per_request)
            response = requests.request("GET",
                                        car_market_url,
                                        headers=headers,
                                        params=querystring)
            cars_json = json.loads(response.text)

        for car in cars_json["listings"]:  # car is a dictionary
            car_dict = {
                ch: car.get(dh, None)
                for ch, dh in zip(short_csv_header, dict_header)
            }
            if "dealer" in car:
                dealer = car["dealer"]
                car_dict["Seller Phone"] = dealer.get("phone", None)
                car_dict["Seller Name"] = dealer.get("name", None)
            if "build" in car:
                build = car["build"]
                car_dict["Transmission"] = build.get("transmission", None)
                car_dict["Drivetrain"] = build.get("drivetrain", None)
                car_dict["year"] = build.get("year", None)
                car_dict["make"] = build.get("make", None)
                car_dict["model"] = build.get("model", None)
            csv_rows.append(dict(car_dict))

    # write data to csv file
    csv_name = "{}-{}-{:d}-{:d}-{:s}.csv".format(maker, model, zipcode, radius,
                                                 condition)
    csv_name = os.path.join(directory, csv_name)
    write_cars_to_csv(csv_name, long_csv_header, csv_rows)

    # do some price analysis there
    df = load_csvfile(csv_name)
    maker, model = maker.upper(), model.upper()
    analyze_price(df, maker, model)
Example #6
0
def main():
    validation_message = utility.validate_initial_parameters()
    if validation_message != "":
        print(validation_message)
        sys.exit()

    print(constants.terminal_delimiter)
    print("\n" + constants.foreground_colorant_yellow + "The application started" + constants.attribute_default)
    private_key_primes_holder = list()
    generated_key_found = False
    t1 = time.process_time()
    print("\nPublic keys are generating...")
    while generated_key_found is False:
        private_key_primes_holder.append(utility.generate_large_prime(utility.private_key_primes_bit_length))
        private_key_primes_holder.append(utility.generate_large_prime(utility.private_key_primes_bit_length))
        if private_key_primes_holder[0] == private_key_primes_holder[1]:
            private_key_primes_holder = list()
            print("\n" + str(utility.private_key_primes_bit_length) + " bit prime number generation produce the same" +
                  " prime numbers for pair. So, public keys are generating again...")
        else:
            generated_key_found = True
    print("\nGenerating two " + str(utility.private_key_primes_bit_length) + " bit prime numbers" + " took " +
          str(time.process_time()-t1) + " ms.")
    t = time.process_time()
    multiplication_of_key_primes = private_key_primes_holder[0] * private_key_primes_holder[1]
    print("\nMultiplication of key primes took " + str(time.process_time() - t) + " ms.")
    t = time.process_time()
    totient_of_key_primes = utility.calculate_totient_of_primes(private_key_primes_holder[0],
                                                                private_key_primes_holder[1])
    print("\nCalculating totient of key primes took " + str(time.process_time() - t) + " ms.")
    t = time.process_time()
    power_prime_to_mask = utility.determine_power_prime_to_mask()
    print("\nDetermining power prime to mask took " + str(time.process_time() - t) + " ms.")
    t = time.process_time()
    if utility.log_enabled:
        print("\nprivate_key_primes: " + str(private_key_primes_holder[0]) + ", " + str(private_key_primes_holder[1]))
        print("totient_of_key_primes: " + str(totient_of_key_primes))
        print("power_prime_to_mask: " + str(power_prime_to_mask))

    print("\nPublic keys are generated. It took " + str(time.process_time()-t) + " ms in total. \n" +
          "power to mask: " + str(power_prime_to_mask) + " \n" +
          "key primes multiplication: " + str(multiplication_of_key_primes) + " \n" +
          constants.foreground_colorant_green +
          "These keys will be used to cipher the message you text.\n" + constants.attribute_default)

    input_text = ""
    if not utility.random_text_test:
        input_text = utility.user_input("Please enter text to cipher", "")
        print("input text: " + str(input_text) + "\n")
    else:
        input_text = utility.generate_random_text(utility.length_of_random_text)
        print("generated input text: " + str(input_text) + "\n")
        utility.press_enter_to_continue()

    print("Ciphering part of the application is starting...\n")

    ascii_response_of_text = utility.text_to_ascii_response(input_text)

    if utility.log_enabled:
        print("ascii_response_of_text: " + str(ascii_response_of_text) + "\n")

    cipher_text = ciphering.cipher_with_public_keys(ascii_response_of_text,
                                                    multiplication_of_key_primes, power_prime_to_mask)

    print("Ciphering part of the application is over. You just sent this cipher text: \n" + str(cipher_text) + " \n" +
          constants.foreground_colorant_green +
          "It can't be deciphered by anybody except the one who generated the public key vector." +
          constants.attribute_default + "\n")

    decipher_as_receiver(cipher_text, private_key_primes_holder, power_prime_to_mask, multiplication_of_key_primes)

    print("\n" + constants.foreground_colorant_yellow + "The application ended" + constants.attribute_default)
    print(constants.terminal_delimiter)
Example #7
0
def main():
    validation_message = utility.validate_initial_parameters()
    if validation_message != "":
        print(validation_message)
        sys.exit()

    print(constants.terminal_delimiter)
    print("\n" + constants.foreground_colorant_yellow +
          "The application started" + constants.attribute_default)
    private_key_primes_holder = list()
    generated_key_found = False
    t1 = time.process_time()
    print("\nPublic keys are generating...")
    while generated_key_found is False:
        private_key_primes_holder.append(
            utility.generate_large_prime(
                utility.private_key_primes_bit_length))
        private_key_primes_holder.append(
            utility.generate_large_prime(
                utility.private_key_primes_bit_length))
        if private_key_primes_holder[0] == private_key_primes_holder[1]:
            private_key_primes_holder = list()
            print(
                "\n" + str(utility.private_key_primes_bit_length) +
                " bit prime number generation produce the same" +
                " prime numbers for pair. So, public keys are generating again..."
            )
        else:
            generated_key_found = True
    print("\nGenerating two " + str(utility.private_key_primes_bit_length) +
          " bit prime numbers" + " took " + str(time.process_time() - t1) +
          " ms.")
    t = time.process_time()
    multiplication_of_key_primes = private_key_primes_holder[
        0] * private_key_primes_holder[1]
    print("\nMultiplication of key primes took " +
          str(time.process_time() - t) + " ms.")
    t = time.process_time()
    totient_of_key_primes = utility.calculate_totient_of_primes(
        private_key_primes_holder[0], private_key_primes_holder[1])
    print("\nCalculating totient of key primes took " +
          str(time.process_time() - t) + " ms.")
    t = time.process_time()
    power_prime_to_mask = utility.determine_power_prime_to_mask()
    print("\nDetermining power prime to mask took " +
          str(time.process_time() - t) + " ms.")
    t = time.process_time()
    if utility.log_enabled:
        print("\nprivate_key_primes: " + str(private_key_primes_holder[0]) +
              ", " + str(private_key_primes_holder[1]))
        print("totient_of_key_primes: " + str(totient_of_key_primes))
        print("power_prime_to_mask: " + str(power_prime_to_mask))

    print("\nPublic keys are generated. It took " +
          str(time.process_time() - t) + " ms in total. \n" +
          "power to mask: " + str(power_prime_to_mask) + " \n" +
          "key primes multiplication: " + str(multiplication_of_key_primes) +
          " \n" + constants.foreground_colorant_green +
          "These keys will be used to cipher the message you text.\n" +
          constants.attribute_default)

    input_text = ""
    if not utility.random_text_test:
        input_text = utility.user_input("Please enter text to cipher", "")
        print("input text: " + str(input_text) + "\n")
    else:
        input_text = utility.generate_random_text(
            utility.length_of_random_text)
        print("generated input text: " + str(input_text) + "\n")
        utility.press_enter_to_continue()

    print("Ciphering part of the application is starting...\n")

    ascii_response_of_text = utility.text_to_ascii_response(input_text)

    if utility.log_enabled:
        print("ascii_response_of_text: " + str(ascii_response_of_text) + "\n")

    cipher_text = ciphering.cipher_with_public_keys(
        ascii_response_of_text, multiplication_of_key_primes,
        power_prime_to_mask)

    print(
        "Ciphering part of the application is over. You just sent this cipher text: \n"
        + str(cipher_text) + " \n" + constants.foreground_colorant_green +
        "It can't be deciphered by anybody except the one who generated the public key vector."
        + constants.attribute_default + "\n")

    decipher_as_receiver(cipher_text, private_key_primes_holder,
                         power_prime_to_mask, multiplication_of_key_primes)

    print("\n" + constants.foreground_colorant_yellow +
          "The application ended" + constants.attribute_default)
    print(constants.terminal_delimiter)