public void Decrypt(byte[] content, byte[] sectorId) { var aes = AesXts.Create(false, 0x20, true); var enc = aes.CreateDecryptor(new byte[0x20], sectorId); enc.TransformBlock(content, 0, content.Length, content, 0); }
public void XtsContextTest() { var aes = AesXts.Create(false, 0x20, true); var enc = aes.CreateEncryptor(new byte[0x20], new byte[0x10]); var dec = aes.CreateDecryptor(new byte[0x20], new byte[0x10]); var content = new byte[0x10]; enc.TransformBlock(content, 0, content.Length, content, 0); dec.TransformBlock(content, 0, content.Length, content, 0); Assert.IsTrue(new byte[0x10].SequenceEqual(content)); }
public XtsStream(Stream input, byte[] key, byte[] sectorId, bool advanceSectorId, bool littleEndianId, int sectorSize) { if (key.Length / 4 < 4 || key.Length / 4 > 8 || key.Length % 4 > 0) { throw new InvalidOperationException("Key has invalid length."); } if (sectorId.Length != BlockSize) { throw new InvalidOperationException("SectorId has invalid length."); } if (input.Length % BlockSize != 0) { throw new InvalidOperationException($"Stream needs to have a length dividable by {BlockSize}."); } if (sectorSize % BlockSize != 0) { throw new InvalidOperationException($"SectorSize needs to be dividable by {BlockSize}"); } _baseStream = input; _internalLength = _baseStream.Length; _lastBlockBuffer = new byte[BlockSize]; _littleEndianId = littleEndianId; _advanceSectorId = advanceSectorId; _sectorSize = sectorSize; _initialId = new byte[sectorId.Length]; Array.Copy(sectorId, _initialId, sectorId.Length); _currentId = new byte[sectorId.Length]; Array.Copy(sectorId, _currentId, sectorId.Length); var xts = AesXts.Create(littleEndianId, sectorSize, advanceSectorId); _encryptor = (AesXtsCryptoTransform)xts.CreateEncryptor(key, _initialId); _decryptor = (AesXtsCryptoTransform)xts.CreateDecryptor(key, _initialId); }