public static void DecryptPacket(ServerState state, byte[] packet) { using (var reader = new Reader(packet)) { var ID = reader.ReadUInt16(); reader.Seek(3, SeekOrigin.Current); var Version = reader.ReadUInt16(); byte[] cipherText = reader.ReadAllBytes, plainText; var Name = Packet_Names.GetName(ID); switch (ID) { case 10100: { plainText = cipherText; break; } case 10101: { state.ClientKey = cipherText.Take(32).ToArray(); var nonce = GenericHash.Hash(state.ClientKey.Concat(state.ServerKey.PublicKey).ToArray(), null, 24); cipherText = cipherText.Skip(32).ToArray(); plainText = PublicKeyBox.Open(cipherText, nonce, state.ServerKey.PrivateKey, state.ClientKey); state.SessionKey = plainText.Take(24).ToArray(); state.ClientState.Nonce = plainText.Skip(24).Take(24).ToArray(); plainText = plainText.Skip(24).Skip(24).ToArray(); break; } default: { state.ClientState.Nonce = Utilities.Increment(Utilities.Increment(state.ClientState.Nonce)); plainText = SecretBox.Open(new byte[16].Concat(cipherText).ToArray(), state.ClientState.Nonce, state.SharedKey); break; } } ClientCrypto.EncryptPacket(state.ClientState, ID, Version, plainText); Console.WriteLine( $"[{DateTime.Now.ToLongTimeString()}, CLIENT, {ID}] {Resources.Definition.Decode(new Reader(plainText), ID)}"); Logger.Write(BitConverter.ToString(plainText).Replace("-", string.Empty), $"{ID}_{Name}", LogType.PACKET); } }
public string Decode(Reader reader, ushort ID) { if (this.ContainsKey(ID)) { try { Type _Type = Activator.CreateInstance(this[ID], reader, ID) as Type; _Type.Decode(); return(_Type.Json.Count == 0 ? string.Empty : JsonConvert.SerializeObject(_Type.Json, Formatting.Indented)); } catch (Exception) { return($"Failed to decode {ID}."); } } else { return($"Missing definition for Packet {ID} [{Packet_Names.GetName(ID)}]"); } }