Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 4
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;
        }
Ejemplo n.º 5
0
        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;
        }