예제 #1
0
        public int doFinal(
            byte[]  outBytes,
            int outOff)
        {
            int blockSize = cipher.getBlockSize();

            //
            // pad with zeroes
            //
            if (this.padding == null)
            {
                while (bufOff < blockSize)
                {
                    buf[bufOff] = 0;
                    bufOff++;
                }
            }
            else
            {
                padding.addPadding(buf, bufOff);
            }

            cipher.processBlock(buf, 0, mac, 0);

            cipher.getMacBlock(mac);

            Array.Copy(mac, 0, outBytes, outOff, macSize);

            reset();

            return(macSize);
        }
예제 #2
0
        /**
         * Process the last block in the buffer. If the buffer is currently
         * full and padding needs to be added a call to doFinal will produce
         * 2 * getBlockSize() bytes.
         *
         * @param out the array the block currently being held is copied into.
         * @param outOff the offset at which the copying starts.
         * @return the number of output bytes copied to out.
         * @exception DataLengthException if there is insufficient space in out for
         * the output or we are decrypting and the input is not block size aligned.
         * @exception IllegalStateException if the underlying cipher is not
         * initialised.
         * @exception InvalidCipherTextException if padding is expected and not found.
         */
        public override int doFinal(
            byte[]  outBytes,
            int outOff)
        //throws DataLengthException, IllegalStateException, InvalidCipherTextException
        {
            int blockSize = cipher.getBlockSize();
            int resultLen = 0;

            if (forEncryption)
            {
                if (bufOff == blockSize)
                {
                    if ((outOff + 2 * blockSize) > outBytes.Length)
                    {
                        reset();

                        throw new DataLengthException("output buffer too short");
                    }

                    resultLen = cipher.processBlock(buf, 0, outBytes, outOff);
                    bufOff    = 0;
                }

                padding.addPadding(buf, bufOff);

                resultLen += cipher.processBlock(buf, 0, outBytes, outOff + resultLen);

                reset();
            }
            else
            {
                if (bufOff == blockSize)
                {
                    resultLen = cipher.processBlock(buf, 0, buf, 0);
                    bufOff    = 0;
                }
                else
                {
                    reset();

                    throw new DataLengthException("last block incomplete in decryption");
                }

                try
                {
                    resultLen -= padding.padCount(buf);

                    Array.Copy(buf, 0, outBytes, outOff, resultLen);
                }
                finally
                {
                    reset();
                }
            }

            return(resultLen);
        }