Esempio n. 1
0
        internal static byte[] Encode(byte[] arr, AesEncryptor aesEncryptor, byte sendIdx)
        {
            bool ziped = false;

            if (arr.Length >= 1024)
            {
                arr   = ZLib.Zip(arr);
                ziped = true;
            }
            bool aesed = (new Random().Next() % 10) < 3;

            if (aesed)
            {
                arr = aesEncryptor.Encrypt(arr);
            }
            int  crc32 = 0;
            bool crced = (new Random().Next() % 10) < 3;

            if (crced)
            {
                crc32 = Crc.Crc32(arr);
            }

            int alllen = arr.Length + 1 + 4;

            byte[] balllen = BitConverter.GetBytes(alllen);
            byte   flag    = sendIdx;

            if (ziped)
            {
                flag |= 0x80;
            }
            if (aesed)
            {
                flag |= 0x40;
            }
            if (crced)
            {
                flag |= 0x20;
            }
            var m2 = BitConverter.GetBytes(crc32);
            var os = new MemoryStream();

            os.Write(balllen, 0, 4); //allLen
            os.WriteByte(flag);      //flag
            os.Write(m2, 0, 4);      //crc
            os.Write(arr, 0, arr.Length);
            return(os.ToArray());
        }
Esempio n. 2
0
        public static byte[] Encode(byte[] arr, AesEncryptor aesEncryptor, byte sendIdx)
        {
            byte flag  = sendIdx;
            int  crc32 = 0;

            if (Enabled_Compress && false)
            {
                if (arr.Length >= 1024)
                {
                    arr   = ZLib.Zip(arr);
                    flag |= CCC_Compress;
                }
            }
            if (Enabled_Crypto || true)
            {
                bool aesed = (new Random().Next() % 10) < 3;
                if (aesed || true)
                {
                    arr   = aesEncryptor.Encrypt(arr);
                    flag |= CCC_Crypto;
                }
            }
            if (Enabled_Crc)
            {
                bool crced = (new Random().Next() % 10) < 3;
                if (crced)
                {
                    crc32 = Crc.Crc32(arr);
                    flag |= CCC_Crc;
                }
            }

            int alllen = arr.Length;// + 1 + 4;

            byte[] balllen = BitConverter.GetBytes(alllen);
            CheckReverse(balllen);

            var m2 = BitConverter.GetBytes(crc32);

            CheckReverse(m2);
            var os = new MemoryStream();

            os.Write(balllen, 0, PackHeadSize); //allLen
            //os.WriteByte(flag);//flag
            //os.Write(m2, 0, 4);//crc
            os.Write(arr, 0, arr.Length);
            return(os.ToArray());
        }