public static byte[] XTS(byte[] Key1, byte[] Key2, int SectorSize, byte[] Data, ulong Sector, bool Encrypt) { byte[] TransformedBytes, BlockData; Xts XTS128 = XtsAes128.Create(Key1, Key2); int Blocks; var MemStrm = new MemoryStream(); var Writer = new BinaryWriter(MemStrm); XtsCryptoTransform CryptoTransform; if (Encrypt) { CryptoTransform = XTS128.CreateEncryptor(); } else { CryptoTransform = XTS128.CreateDecryptor(); } BlockData = new byte[SectorSize]; Blocks = Data.Length / SectorSize; for (int i = 0; i < Blocks; i++) { CryptoTransform.TransformBlock(Data, i * SectorSize, SectorSize, BlockData, 0, Sector++); Writer.Write(BlockData); } TransformedBytes = MemStrm.ToArray(); return(TransformedBytes); }
public override void Write(byte[] buffer, int offset, int count) { ValidateSize(count); if (count == 0) { return; } var currentSector = CurrentSector; if (_encryptor == null) { _encryptor = _xts.CreateEncryptor(); } int transformedCount = _encryptor.TransformBlock(buffer, offset, count, _tempBuffer, 0, currentSector); base.Write(_tempBuffer, 0, transformedCount); }