private void SetEncryption(ExtendedSetEncryptionMessage message)
        {
            byte[] nonce       = message.RemoveNonce();
            int    nonceMethod = message.GetNonceMethod();

            if (nonceMethod == 1)
            {
                this.ScrambleNonceUsingMersenneTwister(nonce, nonce.Length);
            }
            else
            {
                this.ScrambleNonceUsingDefaultMethod(nonce, nonce.Length);
            }

            char[] nonceChars = new char[nonce.Length];

            for (int i = 0; i < nonce.Length; i++)
            {
                nonceChars[i] = (char)nonce[i];
            }

            this.InitRC4Encryption(new string(nonceChars));
            this.m_encryptionScrambled = true;
        }
        private int OnReceive(byte[] buffer, int length)
        {
            if (length >= Messaging.PACKET_HEADER_LENGTH)
            {
                Messaging.ReadHeader(buffer, out int messageType, out int messageLength, out int messageVersion);

                if (length >= Messaging.PACKET_HEADER_LENGTH + messageLength)
                {
                    byte[] encryptedBytes = new byte[messageLength];
                    byte[] encodingBytes;

                    Buffer.BlockCopy(buffer, Messaging.PACKET_HEADER_LENGTH, encryptedBytes, 0, messageLength);

                    int encodingLength;

                    if (this.m_receiveEncrypter != null)
                    {
                        encodingLength = messageLength - this.m_receiveEncrypter.GetOverheadEncryption();
                        encodingBytes  = new byte[encodingLength];

                        this.m_receiveEncrypter.Decrypt(encryptedBytes, encodingBytes, encodingLength);
                    }
                    else
                    {
                        encodingLength = messageLength;
                        encodingBytes  = encryptedBytes;
                    }

                    PiranhaMessage piranhaMessage = this.m_messageFactory.CreateMessageByType(messageType);

                    if (piranhaMessage != null)
                    {
                        piranhaMessage.SetMessageVersion(messageVersion);
                        piranhaMessage.GetByteStream().SetByteArray(encodingBytes, encodingLength);
                        piranhaMessage.Decode();

                        if (piranhaMessage.GetMessageType() != ExtendedSetEncryptionMessage.MESSAGE_TYPE)
                        {
                            this.m_receiveQueue.Enqueue(piranhaMessage);
                        }
                        else
                        {
                            ExtendedSetEncryptionMessage extendedSetEncryptionMessage = (ExtendedSetEncryptionMessage)piranhaMessage;
                            byte[] nonce = extendedSetEncryptionMessage.RemoveNonce();

                            switch (extendedSetEncryptionMessage.GetNonceMethod())
                            {
                            case 1:
                                this.ScrambleNonceUsingMersenneTwister(nonce);
                                break;

                            default:
                                this.ScrambleNonceUsingDefaultMethod(nonce);
                                break;
                            }

                            char[] nonceChars = new char[nonce.Length];
                            for (int i = 0; i < nonce.Length; i++)
                            {
                                nonceChars[i] = (char)nonce[i];
                            }
                            this.InitEncrypters(new string(nonceChars));
                        }
                    }
                    else
                    {
                        Debugger.Warning(string.Format("Messaging.onReceive: ignoring message of unknown type {0}", messageType));
                    }

                    return(Messaging.PACKET_HEADER_LENGTH + messageLength);
                }
            }

            return(0);
        }