private static BaseScertMessage Instantiate(Type classType, RT_MSG_TYPE id, byte[] plain) { BaseScertMessage msg = null; // using (MemoryStream stream = new MemoryStream(plain)) { using (BinaryReader reader = new BinaryReader(stream)) { if (classType == null) { msg = new RawScertMessage(id); } else { msg = (BaseScertMessage)Activator.CreateInstance(classType); } try { msg.Deserialize(reader); } catch (Exception e) { Console.WriteLine($"Error deserializing {id} {BitConverter.ToString(plain)}"); Console.WriteLine(e); } } } return(msg); }
public static BaseScertMessage Instantiate(RT_MSG_TYPE id, byte[] hash, byte[] messageBuffer, Func <RT_MSG_TYPE, CipherContext, ICipher> getCipherCallback = null) { // Init first Initialize(); BaseScertMessage msg = null; // Get class if (!_messageClassById.TryGetValue(id, out var classType)) { classType = null; } // Decrypt if (hash != null) { CipherContext context = (CipherContext)(hash[3] >> 5); var cipher = getCipherCallback(id, context); if (cipher == null) { return(null); } if (cipher.Decrypt(messageBuffer, hash, out var plain)) { msg = Instantiate(classType, id, plain); } // This is a hack to make the dme server connect // We don't really care what their key is since we're not encrypting our response else if (id == RT_MSG_TYPE.RT_MSG_CLIENT_CRYPTKEY_PUBLIC) { msg = Instantiate(classType, id, plain); } else { Logger.Error($"Unable to decrypt {id}, HASH:{BitConverter.ToString(hash)} DATA:{BitConverter.ToString(messageBuffer)} CIPHER:{cipher}"); } } else { msg = Instantiate(classType, id, messageBuffer); } return(msg); }