Advertisement
Guest User

hh

a guest
Sep 15th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. from watson_developer_cloud import TextToSpeechV1
  2. import BridgeKeys as bridge
  3. import os
  4. import sys
  5. import socket
  6. import hashlib
  7. from cryptography.fernet import Fernet
  8.  
  9. if __name__ == '__main__':
  10.  
  11. if len(sys.argv) != 11:
  12. print("Not Enough Arguments")
  13. server_port = 0
  14. svr_ip = ""
  15. brg_port = 0
  16. backlog_size = 0
  17. socket_size = 0
  18. for i in [1, 3, 5, 7, 9]:
  19. if sys.argv[i] == "-svr-p":
  20. server_port = int(sys.argv[i+1])
  21. elif sys.argv[i] == "-svr":
  22. srv_ip = sys.argv[i+1]
  23. elif sys.argv[i] == "-p":
  24. brg_port = int(sys.argv[i+1])
  25. elif sys.argv[i] == "-b":
  26. backlog_size = int(sys.argv[i+1])
  27. elif sys.argv[i] == "-z":
  28. socket_size = int(sys.argv[i+1])
  29. else:
  30. print("Unrecognized Parameter")
  31.  
  32. # recieve question
  33. s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  34. s.bind(('localhost', brg_port))
  35. s.listen(backlog_size)
  36.  
  37. while 1:
  38. client, addr = s.accept()
  39. question = client.recv(socket_size)
  40. if question:
  41. plain_text = ""
  42. # decrypt
  43. key,cipher_text,hash_text_recv = cPickle.loads(question)
  44. md5_hash_tool0= hashlib.md5()
  45. md5_hash_tool0.update(cipher_text)
  46. hash_text_generate = md5_hash_tool0.hexdigest()
  47. # compare hash
  48. if hash_text_generate == has_text_recv:
  49. cipher_tool0 = Fernet(key)
  50. plain_text = cipher_tool0.decrypt(cipher_text)
  51. print(plain_text)
  52. else:
  53. print("Data Incomplete")
  54.  
  55. # get answer
  56. t2s = TextToSpeechV1(username=bridge.watson_username, password=bridge.watson_password,url=bridge.watson_url)
  57.  
  58. with open('output.wav','wb') as audio_file:
  59. response =t2s.synthesize(plain_text, accept='audio/wav',voice="en-US_AllisonVoice").get_result()
  60. audio_file.write(response.content)
  61.  
  62. # test
  63. os.system("start output.wav")
  64.  
  65. # encrypt
  66.  
  67. # send back
  68.  
  69.  
  70. client.close()
  71.  
  72.  
  73.  
  74.  
  75.  
  76. '''
  77. # Generate Fernet key
  78. key = Fernet.generate_key()
  79. print(key)
  80. cipher_tool = Fernet(key)
  81. # Encrypt plain text
  82. cipher_text = cipher_tool.encrypt(b"How old is Virginia Tech?")
  83. print(cipher_text)
  84.  
  85. # Decrypt cipher text
  86. plain_text = cipher_tool.decrypt(cipher_text)
  87. print(plain_text)
  88.  
  89. # MD5 Hash Encrypted text
  90. md5_hash_tool = hashlib.md5()
  91. md5_hash_tool.update(cipher_text)
  92. hash_text = md5_hash_tool.hexdigest()
  93. print(
  94. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement