public static void EncryptPacket(Socket socket, ServerState state, int messageId, int unknown, byte[] plainText) { byte[] cipherText; if (messageId == 20100 || (messageId == 20103 && state.sharedKey == null)) { cipherText = plainText; } else if (messageId == 20103 || messageId == 20104) { byte[] nonce = GenericHash.Hash(state.clientState.nonce.Concat(state.clientKey).Concat(state.serverKey.PublicKey).ToArray(), null, 24); plainText = state.nonce.Concat(state.sharedKey).Concat(plainText).ToArray(); cipherText = PublicKeyBox.Create(plainText, nonce, state.serverKey.PrivateKey, state.clientKey); } else { // nonce was already incremented in ClientCrypto.DecryptPacket cipherText = SecretBox.Create(plainText, state.nonce, state.sharedKey).Skip(16).ToArray(); } byte[] packet = BitConverter.GetBytes(messageId).Reverse().Skip(2).Concat(BitConverter.GetBytes(cipherText.Length).Reverse().Skip(1)).Concat(BitConverter.GetBytes(unknown).Reverse().Skip(2)).Concat(cipherText).ToArray(); socket.BeginSend(packet, 0, packet.Length, 0, new AsyncCallback(SendCallback), state); }
public Client(ServerState serverstate) { this.state.serverState = serverstate; this.state.clientKey = this.clientKey; this.state.serverKey = ClientCrypto.serverKey; }