private async void HandleIncomingPublicMessageAsync(string json) { var packet = await JsonUtils.DeserializeAsync <PublicMessagePacket>(json); if (packet == null) { return; } var chatRoom = _publicRooms.FirstOrDefault(r => r.Equals(packet.ChatRoom)); if (chatRoom == null) { return; } var decrypted = CryptUtils.AesDecryptBytes(packet.Content, chatRoom.PreSharedKey); var signedJson = Config.DefaultEncoding.GetString(decrypted); var message = await GetAndVerifySignedDataAsync <Message>(signedJson); if (message?.Sender == null) { return; } OnKeepAliveReceived(message.Sender); chatRoom.OnPublicMessageReceived(new MessageReceivedEventArgs(message.Sender, message)); }
public bool Equals(byte[] chatRoomIdentifier) { var decrypted = CryptUtils.AesDecryptBytes(chatRoomIdentifier, PreSharedKey); if (decrypted == null) { return(false); } var encoded = Config.DefaultEncoding.GetString(decrypted); return(encoded == Name); }
private async void HandleIncomingInviteAsync(string json) { var packet = await JsonUtils.DeserializeAsync <InvitePacket>(json); if (packet?.Content == null || packet.AesKey == null) { return; } Invite invite; try { var aesKey = CryptUtils.RsaDecryptWithPrivate(packet.AesKey, 0, packet.AesKey.Length, _personalIdentity.KeyPair.Private); var decrypted = CryptUtils.AesDecryptBytes(packet.Content, aesKey); var signedDataJson = Config.DefaultEncoding.GetString(decrypted); invite = await GetAndVerifySignedDataAsync <Invite>(signedDataJson); if (invite?.Name == null || invite.PreSharedKey == null || invite.Sender?.Name == null || invite.Sender.PublicKey == null) { return; } } catch (CryptographicException) { return; } catch (InvalidCipherTextException) { return; } OnKeepAliveReceived(invite.Sender); OnInviteReceived(invite.Name, invite.PreSharedKey, invite.Sender); }