Esempio n. 1
0
        public int Decrypt(Internal.CommunicationStream message, int offset = 0, int length = int.MaxValue)
        {
            length  = Math.Min(length, (int)message.Length - offset);
            length -= length % BlockSize;
            int i = offset;

            while (i < offset + length)
            {
                message.Position = i;
                uint v0    = message.ReadUnsignedInt();
                uint v1    = message.ReadUnsignedInt();
                uint delta = 0x61C88647;
                uint sum   = 0xC6EF3720;
                for (int r = 0; r < 32; r++)
                {
                    v1  -= (v0 << 4 ^ v0 >> 5) + v0 ^ sum + _key[sum >> 11 & 3];
                    sum += delta;
                    v0  -= (v1 << 4 ^ v1 >> 5) + v1 ^ sum + _key[sum & 3];
                }
                message.Position -= BlockSize;
                message.WriteUnsignedInt(v0);
                message.WriteUnsignedInt(v1);
                i += BlockSize;
            }

            message.Position = offset;
            int payloadLength = message.ReadUnsignedShort();
            int messageLength = payloadLength + Internal.Connection.PacketLengthSize;

            int lengthDelta = messageLength - length;

            if (lengthDelta > 0 || -lengthDelta > length)
            {
                return(0);
            }

            message.SetLength(message.Length + lengthDelta);
            return(length);
        }