private void NotificationHandler(string notificationInJson) { Notification notification = JsonSerializer.Deserialize <Notification>(notificationInJson); aesEncryption.SetKey(chatKey); string decryptedMessage = coding.Decode(aesEncryption.Decrypt(notification.EncryptedMessage)); Console.WriteLine($"{notification.Sender}: {decryptedMessage}"); }
private void KeyExchange() { while (true) { // send to the client server public key [and other credentials] tcpClient.Send(server.rsa.ExportRSAPublicKey()); // get key for symmetric encryption from client byte[] encryptedMessageWithKey = tcpClient.GetMessage(); try { byte[] messageWithKeyBytes = server.rsa.Decrypt(encryptedMessageWithKey, false); string messageWithKeyInJson = coding.Decode(messageWithKeyBytes); AesKeyExchangeRequest aesKeyExchangeRequest = JsonSerializer.Deserialize <AesKeyExchangeRequest>(messageWithKeyInJson); aesEncryption.SetKey(aesKeyExchangeRequest.Key); aesEncryption.SetIV(aesKeyExchangeRequest.IV); ClientAesKey = aesEncryption.GetKey(); AesKeyExchangeResponse response = new AesKeyExchangeResponse { Code = StatusCode.Ok, RequestId = aesKeyExchangeRequest.Id }; SendMessageAesEncrypted(response, ClientAesKey); return; } catch (Exception exception) { Console.WriteLine(exception.Message); } } }