/**
         * add the pad bytes to the passed in block, returning the
         * number of bytes added.
         */
        public int AddPadding(
            byte[]      input,
            int inOff)
        {
            var code = (byte)(input.Length - inOff);

            while (inOff < (input.Length - 1))
            {
                input[inOff] = (byte)_random.NextInt();
                inOff++;
            }

            input[inOff] = code;

            return(code);
        }
예제 #2
0
        private byte[] EncodeBlock(byte[] input, int inOff, int inLen)
        {
            if (inLen > GetInputBlockSize())
            {
                throw new ArgumentException(@"input data too large", "inLen");
            }

            var block = new byte[_engine.GetInputBlockSize()];

            if (_forPrivateKey)
            {
                block[0] = 0x01;                        // type code 1

                for (var i = 1; i != block.Length - inLen - 1; i++)
                {
                    block[i] = (byte)0xFF;
                }
            }
            else
            {
                _random.NextBytes(block);               // random fill

                block[0] = 0x02;                        // type code 2

                //
                // a zero byte marks the end of the padding, so all
                // the pad bytes must be non-zero.
                //
                for (var i = 1; i != block.Length - inLen - 1; i++)
                {
                    while (block[i] == 0)
                    {
                        block[i] = (byte)_random.NextInt();
                    }
                }
            }

            block[block.Length - inLen - 1] = 0x00;       // mark the end of the padding
            Array.Copy(input, inOff, block, block.Length - inLen, inLen);

            return(_engine.ProcessBlock(block, 0, block.Length));
        }