Example #1
0
        private byte[] GenerateSequence()
        {
            // Generate between 0 and maxLen random bytes to be used as padding
            byte[] padding = provider.GetBytes(provider.NextUShort((ushort)(maxLen + 1)));

            // Remove instances of the delimiter sequence from the padding
            int idx;

            while ((idx = Support.ArrayContains(padding, delimiter)) != -1)
            {
                foreach (byte val in provider.GetBytes(delimiter.Length))
                {
                    padding[idx++] = val;
                }
            }
            return(padding);
        }
Example #2
0
        public byte[] Unpad(byte[] message)
        {
            int index = Support.ArrayContains(message, delimiter);

            if (index == -1)
            {
                throw new InvalidPaddingException("Preceding delimiter could not be found");
            }
            byte[] result_stage1 = new byte[message.Length - 1 - index];
            Array.Copy(message, index + 1, result_stage1, 0, message.Length - 1 - index);
            index = Support.ArrayContains(result_stage1, delimiter, false);
            if (index == -1)
            {
                throw new InvalidPaddingException("Trailing delimeter could not be found");
            }
            byte[] result_stage2 = new byte[index];
            Array.Copy(result_stage1, 0, result_stage2, 0, index);
            return(result_stage2);
        }