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
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
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()