示例#1
0
        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);
        }
示例#2
0
            public override int Read(byte[] buffer, int offset, int count)
            {
                ValidateSize(count);
                var currentSector = CurrentSector;
                var ret           = base.Read(_tempBuffer, 0, count);

                if (ret == 0)
                {
                    return(0);
                }
                if (_decryptor == null)
                {
                    _decryptor = _xts.CreateDecryptor();
                }
                var retV = _decryptor.TransformBlock(_tempBuffer, 0, ret, buffer, offset, currentSector);

                return(retV);
            }