Guest User

Untitled

a guest
Mar 25th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. #include "Client.h"
  2. #include "C:\cryptopp563\hex.h"
  3. #include "C:\cryptopp563\md5.h"
  4. CryptoDevice cryptoDevice;
  5.  
  6. int process_client(client_type &new_client)
  7. {
  8. while (1)
  9. {
  10. memset(new_client.received_message, 0, DEFAULT_BUFLEN);
  11.  
  12. if (new_client.socket != 0)
  13. {
  14.  
  15.  
  16. int iResult = recv(new_client.socket, new_client.received_message, DEFAULT_BUFLEN, 0);
  17.  
  18. string strMessage(new_client.received_message);
  19.  
  20. // some logic, we dont want to decrypt notifications sent by the operator
  21. // our protocol says - ": " means notification from the operator
  22. size_t position = strMessage.find(": ") + 2;
  23. string prefix = strMessage.substr(0, position);
  24. string postfix = strMessage.substr(position);
  25. string decrypted_message;
  26.  
  27. //this is the only notification we use right now :(
  28. if (postfix != "Disconnected")
  29. {
  30. postfix = cryptoDevice.decryptAES(postfix);
  31. decrypted_message = postfix;
  32. }
  33. else
  34. //dont decrypt this - not classified and has not been ecrypted!
  35. //trying to do so may cause errors
  36. decrypted_message = postfix;
  37.  
  38. if (iResult != SOCKET_ERROR)
  39. cout << prefix + postfix << endl;
  40. else
  41. {
  42. cout << "recv() failed: " << WSAGetLastError() << endl;
  43. break;
  44. }
  45. }
  46. }
  47.  
  48. if (WSAGetLastError() == WSAECONNRESET)
  49. cout << "The server has disconnected" << endl;
  50.  
  51. return 0;
  52. }
  53.  
  54.  
  55.  
  56. int main()
  57. {
  58. WSAData wsa_data;
  59. struct addrinfo *result = NULL, *ptr = NULL, hints;
  60. string sent_message = "";
  61. client_type client = { INVALID_SOCKET, -1, "" };
  62. int iResult = 0;
  63. string message;
  64. std::string username, password, salt, output;
  65. int counter = 0;
  66.  
  67. cout << "Starting Client...\n";
  68.  
  69. // Initialize Winsock
  70. iResult = WSAStartup(MAKEWORD(2, 2), &wsa_data);
  71. if (iResult != 0) {
  72. cout << "WSAStartup() failed with error: " << iResult << endl;
  73. return 1;
  74. }
  75.  
  76. ZeroMemory(&hints, sizeof(hints));
  77. hints.ai_family = AF_UNSPEC;
  78. hints.ai_socktype = SOCK_STREAM;
  79. hints.ai_protocol = IPPROTO_TCP;
  80.  
  81. cout << "Connecting...\n";
  82.  
  83. // Resolve the server address and port
  84. iResult = getaddrinfo(IP_ADDRESS, DEFAULT_PORT, &hints, &result);
  85. if (iResult != 0) {
  86. cout << "getaddrinfo() failed with error: " << iResult << endl;
  87. WSACleanup();
  88. system("pause");
  89. return 1;
  90. }
  91.  
  92. // Attempt to connect to an address until one succeeds
  93. for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
  94.  
  95. // Create a SOCKET for connecting to server
  96. client.socket = socket(ptr->ai_family, ptr->ai_socktype,
  97. ptr->ai_protocol);
  98. if (client.socket == INVALID_SOCKET) {
  99. cout << "socket() failed with error: " << WSAGetLastError() << endl;
  100. WSACleanup();
  101. system("pause");
  102. return 1;
  103. }
  104.  
  105. // Connect to server.
  106. iResult = connect(client.socket, ptr->ai_addr, (int)ptr->ai_addrlen);
  107. if (iResult == SOCKET_ERROR) {
  108. closesocket(client.socket);
  109. client.socket = INVALID_SOCKET;
  110. continue;
  111. }
  112. break;
  113. }
  114.  
  115. freeaddrinfo(result);
  116.  
  117. if (client.socket == INVALID_SOCKET) {
  118. cout << "Unable to connect to server!" << endl;
  119. WSACleanup();
  120. system("pause");
  121. return 1;
  122. }
  123.  
  124. cout << "Successfully Connected" << endl;
  125.  
  126. //Obtain id from server for this client;
  127. recv(client.socket, client.received_message, DEFAULT_BUFLEN, 0);
  128. message = client.received_message;
  129.  
  130. if (message != "Server is full")
  131. {
  132. client.id = atoi(client.received_message);
  133.  
  134. thread my_thread(process_client, client);
  135.  
  136. do
  137. {
  138. counter++;
  139. CryptoPP::MD5 hash;
  140. byte digest[CryptoPP::MD5::DIGESTSIZE];
  141. std::cout << "Enter username: ";
  142. std::getline(std::cin, username);
  143. std::cout << std::endl << "Enter password: ";
  144. std::getline(std::cin, password);
  145. salt = username + password;
  146.  
  147. hash.CalculateDigest(digest, (byte*)salt.c_str(), salt.length());
  148.  
  149.  
  150. CryptoPP::HexEncoder encoder;
  151. CryptoPP::StringSink *SS = new CryptoPP::StringSink(output);
  152. encoder.Attach(SS);
  153. encoder.Put(digest, sizeof(digest));
  154. encoder.MessageEnd();
  155. }
  156. while (output != "9E5867F73B784139F191E4643A398945" && counter < 5);
  157. std::cout << "The username/password salted hash is => " << output << std::endl;
  158. if (output == "9E5867F73B784139F191E4643A398945")//username: amit, password: youarewelcome
  159. {
  160. cout << "Welcome agent!" << endl;
  161. while (1)
  162. {
  163.  
  164. getline(cin, sent_message);
  165. sent_message = cryptoDevice.encryptAES(sent_message);
  166. string cipher = sent_message;
  167.  
  168. iResult = send(client.socket, cipher.c_str(), cipher.length(), 0);
  169.  
  170. if (iResult <= 0)
  171. {
  172. cout << "send() failed: " << WSAGetLastError() << endl;
  173. break;
  174. }
  175. }
  176.  
  177. }
  178. else
  179. {
  180. cout << "Wrong password or username!" << endl;
  181. }
  182. //Shutdown the connection since no more data will be sent
  183. my_thread.detach();
  184. }
  185. else cout << client.received_message << endl;
  186.  
  187. cout << "Shutting down socket..." << endl;
  188. iResult = shutdown(client.socket, SD_SEND);
  189. if (iResult == SOCKET_ERROR)
  190. {
  191. cout << "shutdown() failed with error: " << WSAGetLastError() << endl;
  192. closesocket(client.socket);
  193. WSACleanup();
  194. system("pause");
  195. return 1;
  196. }
  197.  
  198. closesocket(client.socket);
  199. WSACleanup();
  200. system("pause");
  201. return 0;
  202. }
Add Comment
Please, Sign In to add comment