public static TLString EncryptMessage(TLObject decryptedMessage, TLEncryptedChatCommon chat) { var random = new Random(); var key = chat.Key.Data; var keyHash = Utils.ComputeSHA1(key); var keyFingerprint = new TLLong(BitConverter.ToInt64(keyHash, 12)); var decryptedBytes = decryptedMessage.ToBytes(); var bytes = Combine(BitConverter.GetBytes(decryptedBytes.Length), decryptedBytes); var sha1Hash = Utils.ComputeSHA1(bytes); var msgKey = sha1Hash.SubArray(sha1Hash.Length - 16, 16); var padding = (bytes.Length % 16 == 0) ? 0 : (16 - (bytes.Length % 16)); var paddingBytes = new byte[padding]; random.NextBytes(paddingBytes); var bytesWithPadding = Combine(bytes, paddingBytes); var x = 0; var sha1_a = Utils.ComputeSHA1(Combine(msgKey, key.SubArray(x, 32))); var sha1_b = Utils.ComputeSHA1(Combine(key.SubArray(32 + x, 16), msgKey, key.SubArray(48 + x, 16))); var sha1_c = Utils.ComputeSHA1(Combine(key.SubArray(64 + x, 32), msgKey)); var sha1_d = Utils.ComputeSHA1(Combine(msgKey, key.SubArray(96 + x, 32))); var aesKey = Combine(sha1_a.SubArray(0, 8), sha1_b.SubArray(8, 12), sha1_c.SubArray(4, 12)); var aesIV = Combine(sha1_a.SubArray(8, 12), sha1_b.SubArray(0, 8), sha1_c.SubArray(16, 4), sha1_d.SubArray(0, 8)); var encryptedBytes = Utils.AesIge(bytesWithPadding, aesKey, aesIV, true); var resultBytes = Combine(keyFingerprint.ToBytes(), msgKey, encryptedBytes); return(TLString.FromBigEndianData(resultBytes)); }
public static void ToStream(Stream input, TLObject obj, TLLong customFlags, int flag) { if (IsSet(customFlags, flag)) { obj.ToStream(input); } }
public static void ToStream(Stream input, TLObject obj, TLInt flags, int flag) { if (IsSet(flags, flag)) { obj.ToStream(input); } }
public override TLObject FromBytes(byte[] bytes, ref int position) { MessageId = GetObject <TLLong>(bytes, ref position); SeqNo = GetObject <TLInt>(bytes, ref position); MessageDataLength = GetObject <TLInt>(bytes, ref position); MessageData = GetObject <TLObject>(bytes, ref position); return(this); }
public override TLObject FromBytes(byte[] bytes, ref int position) { MessageId = GetObject <TLLong>(bytes, ref position); SeqNo = GetObject <TLInt>(bytes, ref position); MessageDataLength = GetObject <TLInt>(bytes, ref position); MessageData = GetObject <TLObject>(bytes, ref position); Debug.WriteLine(" <<{0, -28} MsgId {1} SeqNo {2, 4}", "containerMessage", MessageId, SeqNo); return(this); }
public override void Read(TLBinaryReader from) { AuthKeyId = from.ReadInt64(); MsgId = from.ReadInt64(); var length = from.ReadInt32(); var innerType = (TLType)from.ReadInt32(); Query = TLFactory.Read <TLObject>(from, innerType); //Query = TLFactory.Read<TLObject>(from, (TLType)from.ReadInt32()); }
public static void ToStream(Stream output, TLObject obj, TLLong customFlags, int flag) { if (IsSet(customFlags, flag)) { if (obj == null) { } obj.ToStream(output); } }
public void WriteObject(TLObject obj) { if (obj != null) { obj.Write(this); } else { // TLNull Write(0x56730BCC); } }
public static TLResponse Parse(byte[] bytes, byte[] authKey) { TLUtils.WriteLine("-------------------"); TLUtils.WriteLine("--Parse response --"); TLUtils.WriteLine("-------------------"); int position = 0; var response = new TLResponse(); response.AuthKeyId = bytes.SubArray(0, 8); TLUtils.WriteLine("AuthKeyId: " + BitConverter.ToString(response.AuthKeyId)); response.MessageKey = bytes.SubArray(8, 16); TLUtils.WriteLine("MessageKey: " + BitConverter.ToString(response.MessageKey)); response.EncryptedData = bytes.SubArray(24, bytes.Length - 24); //TLUtils.WriteLine("Encrypted data: " + BitConverter.ToString(response.Data)); var keyIV = Utils.GetDecryptKeyIV(authKey, response.MessageKey); response.DecryptedData = Utils.AesIge(response.EncryptedData, keyIV.Item1, keyIV.Item2, false); //TLUtils.WriteLine("Decrypted data: " + BitConverter.ToString(response.DecryptedData)); response.Salt = response.DecryptedData.SubArray(0, 8); TLUtils.WriteLine("Salt: " + BitConverter.ToString(response.Salt)); response.SessionId = response.DecryptedData.SubArray(8, 8); TLUtils.WriteLine("SessionId: " + BitConverter.ToString(response.SessionId)); position = 0; response.MessageId = TLObject.GetObject <TLLong>(response.DecryptedData.SubArray(16, 8), ref position); TLUtils.WriteLine("<-MESSAGEID: " + TLUtils.MessageIdString(response.MessageId)); position = 0; response.SequenceNumber = TLObject.GetObject <TLInt>(response.DecryptedData.SubArray(24, 4), ref position); TLUtils.WriteLine(" SEQUENCENUMBER: " + response.SequenceNumber); response.MessageLength = BitConverter.ToInt32(response.DecryptedData.SubArray(28, 4), 0); TLUtils.WriteLine("MessageLength: " + response.MessageLength); response.MessageData = response.DecryptedData.SubArray(32, response.MessageLength); TLUtils.WriteLine("MessageData: " + BitConverter.ToString(response.MessageData)); position = 0; response.Data = TLObject.GetObject <TLObject>(response.MessageData, ref position); return(response); }
public static T OpenObjectFromMTProtoFile <T>(object syncRoot, string fileName) where T : TLObject { try { lock (syncRoot) { using (var fileStream = FileUtils.GetLocalFileStreamForRead(fileName)) { if (fileStream.Length > 0) { return(TLObject.GetObject <T>(fileStream)); } } } } catch (Exception e) { WriteLine("MTPROTO FILE ERROR: cannot read " + typeof(T) + " from file " + fileName, LogSeverity.Error); WriteException(e); } return(default(T)); }
public override void Read(TLBinaryReader from) { SendBefore = from.ReadInt32(); Action = TLFactory.Read <TLObject>(from); }
public static bool IsValidAction(TLObject obj) { var readHistoryAction = obj as TLMessagesReadHistory; if (readHistoryAction != null) { return(true); } var sendMessageAction = obj as TLMessagesSendMessage; if (sendMessageAction != null) { return(true); } var sendMediaAction = obj as TLMessagesSendMedia; if (sendMediaAction != null) { var mediaContact = sendMediaAction.Media as TLInputMediaContact; if (mediaContact != null) { return(true); } var mediaGeoPoint = sendMediaAction.Media as TLInputMediaGeoPoint; if (mediaGeoPoint != null) { return(true); } var mediaVenue = sendMediaAction.Media as TLInputMediaVenue; if (mediaVenue != null) { return(true); } } var forwardMessagesAction = obj as TLMessagesForwardMessages; if (forwardMessagesAction != null) { return(true); } var forwardMessageAction = obj as TLMessagesForwardMessage; if (forwardMessageAction != null) { return(true); } var startBotAction = obj as TLMessagesStartBot; if (startBotAction != null) { return(true); } var sendEncryptedAction = obj as TLMessagesSendEncrypted; if (sendEncryptedAction != null) { return(true); } var sendEncryptedFileAction = obj as TLMessagesSendEncryptedFile; if (sendEncryptedFileAction != null) { return(true); } var sendEncryptedServiceAction = obj as TLMessagesSendEncryptedService; if (sendEncryptedServiceAction != null) { return(true); } var readEncryptedHistoryAction = obj as TLMessagesReadEncryptedHistory; if (readEncryptedHistoryAction != null) { return(true); } return(false); }
public static TLDecryptedMessageBase DecryptMessage(TLString data, TLEncryptedChat chat, out bool commitChat) { commitChat = false; var bytes = data.Data; var keyFingerprint = BitConverter.ToInt64(bytes, 0); var msgKey = bytes.SubArray(8, 16); var key = chat.Key.Data; var keyHash = Utils.ComputeSHA1(key); var calculatedKeyFingerprint = BitConverter.ToInt64(keyHash, keyHash.Length - 8); if (keyFingerprint != calculatedKeyFingerprint) { var chat20 = chat as TLEncryptedChat20; if (chat20 != null && chat20.PFS_Key != null) { var pfsKeyHash = Utils.ComputeSHA1(chat20.PFS_Key.Data); var pfsKeyFingerprint = BitConverter.ToInt64(pfsKeyHash, pfsKeyHash.Length - 8); if (pfsKeyFingerprint == keyFingerprint) { chat20.Key = chat20.PFS_Key; chat20.PFS_Key = null; chat20.PFS_KeyFingerprint = null; chat20.PFS_A = null; chat20.PFS_ExchangeId = null; commitChat = true; } else { return(null); } } else { return(null); } } var x = 0; var sha1_a = Utils.ComputeSHA1(Combine(msgKey, key.SubArray(x, 32))); var sha1_b = Utils.ComputeSHA1(Combine(key.SubArray(32 + x, 16), msgKey, key.SubArray(48 + x, 16))); var sha1_c = Utils.ComputeSHA1(Combine(key.SubArray(64 + x, 32), msgKey)); var sha1_d = Utils.ComputeSHA1(Combine(msgKey, key.SubArray(96 + x, 32))); var aesKey = Combine(sha1_a.SubArray(0, 8), sha1_b.SubArray(8, 12), sha1_c.SubArray(4, 12)); var aesIV = Combine(sha1_a.SubArray(8, 12), sha1_b.SubArray(0, 8), sha1_c.SubArray(16, 4), sha1_d.SubArray(0, 8)); var encryptedBytes = bytes.SubArray(24, bytes.Length - 24); var decryptedBytes = Utils.AesIge(encryptedBytes, aesKey, aesIV, false); var length = BitConverter.ToInt32(decryptedBytes, 0); if (length <= 0 || (4 + length) > decryptedBytes.Length) { return(null); } var calculatedMsgKey = Utils.ComputeSHA1(decryptedBytes.SubArray(0, 4 + length)); for (var i = 0; i < 16; i++) { if (msgKey[i] != calculatedMsgKey[i + 4]) { return(null); } } var position = 4; var decryptedObject = TLObject.GetObject <TLObject>(decryptedBytes, ref position); var decryptedMessageLayer = decryptedObject as TLDecryptedMessageLayer; var decryptedMessageLayer17 = decryptedObject as TLDecryptedMessageLayer17; TLDecryptedMessageBase decryptedMessage = null; if (decryptedMessageLayer17 != null) { decryptedMessage = decryptedMessageLayer17.Message; var decryptedMessage17 = decryptedMessage as ISeqNo; if (decryptedMessage17 != null) { decryptedMessage17.InSeqNo = decryptedMessageLayer17.InSeqNo; decryptedMessage17.OutSeqNo = decryptedMessageLayer17.OutSeqNo; } } else if (decryptedMessageLayer != null) { decryptedMessage = decryptedMessageLayer.Message; } else if (decryptedObject is TLDecryptedMessageBase) { decryptedMessage = (TLDecryptedMessageBase)decryptedObject; } return(decryptedMessage); }
public static byte[] ToBytes(TLObject obj, TLInt flags, int flag) { return(obj != null && IsSet(flags, flag) ? obj.ToBytes() : new byte[] {}); }