예제 #1
0
        private bool CryptHeaderCik(bool encrypt)
        {
            if ((!encrypt && CikIsDecrypted) || IsXvcFile)
            {
                CikIsDecrypted = true;
                return(true);
            }

            if (!OdkKeyLoaded)
            {
                return(false);
            }

            var cipher = new AesCipher(OdkKey);

            if (encrypt)
            {
                cipher.EncryptBlock(Header.EncryptedCIK, 0, 0x10, Header.EncryptedCIK, 0);
                cipher.EncryptBlock(Header.EncryptedCIK, 0x10, 0x10, Header.EncryptedCIK, 0x10);
            }
            else
            {
                cipher.DecryptBlock(Header.EncryptedCIK, 0, 0x10, Header.EncryptedCIK, 0);
                cipher.DecryptBlock(Header.EncryptedCIK, 0x10, 0x10, Header.EncryptedCIK, 0x10);
            }

            CikIsDecrypted = !encrypt;

            return(true);
        }
예제 #2
0
파일: Shared.cs 프로젝트: hmyit/xvdtool
        public static byte[] CryptData(bool encrypt, byte[] data, byte[] key, byte[] startIv)
        {
            var cipher  = new AesCipher(key);
            int blocks  = data.Length / 0x10;
            var newData = new byte[data.Length];
            var iv      = new byte[startIv.Length];

            Array.Copy(startIv, iv, startIv.Length);
            for (int i = 0; i < blocks; i++)
            {
                byte[] crypted = CryptBlock(encrypt, data, i * 0x10, 0x10, iv, cipher);
                iv = MorphIv(iv);
                Array.Copy(crypted, 0, newData, i * 0x10, 0x10);
            }
            return(newData);
        }
예제 #3
0
        private bool CryptSection(bool encrypt, byte[] key, uint headerId, ulong offset, ulong length)
        {
            var ivKey   = new byte[0x10];
            var dataKey = new byte[0x10];

            Array.Copy(key, ivKey, 0x10);
            Array.Copy(key, 0x10, dataKey, 0, 0x10);

            var counterToEncrypt = new byte[0x10];

            Array.Copy(Header.VDUID, 0, counterToEncrypt, 0x8, 0x8);
            byte[] test = BitConverter.GetBytes(headerId);
            Array.Copy(test, 0, counterToEncrypt, 0x4, 0x4);

            int numBlocks = (int)(length + 0xFFF) / 0x1000;

            var ivCipher = new AesCipher(ivKey);

            _io.Stream.Position = (long)offset;
            for (int i = 0; i < numBlocks; i++)
            {
                var counter = new byte[0x10];
                Array.Copy(counterToEncrypt, counter, 0x10);

                var blockIdBytes = BitConverter.GetBytes(i);
                Array.Copy(blockIdBytes, counter, 4);

                var counterEnc = new byte[0x10];
                ivCipher.EncryptBlock(counter, 0, 0x10, counterEnc, 0);

                byte[] origData = _io.Reader.ReadBytes(0x1000);
                byte[] newData  = Shared.CryptData(encrypt, origData, dataKey, counterEnc);

                _io.Stream.Position = _io.Stream.Position - 0x1000;
                _io.Writer.Write(newData);
            }
            return(true);
        }
예제 #4
0
파일: XVDFile.cs 프로젝트: Overx/xvdtool
        private bool CryptSection(bool encrypt, byte[] key, uint headerId, ulong offset, ulong length)
        {
            var ivKey = new byte[0x10];
            var dataKey = new byte[0x10];
            Array.Copy(key, ivKey, 0x10);
            Array.Copy(key, 0x10, dataKey, 0, 0x10);

            var counterToEncrypt = new byte[0x10];
            Array.Copy(Header.VDUID, 0, counterToEncrypt, 0x8, 0x8);
            byte[] test = BitConverter.GetBytes(headerId);
            Array.Copy(test, 0, counterToEncrypt, 0x4, 0x4);

            int numBlocks = (int)(length + 0xFFF) / 0x1000;

            var ivCipher = new AesCipher(ivKey);

            _io.Stream.Position = (long)offset;
            for (int i = 0; i < numBlocks; i++)
            {
                var counter = new byte[0x10];
                Array.Copy(counterToEncrypt, counter, 0x10);

                var blockIdBytes = BitConverter.GetBytes(i);
                Array.Copy(blockIdBytes, counter, 4);

                var counterEnc = new byte[0x10];
                ivCipher.EncryptBlock(counter, 0, 0x10, counterEnc, 0);

                byte[] origData = _io.Reader.ReadBytes(0x1000);
                byte[] newData = Shared.CryptData(encrypt, origData, dataKey, counterEnc);

                _io.Stream.Position = _io.Stream.Position - 0x1000;
                _io.Writer.Write(newData);
            }
            return true;
        }
예제 #5
0
파일: XVDFile.cs 프로젝트: Overx/xvdtool
        private void CryptHeaderCik(bool encrypt)
        {
            if ((!encrypt && CikIsDecrypted) || IsXvcFile)
            {
                CikIsDecrypted = true;
                return;
            }

            if (!OdkKeyLoaded)
                return;

            var cipher = new AesCipher(OdkKey);
            if (encrypt)
            {
                cipher.EncryptBlock(Header.EncryptedCIK, 0, 0x10, Header.EncryptedCIK, 0);
                cipher.EncryptBlock(Header.EncryptedCIK, 0x10, 0x10, Header.EncryptedCIK, 0x10);
            }
            else
            {
                cipher.DecryptBlock(Header.EncryptedCIK, 0, 0x10, Header.EncryptedCIK, 0);
                cipher.DecryptBlock(Header.EncryptedCIK, 0x10, 0x10, Header.EncryptedCIK, 0x10);
            }

            CikIsDecrypted = !encrypt;
        }
예제 #6
0
파일: Shared.cs 프로젝트: hmyit/xvdtool
        static byte[] CryptBlock(bool encrypt, byte[] data, int dataOffset, int dataLength, byte[] iv, AesCipher cipher)
        {
            var newData = new byte[dataLength];

            //if (!encrypt)
            for (int i = 0; i < dataLength; i++)
            {
                newData[i] = (byte)(data[dataOffset + i] ^ iv[i % iv.Length]);
            }

            var cryptData = new byte[dataLength];

            if (encrypt)
            {
                cipher.EncryptBlock(newData, 0, dataLength, cryptData, 0);
            }
            else
            {
                cipher.DecryptBlock(newData, 0, dataLength, cryptData, 0);
            }

            for (int i = 0; i < dataLength; i++)
            {
                cryptData[i] = (byte)(cryptData[i] ^ iv[i % iv.Length]);
            }

            return(cryptData);
        }