Example #1
0
        /// <summary>
        /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
        /// </summary>
        /// <param name="buffer">An array of bytes. This method copies <paramref name="count"/> bytes from <paramref name="buffer"/> to the current stream.</param>
        /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin copying bytes to the current stream.</param>
        /// <param name="count">The number of bytes to be written to the current stream.</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            ValidateSize(count);

            if (count == 0)
            {
                return;
            }

            //get the current sector
            long currentSector = CurrentSector;

            if (_encryptor == null)
            {
                _encryptor = _xts.CreateEncryptor();
            }

            //encrypt the sector
            int transformedCount = _encryptor.TransformBlock(buffer, offset, count, _tempBuffer, 0, (ulong)currentSector);

            //Console.WriteLine("Encrypting sector {0}", currentSector);

            //write it to the base stream
            base.Write(_tempBuffer, 0, transformedCount);
        }