Ejemplo n.º 1
0
        private int encryptData(FileStream ii, FileStream o, NPD npd, EDATData data, byte[] rifkey)
        {
            int num = (int)(((data.getFileLen() + data.getBlockSize()) - 1) / data.getBlockSize());

            byte[] dest    = new byte[num * 0x10];
            byte[] buffer2 = new byte[ii.Length + 15L];
            for (int i = 0; i < num; i++)
            {
                long offset = i * data.getBlockSize();
                ii.Seek(offset, SeekOrigin.Begin);
                int length = (int)data.getBlockSize();
                if (i == (num - 1))
                {
                    length = (int)(data.getFileLen() % new BigInteger(data.getBlockSize()));
                }
                int num5 = length;
                length = (length + 15) & -16;
                byte[] buffer3 = new byte[length];
                byte[] buffer4 = new byte[length];
                for (int j = num5; j > 0; j -= ii.Read(buffer4, num5 - j, j))
                {
                }
                for (int k = num5; k < length; k++)
                {
                    buffer4[k] = 0;
                }
                byte[] buffer5 = new byte[0x10];
                byte[] buffer6 = new byte[0x10];
                byte[] buffer7 = this.calculateBlockKey(i, npd);
                ToolsImpl.aesecbEncrypt(rifkey, buffer7, 0, buffer5, 0, buffer7.Length);
                ConversionUtils.arraycopy(buffer5, 0, buffer6, 0L, buffer5.Length);
                int cryptoFlag                 = 2;
                int hashFlag                   = 2;
                AppLoaderReverse reverse       = new AppLoaderReverse();
                byte[]           iv            = npd.getDigest();
                byte[]           generatedHash = new byte[0x10];
                reverse.doAll(hashFlag, cryptoFlag, buffer4, 0, buffer3, 0, buffer4.Length, buffer5, iv, buffer6, generatedHash, 0);
                ConversionUtils.arraycopy(buffer3, 0, buffer2, offset, length);
                ConversionUtils.arraycopy(generatedHash, 0, dest, (long)(i * 0x10), 0x10);
            }
            byte[] buffer = ConversionUtils.getByteArray("4D6164652062792052325220546F6F6C");
            o.Write(dest, 0, dest.Length);
            o.Write(buffer2, 0, buffer2.Length - 15);
            o.Write(buffer, 0, buffer.Length);
            return(STATUS_OK);
        }
Ejemplo n.º 2
0
        private int decryptData(FileStream ii, FileStream o, NPD npd, EDATData data, byte[] rifkey)
        {
            int numBlocks           = (int)((data.getFileLen() + data.getBlockSize() - 1) / data.getBlockSize());
            int metadataSectionSize = ((data.getFlags() & FLAG_COMPRESSED) != 0 || (data.getFlags() & FLAG_0x20) != 0) ? 0x20 : 0x10;
            int baseOffset          = 0x100; //+ offset (unknown)

            for (int i = 0; i < numBlocks; i++)
            {
                ii.Seek(baseOffset + i * metadataSectionSize, SeekOrigin.Begin);
                byte[] expectedHash = new byte[0x10];
                long   offset;
                int    len;
                int    compressionEndBlock = 0;
                if ((data.getFlags() & FLAG_COMPRESSED) != 0)
                {
                    byte[] metadata = new byte[0x20];
                    ii.Read(metadata, 0, metadata.Length);
                    byte[] result = decryptMetadataSection(metadata);
                    offset = (int)(ConversionUtils.be64(result, 0)); // + offset (unknown)
                    len    = (int)(ConversionUtils.be32(result, 8));
                    compressionEndBlock = (int)(ConversionUtils.be32(result, 0xC));
                    ConversionUtils.arraycopy(metadata, 0, expectedHash, 0, 0x10);
                }
                else if ((data.getFlags() & FLAG_0x20) != 0)
                {
                    //NOT TESTED: CASE WHERE METADATASECTION IS 0x20 BYTES LONG
                    byte[] metadata = new byte[0x20];
                    ii.Read(metadata, 0, metadata.Length);
                    for (int j = 0; j < 0x10; j++)
                    {
                        expectedHash[j] = (byte)(metadata[j] ^ metadata[j + 0x10]);
                    }
                    offset = baseOffset + i * data.getBlockSize() + numBlocks * metadataSectionSize;
                    len    = (int)(data.getBlockSize());
                    if (i == numBlocks - 1)
                    {
                        len = (int)(data.getFileLen() % (new BigInteger(data.getBlockSize())));
                    }
                }
                else
                {
                    ii.Read(expectedHash, 0, expectedHash.Length);
                    offset = baseOffset + i * data.getBlockSize() + numBlocks * metadataSectionSize;
                    len    = (int)(data.getBlockSize());
                    if (i == numBlocks - 1)
                    {
                        len = (int)(data.getFileLen() % (new BigInteger(data.getBlockSize())));
                    }
                }
                int realLen = len;
                len = (int)((uint)(len + 0xF) & 0xFFFFFFF0);
                Debug.Print("Offset: %016X, len: %08X, realLen: %08X, endCompress: %d\r\n", offset, len, realLen, compressionEndBlock);
                ii.Seek(offset, SeekOrigin.Begin);
                byte[] encryptedData = new byte[len];
                byte[] decryptedData = new byte[len];
                ii.Read(encryptedData, 0, encryptedData.Length);
                byte[] key      = new byte[0x10];
                byte[] hash     = new byte[0x10];
                byte[] blockKey = calculateBlockKey(i, npd);

                ToolsImpl.aesecbEncrypt(rifkey, blockKey, 0, key, 0, blockKey.Length);
                if ((data.getFlags() & FLAG_0x10) != 0)
                {
                    ToolsImpl.aesecbEncrypt(rifkey, key, 0, hash, 0, key.Length);
                }
                else
                {
                    ConversionUtils.arraycopy(key, 0, hash, 0, key.Length);
                }
                int cryptoFlag = ((data.getFlags() & FLAG_0x02) == 0) ? 0x2 : 0x1;
                int hashFlag;
                if ((data.getFlags() & FLAG_0x10) == 0)
                {
                    hashFlag = 0x02;
                }
                else if ((data.getFlags() & FLAG_0x20) == 0)
                {
                    hashFlag = 0x04;
                }
                else
                {
                    hashFlag = 0x01;
                }
                if ((data.getFlags() & FLAG_KEYENCRYPTED) != 0)
                {
                    cryptoFlag |= 0x10000000;
                    hashFlag   |= 0x10000000;
                }
                if ((data.getFlags() & FLAG_DEBUG) != 0)
                {
                    cryptoFlag |= 0x01000000;
                    hashFlag   |= 0x01000000;
                }
                AppLoader a  = new AppLoader();
                byte[]    iv = (npd.getVersion() <= 1) ? (new byte[0x10]) : npd.getDigest();

                bool rresult = a.doAll(hashFlag, cryptoFlag, encryptedData, 0, decryptedData, 0, encryptedData.Length, key, npd.getDigest(), hash, expectedHash, 0);
                if (!rresult)
                {
                    Debug.WriteLine("Error decrypting block " + i);
                    // KDSBest find out why block 30 errors
                    //return STATUS_ERROR_DECRYPTING;
                }
                if ((data.getFlags() & FLAG_COMPRESSED) != 0)
                {
                    //byte[] decompress = new byte[Long.valueOf(data.getBlockSize()).intValue()];
                    //DECOMPRESS: MISSING ALGORITHM
                    //out.write(decompress, 0, data.getBlockSize());
                }
                else
                {
                    o.Write(decryptedData, 0, realLen);
                }
            }
            return(STATUS_OK);
        }
Ejemplo n.º 3
0
        private int encryptData(FileStream ii, FileStream o, NPD npd, EDATData data, byte[] rifkey)
        {
            int numBlocks = (int)((data.getFileLen() + data.getBlockSize() - 1) / data.getBlockSize());

            byte[] expectedHashForFile  = new byte[numBlocks * 0x10];
            byte[] encryptedDataForFile = new byte[ii.Length + 0xF];

            // File Format:
            // ALL HASHES
            // Encrypted Data
            for (int i = 0; i < numBlocks; i++)
            {
                long offset;
                int  len;

                offset = i * data.getBlockSize();
                ii.Seek(offset, SeekOrigin.Begin);
                len = (int)(data.getBlockSize());
                if (i == numBlocks - 1)
                {
                    len = (int)(data.getFileLen() % (new BigInteger(data.getBlockSize())));
                }


                int realLen = len;
                len = (int)((uint)(len + 0x0F) & 0xFFFFFFF0);

                byte[] encryptedData = new byte[len];
                byte[] decryptedData = new byte[len];

                int toRead = realLen;
                while (toRead > 0)
                {
                    toRead -= ii.Read(decryptedData, realLen - toRead, toRead);
                }
                for (int ai = realLen; ai < len; ai++)
                {
                    decryptedData[ai] = 0x00;
                }

                byte[] key      = new byte[0x10];
                byte[] hash     = new byte[0x10];
                byte[] blockKey = calculateBlockKey(i, npd);

                ToolsImpl.aesecbEncrypt(rifkey, blockKey, 0, key, 0, blockKey.Length);
                ConversionUtils.arraycopy(key, 0, hash, 0, key.Length);
                int cryptoFlag = 0x2;
                int hashFlag   = 0x02;

                AppLoaderReverse a             = new AppLoaderReverse();
                byte[]           iv            = npd.getDigest();
                byte[]           generatedHash = new byte[0x10];
                a.doAll(hashFlag, cryptoFlag, decryptedData, 0, encryptedData, 0, decryptedData.Length, key, iv, hash, generatedHash, 0);
                ConversionUtils.arraycopy(encryptedData, 0, encryptedDataForFile, offset, len);
                ConversionUtils.arraycopy(generatedHash, 0, expectedHashForFile, i * 0x10, 0x10);
            }
            byte[] EDATAVersion = ConversionUtils.getByteArray("4D6164652062792052325220546F6F6C");
            o.Write(expectedHashForFile, 0, expectedHashForFile.Length);
            o.Write(encryptedDataForFile, 0, encryptedDataForFile.Length - 0xf);
            o.Write(EDATAVersion, 0, EDATAVersion.Length);
            return(STATUS_OK);
        }
Ejemplo n.º 4
0
 private int encryptData(FileStream ii, FileStream o, NPD npd, EDATData data, byte[] rifkey)
 {
     int num = (int) (((data.getFileLen() + data.getBlockSize()) - 1) / data.getBlockSize());
     byte[] dest = new byte[num * 0x10];
     byte[] buffer2 = new byte[ii.Length + 15L];
     for (int i = 0; i < num; i++)
     {
         long offset = i * data.getBlockSize();
         ii.Seek(offset, SeekOrigin.Begin);
         int length = (int) data.getBlockSize();
         if (i == (num - 1))
         {
             length = (int) (data.getFileLen() % new BigInteger(data.getBlockSize()));
         }
         int num5 = length;
         length = (length + 15) & -16;
         byte[] buffer3 = new byte[length];
         byte[] buffer4 = new byte[length];
         for (int j = num5; j > 0; j -= ii.Read(buffer4, num5 - j, j))
         {
         }
         for (int k = num5; k < length; k++)
         {
             buffer4[k] = 0;
         }
         byte[] buffer5 = new byte[0x10];
         byte[] buffer6 = new byte[0x10];
         byte[] buffer7 = this.calculateBlockKey(i, npd);
         ToolsImpl.aesecbEncrypt(rifkey, buffer7, 0, buffer5, 0, buffer7.Length);
         ConversionUtils.arraycopy(buffer5, 0, buffer6, 0L, buffer5.Length);
         int cryptoFlag = 2;
         int hashFlag = 2;
         AppLoaderReverse reverse = new AppLoaderReverse();
         byte[] iv = npd.getDigest();
         byte[] generatedHash = new byte[0x10];
         reverse.doAll(hashFlag, cryptoFlag, buffer4, 0, buffer3, 0, buffer4.Length, buffer5, iv, buffer6, generatedHash, 0);
         ConversionUtils.arraycopy(buffer3, 0, buffer2, offset, length);
         ConversionUtils.arraycopy(generatedHash, 0, dest, (long) (i * 0x10), 0x10);
     }
     byte[] buffer = ConversionUtils.getByteArray("4D6164652062792052325220546F6F6C");
     o.Write(dest, 0, dest.Length);
     o.Write(buffer2, 0, buffer2.Length - 15);
     o.Write(buffer, 0, buffer.Length);
     return STATUS_OK;
 }
Ejemplo n.º 5
0
 private int decryptData(FileStream ii, FileStream o, NPD npd, EDATData data, byte[] rifkey)
 {
     int num = (int) (((data.getFileLen() + data.getBlockSize()) - 1) / data.getBlockSize());
     int num2 = (((data.getFlags() & FLAG_COMPRESSED) != 0L) || ((data.getFlags() & FLAG_0x20) != 0L)) ? 0x20 : 0x10;
     int num3 = 0x100;
     for (int i = 0; i < num; i++)
     {
         long num5;
         int num6;
         byte[] buffer2;
         int num11;
         ii.Seek((long) (num3 + (i * num2)), SeekOrigin.Begin);
         byte[] dest = new byte[0x10];
         int num7 = 0;
         if ((data.getFlags() & FLAG_COMPRESSED) != 0L)
         {
             buffer2 = new byte[0x20];
             ii.Read(buffer2, 0, buffer2.Length);
             byte[] buffer3 = this.decryptMetadataSection(buffer2);
             num5 = (int) ConversionUtils.be64(buffer3, 0);
             num6 = (int) ConversionUtils.be32(buffer3, 8);
             num7 = (int) ConversionUtils.be32(buffer3, 12);
             ConversionUtils.arraycopy(buffer2, 0, dest, 0L, 0x10);
         }
         else if ((data.getFlags() & FLAG_0x20) != 0L)
         {
             buffer2 = new byte[0x20];
             ii.Read(buffer2, 0, buffer2.Length);
             for (int j = 0; j < 0x10; j++)
             {
                 dest[j] = (byte) (buffer2[j] ^ buffer2[j + 0x10]);
             }
             num5 = (num3 + (i * data.getBlockSize())) + (num * num2);
             num6 = (int) data.getBlockSize();
             if (i == (num - 1))
             {
                 num6 = (int) (data.getFileLen() % new BigInteger(data.getBlockSize()));
             }
         }
         else
         {
             ii.Read(dest, 0, dest.Length);
             num5 = (num3 + (i * data.getBlockSize())) + (num * num2);
             num6 = (int) data.getBlockSize();
             if (i == (num - 1))
             {
                 num6 = (int) (data.getFileLen() % new BigInteger(data.getBlockSize()));
             }
         }
         int count = num6;
         num6 = (num6 + 15) & -16;
         Debug.Print("Offset: %016X, len: %08X, realLen: %08X, endCompress: %d\r\n", new object[] { num5, num6, count, num7 });
         ii.Seek(num5, SeekOrigin.Begin);
         byte[] buffer = new byte[num6];
         byte[] buffer5 = new byte[num6];
         ii.Read(buffer, 0, buffer.Length);
         byte[] buffer6 = new byte[0x10];
         byte[] buffer7 = new byte[0x10];
         byte[] buffer8 = this.calculateBlockKey(i, npd);
         ToolsImpl.aesecbEncrypt(rifkey, buffer8, 0, buffer6, 0, buffer8.Length);
         if ((data.getFlags() & FLAG_0x10) != 0L)
         {
             ToolsImpl.aesecbEncrypt(rifkey, buffer6, 0, buffer7, 0, buffer6.Length);
         }
         else
         {
             ConversionUtils.arraycopy(buffer6, 0, buffer7, 0L, buffer6.Length);
         }
         int cryptoFlag = ((data.getFlags() & FLAG_0x02) == 0L) ? 2 : 1;
         if ((data.getFlags() & FLAG_0x10) == 0L)
         {
             num11 = 2;
         }
         else if ((data.getFlags() & FLAG_0x20) == 0L)
         {
             num11 = 4;
         }
         else
         {
             num11 = 1;
         }
         if ((data.getFlags() & FLAG_KEYENCRYPTED) != 0L)
         {
             cryptoFlag |= 0x10000000;
             num11 |= 0x10000000;
         }
         if ((data.getFlags() & FLAG_DEBUG) != 0L)
         {
             cryptoFlag |= 0x1000000;
             num11 |= 0x1000000;
         }
         AppLoader loader = new AppLoader();
         byte[] buffer9 = (npd.getVersion() <= 1L) ? new byte[0x10] : npd.getDigest();
         if (!loader.doAll(num11, cryptoFlag, buffer, 0, buffer5, 0, buffer.Length, buffer6, npd.getDigest(), buffer7, dest, 0))
         {
             Debug.WriteLine("Error decrypting block " + i);
         }
         if ((data.getFlags() & FLAG_COMPRESSED) == 0L)
         {
             o.Write(buffer5, 0, count);
         }
     }
     return STATUS_OK;
 }
Ejemplo n.º 6
0
        private int decryptData(FileStream ii, FileStream o, NPD npd, EDATData data, byte[] rifkey)
        {
            int num  = (int)(((data.getFileLen() + data.getBlockSize()) - 1) / data.getBlockSize());
            int num2 = (((data.getFlags() & FLAG_COMPRESSED) != 0L) || ((data.getFlags() & FLAG_0x20) != 0L)) ? 0x20 : 0x10;
            int num3 = 0x100;

            for (int i = 0; i < num; i++)
            {
                long   num5;
                int    num6;
                byte[] buffer2;
                int    num11;
                ii.Seek((long)(num3 + (i * num2)), SeekOrigin.Begin);
                byte[] dest = new byte[0x10];
                int    num7 = 0;
                if ((data.getFlags() & FLAG_COMPRESSED) != 0L)
                {
                    buffer2 = new byte[0x20];
                    ii.Read(buffer2, 0, buffer2.Length);
                    byte[] buffer3 = this.decryptMetadataSection(buffer2);
                    num5 = (int)ConversionUtils.be64(buffer3, 0);
                    num6 = (int)ConversionUtils.be32(buffer3, 8);
                    num7 = (int)ConversionUtils.be32(buffer3, 12);
                    ConversionUtils.arraycopy(buffer2, 0, dest, 0L, 0x10);
                }
                else if ((data.getFlags() & FLAG_0x20) != 0L)
                {
                    buffer2 = new byte[0x20];
                    ii.Read(buffer2, 0, buffer2.Length);
                    for (int j = 0; j < 0x10; j++)
                    {
                        dest[j] = (byte)(buffer2[j] ^ buffer2[j + 0x10]);
                    }
                    num5 = (num3 + (i * data.getBlockSize())) + (num * num2);
                    num6 = (int)data.getBlockSize();
                    if (i == (num - 1))
                    {
                        num6 = (int)(data.getFileLen() % new BigInteger(data.getBlockSize()));
                    }
                }
                else
                {
                    ii.Read(dest, 0, dest.Length);
                    num5 = (num3 + (i * data.getBlockSize())) + (num * num2);
                    num6 = (int)data.getBlockSize();
                    if (i == (num - 1))
                    {
                        num6 = (int)(data.getFileLen() % new BigInteger(data.getBlockSize()));
                    }
                }
                int count = num6;
                num6 = (num6 + 15) & -16;
                Debug.Print("Offset: %016X, len: %08X, realLen: %08X, endCompress: %d\r\n", new object[] { num5, num6, count, num7 });
                ii.Seek(num5, SeekOrigin.Begin);
                byte[] buffer  = new byte[num6];
                byte[] buffer5 = new byte[num6];
                ii.Read(buffer, 0, buffer.Length);
                byte[] buffer6 = new byte[0x10];
                byte[] buffer7 = new byte[0x10];
                byte[] buffer8 = this.calculateBlockKey(i, npd);
                ToolsImpl.aesecbEncrypt(rifkey, buffer8, 0, buffer6, 0, buffer8.Length);
                if ((data.getFlags() & FLAG_0x10) != 0L)
                {
                    ToolsImpl.aesecbEncrypt(rifkey, buffer6, 0, buffer7, 0, buffer6.Length);
                }
                else
                {
                    ConversionUtils.arraycopy(buffer6, 0, buffer7, 0L, buffer6.Length);
                }
                int cryptoFlag = ((data.getFlags() & FLAG_0x02) == 0L) ? 2 : 1;
                if ((data.getFlags() & FLAG_0x10) == 0L)
                {
                    num11 = 2;
                }
                else if ((data.getFlags() & FLAG_0x20) == 0L)
                {
                    num11 = 4;
                }
                else
                {
                    num11 = 1;
                }
                if ((data.getFlags() & FLAG_KEYENCRYPTED) != 0L)
                {
                    cryptoFlag |= 0x10000000;
                    num11      |= 0x10000000;
                }
                if ((data.getFlags() & FLAG_DEBUG) != 0L)
                {
                    cryptoFlag |= 0x1000000;
                    num11      |= 0x1000000;
                }
                AppLoader loader  = new AppLoader();
                byte[]    buffer9 = (npd.getVersion() <= 1L) ? new byte[0x10] : npd.getDigest();
                if (!loader.doAll(num11, cryptoFlag, buffer, 0, buffer5, 0, buffer.Length, buffer6, npd.getDigest(), buffer7, dest, 0))
                {
                    Debug.WriteLine("Error decrypting block " + i);
                }
                if ((data.getFlags() & FLAG_COMPRESSED) == 0L)
                {
                    o.Write(buffer5, 0, count);
                }
            }
            return(STATUS_OK);
        }