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()
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()
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()
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) else: print >>sys.stderr, 'no more data from', client_address break finally: # Clean up the connection connection.close()