/// <summary> /// recursive method that recieves a message from the server and handles it according to the request or response number /// </summary> /// <param name="ar"></param> public void ReceiveMessage(IAsyncResult ar) { int bytesRead; try { lock (client.GetStream()) { // call EndRead to handle the end of an async read. bytesRead = client.GetStream().EndRead(ar); } byte[] arrived = new byte[bytesRead]; Array.Copy(data, arrived, bytesRead); string messageReceived = aes.DecryptStringFromBytes(arrived, aes.GetKey(), aes.GetIV()); string[] arrayReceived = messageReceived.Split('#'); int requestNumber = Convert.ToInt32(arrayReceived[0]); string details = arrayReceived[1]; if (requestNumber == logResponse) { ReceivingLog(details); } else if (requestNumber == noLogResponse) { // shows message that indicates the absence of a file from the requested date MessageBox.Show("There were no packets from the selected date\nPlease select a different date", "CAPCKET"); } lock (client.GetStream()) { // continue reading from the client client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(client.ReceiveBufferSize), ReceiveMessage, null); } } catch (Exception ex) { Console.WriteLine("catch recieve\n" + ex.ToString()); } }
/// <summary> /// recursive method that recieves a message from the server and handles it according to the request or response number /// </summary> /// <param name="ar"></param> private void ReceiveMessage(IAsyncResult ar) { try { int bytesRead; lock (client.GetStream()) { // call EndRead to handle the end of an async read and read the data from the server bytesRead = client.GetStream().EndRead(ar); } if (rsa.ServerPublicKey == null) // if RSA object is missing server's public key { rsa.SetServerPublicKey(ByteConverter.GetString(data).Split('#')[1]); // creates an Aes instance for symmetric encryption and sends the key to the server aes = new AesCrypto(); byte[] bytesArray = AesKeyAndIVBytesToSend(aes.GetKey(), aes.GetIV()); SendRsaEncryptedMessage(bytesArray); } else { byte[] arrived = new byte[bytesRead]; Array.Copy(data, arrived, bytesRead); string messageReceived; if (aes == null) // if AES object wasn't initialize yet { messageReceived = ByteConverter.GetString(arrived); } else { messageReceived = aes.DecryptStringFromBytes(arrived, aes.GetKey(), aes.GetIV()); } Debug.WriteLine("received: " + messageReceived); string[] arrayReceived = messageReceived.Split('#'); int requestNumber = Convert.ToInt32(arrayReceived[0]); string text = arrayReceived[1]; if (requestNumber == registerStatusResponse) { if (text.Equals("ok")) { name = textBoxName.Text; OpenSnifferForm(); return; } else if (text.Equals("not ok")) { MessageBox.Show("Wrong username or password\nPlease try again", "CAPCKET login error"); } } else if (requestNumber == EmailRequest) { string answer = CreateInteractionForm("Please enter the code that was sent to your email address:", "CAPCKET email verification"); string textToSend = text + "/" + answer; SendAesEncryptedMessage(CodeResponse + "#" + textToSend + "#" + textToSend.Length); } else if (requestNumber == QuestionResponse) { string[] textArray = text.Split('/'); if (textArray.Length > 1) { MessageBox.Show("Wrong answer\nPlease try again", "CAPCKET login error"); } string answer = CreateInteractionForm(textArray[0], "CAPCKET changing password"); SendAesEncryptedMessage(AnswerResponse + "#" + answer + "#" + answer.Length); } else if (requestNumber == PasswordRequest) { string password = CreateInteractionForm("Please enter a new password:"******"CAPCKET changing password"); if (!IsPasswordValid(password)) //checking password validity { //password isn't valid. MessageBox.Show("The pasword isn't valid. Please try again.\nIt should be 6-8 charcters and contain both digits and letters.", "CAPCKET changing password"); } else { // hash password string hashpassword = HashString(password); Debug.WriteLine("hash length: " + hashpassword.Length); SendAesEncryptedMessage(PasswordResponse + "#" + hashpassword + "#" + password.Length); } } else if (requestNumber == PasswordChangeStatusResponse) { if (text.Equals("ok")) { MessageBox.Show("Password changed successfully", "CAPCKET login · changing password"); } else { string password = CreateInteractionForm("Please try again and enter a new password:"******"CAPCKET login error · changing password"); SendAesEncryptedMessage(PasswordResponse + "#" + password + "#" + password.Length); } } else if (requestNumber == RSAPublicKeyTransfer) { rsa.SetServerPublicKey(text); } } // continue reading client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(client.ReceiveBufferSize), ReceiveMessage, null); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }