public override void Write(byte[] buffer, int offset, int count)
        {
            if (!CanWrite)
            {
                throw new IOException(SR.StreamWriteNotSupported);
            }

            while (count > 0)
            {
                if (_bufferIndex == _blockSize)
                {
                    StrongCryptoUtils.Encrypt(_buffer, _key, 0, _bufferIndex);
                    _stream.Write(_buffer, 0, _bufferIndex);
                    _bufferIndex = 0;
                }

                int size = _blockSize - _bufferIndex;
                if (size > count)
                {
                    size = count;
                }

                Buffer.BlockCopy(buffer, offset, _buffer, _bufferIndex, size);

                count        -= size;
                offset       += size;
                _bufferIndex += size;
            }
        }
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    switch (_mode)
                    {
                    case StrongCryptoStreamMode.Write:
                    {
                        if (_bufferIndex > 0)
                        {
                            StrongCryptoUtils.Encrypt(_buffer, _key, 0, _bufferIndex);
                            _stream.Write(_buffer, 0, _bufferIndex);
                            _bufferIndex = 0;
                        }
                    }
                    break;
                    }

                    _stream.Close();
                }

                _buffer = null;
                _stream = null;
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (!CanRead)
            {
                throw new IOException(SR.StreamReadNotSupported);
            }

            if (_bufferLength < _blockSize)
            {
                if (_bufferLength == 0)
                {
                    int readLen = _stream.Read(_buffer, 0, _blockSize);
                    if (readLen == 0)
                    {
                        return(0);
                    }

                    _bufferLength = readLen;
                    StrongCryptoUtils.Decrypt(_buffer, _key, 0, _bufferLength);
                }

                if (_bufferIndex >= _bufferLength)
                {
                    return(0);
                }
            }

            int readCount = 0;

            while (count > 0)
            {
                if (_bufferIndex == _bufferLength)
                {
                    _bufferIndex = 0;

                    _bufferLength = _stream.Read(_buffer, 0, _blockSize);
                    if (_bufferLength == 0)
                    {
                        break;
                    }

                    StrongCryptoUtils.Decrypt(_buffer, _key, 0, _bufferLength);
                }

                int size = _bufferLength - _bufferIndex;
                if (size > count)
                {
                    size = count;
                }

                Buffer.BlockCopy(_buffer, _bufferIndex, buffer, offset, size);

                count        -= size;
                offset       += size;
                readCount    += size;
                _bufferIndex += size;
            }

            return(readCount);
        }