Exemplo n.º 1
0
def download_price_data():

    print("Connect to Factory.")
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = ('localhost', 10000)
    sock.connect(server_address)

    try:

        print("Encrypting Authentication Code.")
        encrypted_code = factory_public_key.encrypt(AUTH_CODE, 24)

        # Send data
        print("Send Authentication Code.")
        message = msg.generate_msg(encrypted_code[0])
        sock.sendall(message)

        # Get response
        print("Downloading New Price Data.")
        response = sock.recv(4096)

        print("Decrypting New Price Data")
        encrpted_priced = msg.get_package_of_msg(response)
        price = enduser_private_key.decrypt(encrpted_priced)

        print("New Pirce Saved.\n")
        f = open("end_user_price.txt", "w")
        f.write(price)
        f.close()

    finally:
        sock.close()
Exemplo n.º 2
0
def get_public_key(ID):
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = ('localhost', 33333)
    print("connecting to KDC")
    sock.connect(server_address)

    try:
        print("Send Request to KDC.")
        T1 = str(datetime.datetime.now())
        request = msg.generate_msg("%s|%s" % (ID, T1))

        # Send data
        sock.sendall(request)

        print("Receive Response From KDC.")
        response = sock.recv(10000)

        print("Parse Response and Get Public Key")
        key = msg.get_package_of_msg(response)
        signature_of_kdc = msg.get_Trail_of_msg(response)

        print("Verifying Signature")
        if not cipher.verify(key, signature_of_kdc, kdc_public_key):
            print("Verify Failed.")
            return

    finally:
        print >> sys.stderr, 'closing socket'
        sock.close()

    return key
Exemplo n.º 3
0
def get_public_key(ID):
        # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = ('localhost', 33333)
    print("connecting to KDC")
    sock.connect(server_address)

    try:
        print("Send Request to KDC.")
        T1 = str(datetime.datetime.now())
        request = msg.generate_msg("%s|%s"%(ID,T1))

        # Send data
        sock.sendall(request)

        print("Receive Response From KDC.")
        response = sock.recv(10000)

        print("Parse Response and Get Public Key")
        key = msg.get_package_of_msg(response)
        signature_of_kdc = msg.get_Trail_of_msg(response)

        print("Verifying Signature")
        if not cipher.verify(key, signature_of_kdc, kdc_public_key):
            print("Verify Failed.")
            return                      

    finally:
        print >>sys.stderr, 'closing socket'
        sock.close()

    return key
Exemplo n.º 4
0
def download_price_data():

    print("Connect to Factory.")
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = ("localhost", 10000)
    sock.connect(server_address)

    try:

        print("Encrypting Authentication Code.")
        encrypted_code = factory_public_key.encrypt(AUTH_CODE, 24)

        # Send data
        print("Send Authentication Code.")
        message = msg.generate_msg(encrypted_code[0])
        sock.sendall(message)

        # Get response
        print("Downloading New Price Data.")
        response = sock.recv(4096)

        print("Decrypting New Price Data")
        encrpted_priced = msg.get_package_of_msg(response)
        price = enduser_private_key.decrypt(encrpted_priced)

        print("New Pirce Saved.\n")
        f = open("end_user_price.txt", "w")
        f.write(price)
        f.close()

    finally:
        sock.close()
Exemplo n.º 5
0
def key_exchange_server():
    global factory_public_key_text
    global factory_public_key 

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server_address = ('127.0.0.1', 20001)
    sock.bind(server_address)
    sock.listen(1)
    while True:
        # Wait for a connection
        # print >>sys.stderr, 'waiting for a connection'
        connection, client_address = sock.accept()
        try:

            print("Receive ID and N1 From Factory.")
            data = connection.recv(4096)
            data = msg.get_package_of_msg(data)
            message = enduser_private_key.decrypt(data)
            message = message.split("|")
            ID = message[0]
            N1 = int(message[1])
            N2 = N1+1
            
            print("To Get Public Key of Factory")
            factory_public_key_text = get_public_key(ID)
            factory_public_key = RSA.importKey(factory_public_key_text)

            response = "%d|%d" % (N1, N2)
            encrypted_code = factory_public_key.encrypt(response,24)
            
            
            print("Send N1 and N2 to Factory")
            message = msg.generate_msg(encrypted_code[0])
            connection.sendall(message)

            print("Receive Finally Check")
            data = connection.recv(4096)
            data = msg.get_package_of_msg(data)
            

        finally:
            # Clean up the connection
            connection.close()
Exemplo n.º 6
0
def send_server():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server_address = ('127.0.0.1', 10000)
    sock.bind(server_address)
    sock.listen(5)
    while True:
        connection, client_address = sock.accept()
        try:
            print 'connection from', client_address

            # Receive the data in small chunks and retransmit it
            while True:

                data = connection.recv(10000)
                if data:
                    print("Get Patch Request From Enduser.")

                    encrypt_auth_code = msg.get_package_of_msg(data)
                    signature_of_enduser = msg.get_Trail_of_msg(data)

                    print("Verifying Signature")
                    if not cipher.verify(encrypt_auth_code, signature_of_enduser, enduser_public_key):
                        print("Verify Failed.")
                        return 

                    print("Decrypt Request.")
                    decrypted_code = factory_private_key.decrypt(encrypt_auth_code)
                    
                    print("Check Authentication Code.")
                    if decrypted_code == AUTH_CODE: 
                        print("Check Success!")

                        print("Encrypt Price Data.")
                        if not os.path.isfile("price.txt"):
                            price_text = "item,price\n"
                        else:
                            f3 = open('price.txt','r')
                            price_text = f3.read()
                        encrypted_code = enduser_public_key.encrypt(price_text,24)
                        signature = cipher.sign(encrypted_code[0], factory_private_key)

                        print("Send Price Data to Enduser with Signature")
                        message = msg.generate_msg(encrypted_code[0],signature)
                        connection.sendall(message)

                    else:
                        print("Check Failed!")
                        connection.sendall("Code Error!")
                else:
                    break
                
        finally:
            # Clean up the connection
            connection.close()
Exemplo n.º 7
0
def send_server():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server_address = ('127.0.0.1', 10000)
    sock.bind(server_address)
    sock.listen(5)
    while True:
        connection, client_address = sock.accept()
        try:
            print 'connection from', client_address

            # Receive the data in small chunks and retransmit it
            while True:

                data = connection.recv(10000)
                if data:
                    print("Get Patch Request From Enduser.")

                    encrypt_auth_code = msg.get_package_of_msg(data)
                    signature_of_enduser = msg.get_Trail_of_msg(data)
                    
                    print("Verifying Signature")
                    if not cipher.verify(encrypt_auth_code, signature_of_enduser, enduser_public_key):
                        print("Verify Failed.")
                        return 

                    print("Decrypt Request.")
                    decrypted_code = factory_private_key.decrypt(encrypt_auth_code)
                    
                    print("Check Authentication Code.")
                    if decrypted_code == AUTH_CODE: 
                        print("Check Success!")

                        print("Encrypt Price Data.")
                        if not os.path.isfile("price.txt"):
                            price_text = "item,price\n"
                        else:
                            f3 = open('price.txt','r')
                            price_text = f3.read()
                        encrypted_code = enduser_public_key.encrypt(price_text,24)
                        signature = cipher.sign(encrypted_code[0], factory_private_key)

                        print("Send Price Data to Enduser with Signature")
                        message = msg.generate_msg(encrypted_code[0],signature)
                        connection.sendall(message)

                    else:
                        print("Check Failed!")
                        connection.sendall("Code Error!")
                else:
                    break
                
        finally:
            # Clean up the connection
            connection.close()
Exemplo n.º 8
0
def key_exchange_server():
    global factory_public_key_text
    global factory_public_key

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server_address = ('127.0.0.1', 20001)
    sock.bind(server_address)
    sock.listen(1)
    while True:
        # Wait for a connection
        # print >>sys.stderr, 'waiting for a connection'
        connection, client_address = sock.accept()
        try:

            print("Receive ID and N1 From Factory.")
            data = connection.recv(4096)
            data = msg.get_package_of_msg(data)
            message = enduser_private_key.decrypt(data)
            message = message.split("|")
            ID = message[0]
            N1 = int(message[1])
            N2 = N1 + 1

            print("To Get Public Key of Factory")
            factory_public_key_text = get_public_key(ID)
            factory_public_key = RSA.importKey(factory_public_key_text)

            response = "%d|%d" % (N1, N2)
            encrypted_code = factory_public_key.encrypt(response, 24)

            print("Send N1 and N2 to Factory")
            message = msg.generate_msg(encrypted_code[0])
            connection.sendall(message)

            print("Receive Finally Check")
            data = connection.recv(4096)
            data = msg.get_package_of_msg(data)

        finally:
            # Clean up the connection
            connection.close()
Exemplo n.º 9
0
def upload_listener():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('127.0.0.1', 20000)
    sock.bind(server_address)
    sock.listen(1)
    while True:
        connection, client_address = sock.accept()
        try:
            data = connection.recv(4096)
            message = msg.get_package_of_msg(data)
            print("%s" % message)

        finally:
            # Clean up the connection
            connection.close()
Exemplo n.º 10
0
def upload_listener():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('127.0.0.1', 20000)
    sock.bind(server_address)
    sock.listen(1)
    while True:
        connection, client_address = sock.accept()
        try:
            data = connection.recv(4096)
            message = msg.get_package_of_msg(data)
            print("%s" % message)
                
        finally:
            # Clean up the connection
            connection.close()
Exemplo n.º 11
0
def download_price_data():

    print("Connect to Factory.")
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = ('localhost', 10000)
    sock.connect(server_address)

    try:
        if not factory_public_key:
            print(
                "[Warning!]Factory Public Key Not Fonud, Please Exchange First."
            )
            return
        print("Encrypting Authentication Code.")
        encrypted_code = factory_public_key.encrypt(AUTH_CODE, 24)
        signature = cipher.sign(encrypted_code[0], enduser_private_key)

        # Send data
        print("Send Authentication Code with Signature.")
        message = msg.generate_msg(encrypted_code[0], signature)
        sock.sendall(message)

        # Get response
        print("Downloading New Price Data.")
        response = sock.recv(10000)

        print("Decrypting New Price Data")
        encrpted_priced = msg.get_package_of_msg(response)
        signature_of_factory = msg.get_Trail_of_msg(response)

        print("Verifying Signature")
        if not cipher.verify(encrpted_priced, signature_of_factory,
                             factory_public_key):
            print("Verify Failed.")
            return

        price = enduser_private_key.decrypt(encrpted_priced)

        print("New Pirce Saved.\n")
        f = open("end_user_price.txt", "w")
        f.write(price)
        f.close()

    finally:
        sock.close()
Exemplo n.º 12
0
def download_price_data():
    
    print("Connect to Factory.")
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = ('localhost', 10000)
    sock.connect(server_address)

    try:
        if not factory_public_key:
            print("[Warning!]Factory Public Key Not Fonud, Please Exchange First.")
            return
        print("Encrypting Authentication Code.")
        encrypted_code = factory_public_key.encrypt(AUTH_CODE,24)
        signature = cipher.sign(encrypted_code[0], enduser_private_key)

        # Send data
        print("Send Authentication Code with Signature.")
        message = msg.generate_msg(encrypted_code[0], signature)
        sock.sendall(message)

        # Get response
        print("Downloading New Price Data.")
        response = sock.recv(10000)

        print("Decrypting New Price Data")
        encrpted_priced = msg.get_package_of_msg(response)
        signature_of_factory = msg.get_Trail_of_msg(response)

        print("Verifying Signature")
        if not cipher.verify(encrpted_priced, signature_of_factory, factory_public_key):
            print("Verify Failed.")
            return 

        price = enduser_private_key.decrypt(encrpted_priced)

        print("New Pirce Saved.\n")
        f = open("end_user_price.txt","w")
        f.write(price)
        f.close()

    finally:
        sock.close()
Exemplo n.º 13
0
def init_session():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = ('localhost', 10001)
    print("connecting to enduser")
    sock.connect(server_address)

    try:
        ID = "enduser"
        N1 = random.randint(0, 100)

        print("Send ID and N1 to Factory")
        message_to_enduser = factory_public_key.encrypt(
            "%s|%d" % (ID, N1), 24)[0]
        message_to_enduser = msg.generate_msg(message_to_enduser)
        sock.sendall(message_to_enduser)

        print("Receive N1 and N2 from Factory")
        data = sock.recv(4096)
        data = msg.get_package_of_msg(data)
        check = enduser_private_key.decrypt(data)

        check = check.split("|")
        N2 = int(check[1])

        if N2 != (N1 + 1):
            print("Failed")
            # return

        print("Send Finally Check to Factory")
        message_to_enduser = factory_public_key.encrypt("%d" % (N2), 24)[0]
        message_to_enduser = msg.generate_msg(message_to_enduser)
        sock.sendall(message_to_enduser)

    finally:
        print >> sys.stderr, 'closing socket'
        sock.close()
Exemplo n.º 14
0
def init_session():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = ('localhost', 10001)
    print("connecting to enduser")
    sock.connect(server_address)

    try:
        ID = "enduser"
        N1 = random.randint(0,100)
        
        print("Send ID and N1 to Factory") 
        message_to_enduser = factory_public_key.encrypt("%s|%d" % (ID,N1),24)[0]
        message_to_enduser = msg.generate_msg(message_to_enduser)
        sock.sendall(message_to_enduser)

        print("Receive N1 and N2 from Factory")
        data = sock.recv(4096)
        data = msg.get_package_of_msg(data)
        check = enduser_private_key.decrypt(data)
        
        check = check.split("|")
        N2 = int(check[1])
        
        if N2 != (N1 + 1):
            print("Failed")
            # return 

        print("Send Finally Check to Factory")
        message_to_enduser = factory_public_key.encrypt("%d" % (N2),24)[0]
        message_to_enduser = msg.generate_msg(message_to_enduser)
        sock.sendall(message_to_enduser)

    finally:
        print >>sys.stderr, 'closing socket'
        sock.close()
Exemplo n.º 15
0
sock.listen(5)

while True:

    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()
    try:
        print sys.stderr, 'connection from', client_address

        # Receive the data in small chunks and retransmit it
        while True:
            request = connection.recv(4000)
            if request:
                print("Request Receive.")
                request = msg.get_package_of_msg(request)
                target = request.split("|")[0]

                print("Target: %s" % target)                
                if target not in ip_list:
                    print("Target not in List.")
                    connection.sendall("No such target in KDC!")
                    break

                print("Encrypting Public key of %s" % target)
                message = ("%s|%s" %(ip_list[target][2], request))
                signature = cipher.sign(message, kdc_private_key)

                print("Send Public Back with Signature")
                response = msg.generate_msg(message, signature)
                connection.sendall(response)