BF2Statistics.Gamespy.Enctypex.Encode C# (CSharp) Method

Encode() public static method

public static Encode ( byte key, byte validate, byte data, long size ) : byte[]
key byte
validate byte
data byte
size long
return byte[]
        public static byte[] Encode(byte[] key, byte[] validate, byte[] data, long size)
        {
            byte[] numArray = new byte[size + 23];
            byte[] numArray1 = new byte[23];
            if (key == null || validate == null || data == null || size < 0)
            {
                return null;
            }
            int length = key.Length;
            int num = validate.Length;
            int num1 = (new Random()).Next();
            for (int i = 0; i < numArray1.Length; i++)
            {
                num1 = num1 * 214013 + 2531011;
                numArray1[i] = (byte)((num1 ^ key[i % length] ^ validate[i % num]) % 256);
            }
            numArray1[0] = 235;
            numArray1[1] = 0;
            numArray1[2] = 0;
            numArray1[8] = 228;
            for (long j = size - 1; j >= 0; j = j - 1)
            {
                numArray[numArray1.Length + j] = data[j];
            }
            Array.Copy(numArray1, numArray, numArray1.Length);
            size = size + numArray1.Length;
            byte[] numArray2 = Two(key, validate, numArray, size, null);
            byte[] numArray3 = new byte[numArray2.Length + numArray1.Length];
            Array.Copy(numArray1, 0, numArray3, 0, numArray1.Length);
            Array.Copy(numArray2, 0, numArray3, numArray1.Length, numArray2.Length);
            return numArray3;
        }

Usage Example

Example #1
0
        /// <summary>
        /// Takes a message sent through the Stream and sends back a response
        /// </summary>
        /// <param name="message"></param>
        protected void ParseRequest(string message)
        {
            string[] data = message.Split(new char[] { '\x00' }, StringSplitOptions.RemoveEmptyEntries);

            //string gamename = data[1].ToLowerInvariant();
            string validate = data[2].Substring(0, 8);
            string filter   = FixFilter(data[2].Substring(8));

            string[] fields = data[3].Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);

            // Send the encrypted serverlist to the client
            byte[] unencryptedServerList = PackServerList(filter, fields);
            Stream.SendAsync(
                Enctypex.Encode(
                    Encoding.UTF8.GetBytes("hW6m9a"), // Battlfield 2 Handoff Key
                    Encoding.UTF8.GetBytes(validate),
                    unencryptedServerList,
                    unencryptedServerList.LongLength
                    )
                );
        }