Example #1
0
        private void PerformTest()
        {
            _engine.Init(true, _key);

            byte[] outBytes = new byte[_input.Length];

            Array.Copy(_input, 0, outBytes, 0, outBytes.Length);

            for (int i = 0; i != _iterations; i++)
            {
                _engine.ProcessBlock(outBytes, 0, outBytes, 0);
            }

            if (!outBytes.SequenceEqual(_output))
            {
                throw new Exception("Arrays are not equal!");
            }

            _engine.Init(false, _key);

            for (int i = 0; i != _iterations; i++)
            {
                _engine.ProcessBlock(outBytes, 0, outBytes, 0);
            }

            if (!outBytes.SequenceEqual(_input))
            {
                throw new Exception("Arrays are not equal!");
            }
        }
Example #2
0
        private byte[] DecryptAesFast(byte[] Key, byte[] Data)
        {
            int blocks = Data.Length / 16;
            byte[] outputData = new byte[Data.Length];

            AesFastEngine transform = new AesFastEngine();
            transform.Init(false, Key);

            for (int i = 0; i < blocks; i++)
                transform.ProcessBlock(Data, i * 16, outputData, i * 16);

            return outputData;
        }
Example #3
0
        /// <summary>
        /// Transforms a packet
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public unsafe void ProcessPacket(byte[] packet, int offset, int count)
        {
            int ptr = offset, ctr = count;

            var temp = new byte[16];

            fixed(byte *tmpPtr = temp, packetPtr = packet)
            {
                // Read and encrypt 16 byte chunks with the provided cipher
                // Write the processed values right back to the packet
                while (ctr >= 16)
                {
                    // Mabi's cipher treats blocks as Big Endian
                    // But standard AES treats them as Little, so we need to swap our data
                    for (var i = 0; i < 16; i += 4)
                    {
                        *(tmpPtr + i + 0) = *(packetPtr + ptr + i + 3);
                        *(tmpPtr + i + 1) = *(packetPtr + ptr + i + 2);
                        *(tmpPtr + i + 2) = *(packetPtr + ptr + i + 1);
                        *(tmpPtr + i + 3) = *(packetPtr + ptr + i + 0);
                    }

                    _aesEngine.ProcessBlock(temp, 0, temp, 0);

                    for (var i = 0; i < 16; i += 4)
                    {
                        *(packetPtr + ptr + i + 0) = *(tmpPtr + i + 3);
                        *(packetPtr + ptr + i + 1) = *(tmpPtr + i + 2);
                        *(packetPtr + ptr + i + 2) = *(tmpPtr + i + 1);
                        *(packetPtr + ptr + i + 3) = *(tmpPtr + i + 0);
                    }

                    ptr += 16;
                    ctr -= 16;
                }

                if (ctr != 0)                 // If we have leftovers, we'd better eat - err, mask - them
                {
                    for (var i = 0; i < ctr; ++i)
                    {
                        *(packetPtr + ptr) ^= _remainderMask[i];
                        ptr++;
                    }
                }
            }
        }
Example #4
0
        private byte[] EncryptAesFast(byte[] Key, byte[] Data)
        {
            int blocks = Data.Length / 16;

            byte[] outputData = new byte[Data.Length];

            AesFastEngine transform = new AesFastEngine();

            transform.Init(true, Key);

            for (int i = 0; i < blocks; i++)
            {
                transform.ProcessBlock(Data, i * 16, outputData, i * 16);
            }

            return(outputData);
        }
 private void MakeBytes()
 {
     bufOffset = 0;
     _engine.ProcessBlock(_counter, 0, _buf, 0);
     IncrementCounter();
 }