示例#1
0
        /* Process description:
         * 1) Insert data
         * 2) Insert size of data (4 bytes int)
         * 3) Insert packet type (4 bytes int)
         * 4) Insert crc32 of type+size+data (4 bytes uint)
         * 5) Insert empty IV (16 bytes string)
         */
        public static byte[] CreatePacket(byte[] serializedPacket, int packetType)
        {
            byte[] packetBytes = serializedPacket;                                                                             //Insert data

            packetBytes = InsertBytesAtIndex(0, BitConverter.GetBytes(packetBytes.Length), packetBytes);                       //+size
            packetBytes = InsertBytesAtIndex(0, BitConverter.GetBytes(packetType), packetBytes);                               //+type
            packetBytes = InsertBytesAtIndex(0, BitConverter.GetBytes(Crc32Operation.ComputeCRC32(packetBytes)), packetBytes); //+crc
            packetBytes = InsertBytesAtIndex(0, Encoding.ASCII.GetBytes(EmptyIV), packetBytes);                                //+empty iv

            return(packetBytes);
        }
示例#2
0
        public static bool IsValidCRC32(uint crc, byte[][] packetParts)
        {
            byte[] total  = new byte[packetParts.Sum(parts => parts.Length)];
            int    offset = 0;

            foreach (byte[] part in packetParts)
            {
                Array.Copy(part, 0, total, offset, part.Length);
                offset += part.Length;
            }

            return(crc == Crc32Operation.ComputeCRC32(total));
        }
示例#3
0
        /* Process description:
         * 1) Insert data
         * 2) Insert size of data (4 bytes int)
         * 3) Insert packet type (4 bytes int)
         * 4) Insert crc32 of type+size+data (4 bytes uint)
         * 5) Encrypt entire array w/ AES256
         * 6) Insert size of encrypted data (4 bytes int)
         * 7) Insert IV (16 bytes string)
         */
        public static byte[] CreateEncryptedPacket(byte[] serializedPacket, int packetType, byte[] key, byte[] iv)
        {
            byte[] finalBytes = serializedPacket;                                                                           //Insert data

            finalBytes = InsertBytesAtIndex(0, BitConverter.GetBytes(finalBytes.Length), finalBytes);                       //+size
            finalBytes = InsertBytesAtIndex(0, BitConverter.GetBytes(packetType), finalBytes);                              //+type
            finalBytes = InsertBytesAtIndex(0, BitConverter.GetBytes(Crc32Operation.ComputeCRC32(finalBytes)), finalBytes); //+crc
            finalBytes = new Aes(256).Encrypt(finalBytes, key, iv);                                                         //+enc

                        #if DEBUG
            Console.WriteLine($"Encrypted data: {BitConverter.ToString(finalBytes)}");
                        #endif

            finalBytes = InsertBytesAtIndex(0, BitConverter.GetBytes(finalBytes.Length), finalBytes); //+encSize
            finalBytes = InsertBytesAtIndex(0, iv, finalBytes);                                       //+iv

            return(finalBytes);
        }