Esempio n. 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
            var currentSector = CurrentSector;

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

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

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

            //write it to the base stream
            base.Write(_tempBuffer, 0, transformedCount);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new stream
        /// </summary>
        /// <param name="baseStream">The base stream</param>
        /// <param name="xts">The xts transform</param>
        /// <param name="sectorSize">Sector size</param>
        public XTSWriteOnlyStream(Stream baseStream, Xts xts, int sectorSize = DefaultSectorSize)
        {
            _baseStream = baseStream;

            _encryptor = xts.CreateEncryptor();

            _sectorSize      = sectorSize;
            _encriptedBuffer = new byte[sectorSize];
            _sectorBuffer    = new byte[sectorSize];
        }