public override async Task <bool> SendMessage(string message) { try { logger.info("send message with key: {0}", BitConverter.ToString(key).Replace("-", "").ToLower()); long messageId = Helpers.GenerateRandomLong(); DecryptedMessage msg = TL.decryptedMessage(messageId, Helpers.GenerateRandomBytes(128), message, TL.decryptedMessageMediaEmpty()); byte[] data; using (MemoryStream memory = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(memory)) { msg.Write(writer); data = memory.ToArray(); } } using (MemoryStream memory = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(memory)) { writer.Write(data.Length); writer.Write(data); data = memory.ToArray(); } } byte[] msgKey = Helpers.CalcMsgKey(data); AESKeyData aesKey = Helpers.CalcKey(key, msgKey, true); data = AES.EncryptAES(aesKey, data); using (MemoryStream memory = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(memory)) { writer.Write(fingerprint); writer.Write(msgKey); writer.Write(data); data = memory.ToArray(); } } Messages_sentEncryptedMessageConstructor sent = (Messages_sentEncryptedMessageConstructor)await session.Api.messages_sendEncrypted(InputEncryptedChat, messageId, data); MessageModel messageModel = new MessageModelEncryptedDelivered(TelegramSession.Instance.SelfId, OpponentId, sent.date, true, true, msg, TL.encryptedFileEmpty()); messages.Add(messageModel); TelegramSession.Instance.Dialogs.Model.UpDialog(this); return(true); } catch (Exception e) { logger.error("send encrypted message exception: {0}", e); return(false); } }