Beispiel #1
0
        /* KDSBEST START */
        public int encryptFile(String inFile, String outFile, byte[] devKLic, byte[] keyFromRif, byte[] contentID, byte[] flags, byte[] type, byte[] version)
        {
            FileStream fin = File.Open(inFile, FileMode.Open);

            //  MemoryMappedFile fin1 = MemoryMappedFile.CreateFromFile(inFile, FileMode.Open);
            NPD[]      ptr = new NPD[1]; //Ptr to Ptr
            FileStream o   = File.Open(outFile, FileMode.Create);

            string[] fn  = o.Name.Split('\\');
            byte[]   npd = writeValidNPD(fn[fn.Length - 1], devKLic, ptr, fin, contentID, flags, version, type);
            o.Write(npd, 0, npd.Length);
            byte[] buffer = new byte[4];
            // FLAGS
            buffer[0] = 0x00;
            buffer[1] = 0x00;
            buffer[2] = 0x00;
            buffer[3] = 0x00;
            o.Write(buffer, 0, 4);

            // blocksize 0x00004000
            buffer[2] = 0x40;
            o.Write(buffer, 0, 4);
            long len = fin.Length;

            byte[] lenBuf  = BitConverter.GetBytes(len);
            byte[] rLenBuf = new byte[8];
            for (int i = 0; i < 8; i++)
            {
                rLenBuf[i] = 0x00;
            }
            for (int i = 0; i < lenBuf.Length; i++)
            {
                rLenBuf[7 - i] = lenBuf[i];
            }
            o.Write(rLenBuf, 0, 8);

            // Fill the rest 0x10 bytes with dummy we generate the metasection hash later!
            // the bytes till 0x100 are unknown
            buffer[0] = 0x00;
            while (o.Length < 0x100)
            {
                o.Write(buffer, 0, 1);
            }

            EDATData data = new EDATData();

            data.flags     = 0x00000000;
            data.blockSize = 0x00004000;
            data.fileLen   = new BigInteger(len);
            byte[] rifkey   = getKey(ptr[0], data, devKLic, keyFromRif); //Obtain the key for decryption (result of sc471 or sdatkey)
            int    hashFlag = 0x00000002;


            encryptData(fin, o, ptr[0], data, rifkey);

            o.Seek(0x90, SeekOrigin.Begin);
            AppLoader aa = new AppLoader();

            aa.doInit(hashFlag, 0x00000001, new byte[0x10], new byte[0x10], rifkey);

            int sectionSize = ((data.getFlags() & FLAG_COMPRESSED) != 0) ? 0x20 : 0x010; //BUG??? What about FLAG0x20??
            //Determine the metadatasection total len
            int numBlocks = (int)((data.getFileLen() + data.getBlockSize() - 11) / data.getBlockSize());

            int readed     = 0;
            int baseOffset = 0x100;
            //baseOffset +=  modifier; //There is an unknown offset to add to the metadatasection... value seen 0
            long remaining = sectionSize * numBlocks;

            while (remaining > 0)
            {
                int lenToRead = (HEADER_MAX_BLOCKSIZE > remaining) ? (int)remaining : HEADER_MAX_BLOCKSIZE;
                o.Seek(baseOffset + readed, SeekOrigin.Begin);
                byte[] content = new byte[lenToRead];
                byte[] ooo     = new byte[lenToRead];
                o.Read(content, 0, content.Length);
                aa.doUpdate(content, 0, ooo, 0, lenToRead);
                readed    += lenToRead;
                remaining -= lenToRead;
            }
            byte[] headerHash = new byte[0x10];
            aa.doFinalButGetHash(headerHash);
            o.Seek(0x90, SeekOrigin.Begin);
            o.Write(headerHash, 0, headerHash.Length);

            // Header Complete

            // Generate Header Hash
            o.Seek(0, SeekOrigin.Begin);
            byte[] header       = new byte[0xA0];
            byte[] headerODummy = new byte[0xA0];
            o.Read(header, 0, header.Length);
            AppLoaderReverse a = new AppLoaderReverse();

            byte[] generatedHash = new byte[0x10];
            bool   result        = a.doAll(hashFlag, 0x00000001, header, 0, headerODummy, 0, header.Length, new byte[0x10], new byte[0x10], rifkey, generatedHash, 0);

            o.Seek(0xA0, SeekOrigin.Begin);
            o.Write(generatedHash, 0, generatedHash.Length);


            //KDSBest We don't know the DATA 0xB0 to 0x100!!!

            while (o.Length < 0x100)
            {
                o.Write(buffer, 0, 1);
            }


            o.Close();
            fin.Close();
            return(STATUS_OK);
        }
Beispiel #2
0
 public int encryptFile(string inFile, string outFile, byte[] devKLic, byte[] keyFromRif, byte[] contentID, byte[] flags, byte[] type, byte[] version)
 {
     int num2;
     int num9;
     FileStream fin = File.Open(inFile, FileMode.Open);
     NPD[] npdPtr = new NPD[1];
     FileStream o = File.Open(outFile, FileMode.Create);
     string[] strArray = o.Name.Split(new char[] { '\\' });
     byte[] buffer = this.writeValidNPD(strArray[strArray.Length - 1], devKLic, npdPtr, fin, contentID, flags, version, type);
     o.Write(buffer, 0, buffer.Length);
     byte[] buffer2 = new byte[] { 0, 0, 0, 0 };
     o.Write(buffer2, 0, 4);
     buffer2[2] = 0x40;
     o.Write(buffer2, 0, 4);
     long length = fin.Length;
     byte[] bytes = BitConverter.GetBytes(length);
     byte[] buffer4 = new byte[8];
     for (num2 = 0; num2 < 8; num2++)
     {
         buffer4[num2] = 0;
     }
     for (num2 = 0; num2 < bytes.Length; num2++)
     {
         buffer4[7 - num2] = bytes[num2];
     }
     o.Write(buffer4, 0, 8);
     buffer2[0] = 0;
     while (o.Length < 0x100L)
     {
         o.Write(buffer2, 0, 1);
     }
     EDATData data = new EDATData {
         flags = 0L,
         blockSize = 0x4000L,
         fileLen = new BigInteger(length)
     };
     byte[] rifkey = this.getKey(npdPtr[0], data, devKLic, keyFromRif);
     int hashFlag = 2;
     this.encryptData(fin, o, npdPtr[0], data, rifkey);
     o.Seek(0x90L, SeekOrigin.Begin);
     AppLoader loader = new AppLoader();
     loader.doInit(hashFlag, 1, new byte[0x10], new byte[0x10], rifkey);
     int num4 = ((data.getFlags() & FLAG_COMPRESSED) != 0L) ? 0x20 : 0x10;
     int num5 = (int) (((data.getFileLen() + data.getBlockSize()) - 11) / data.getBlockSize());
     int num6 = 0;
     int num7 = 0x100;
     for (long i = num4 * num5; i > 0L; i -= num9)
     {
         num9 = (HEADER_MAX_BLOCKSIZE > i) ? ((int) i) : HEADER_MAX_BLOCKSIZE;
         o.Seek((long) (num7 + num6), SeekOrigin.Begin);
         byte[] buffer6 = new byte[num9];
         byte[] buffer7 = new byte[num9];
         o.Read(buffer6, 0, buffer6.Length);
         loader.doUpdate(buffer6, 0, buffer7, 0, num9);
         num6 += num9;
     }
     byte[] generatedHash = new byte[0x10];
     loader.doFinalButGetHash(generatedHash);
     o.Seek(0x90L, SeekOrigin.Begin);
     o.Write(generatedHash, 0, generatedHash.Length);
     o.Seek(0L, SeekOrigin.Begin);
     byte[] buffer9 = new byte[160];
     byte[] buffer10 = new byte[160];
     o.Read(buffer9, 0, buffer9.Length);
     AppLoaderReverse reverse = new AppLoaderReverse();
     byte[] buffer11 = new byte[0x10];
     bool flag = reverse.doAll(hashFlag, 1, buffer9, 0, buffer10, 0, buffer9.Length, new byte[0x10], new byte[0x10], rifkey, buffer11, 0);
     o.Seek(160L, SeekOrigin.Begin);
     o.Write(buffer11, 0, buffer11.Length);
     while (o.Length < 0x100L)
     {
         o.Write(buffer2, 0, 1);
     }
     o.Close();
     fin.Close();
     return STATUS_OK;
 }
Beispiel #3
0
        private int checkHeader(byte[] rifKey, EDATData data, NPD npd, FileStream i)
        {
            i.Seek(0, SeekOrigin.Begin);
            byte[] header       = new byte[0xA0];
            byte[] o            = new byte[0xA0];
            byte[] expectedHash = new byte[0x10];
            //Version check
            Console.WriteLine("Checking NPD Version:" + npd.getVersion());
            if ((npd.getVersion() == 0) || (npd.getVersion() == 1))
            {
                if ((data.getFlags() & 0x7FFFFFFE) != 0)
                {
                    Console.WriteLine("ERROR: Incorrect Header Flags"); return(STATUS_ERROR_INCORRECT_FLAGS);
                }
            }
            else if (npd.getVersion() == 2)
            {
                if ((data.getFlags() & 0x7EFFFFE0) != 0)
                {
                    Console.WriteLine("ERROR: Incorrect Header Flags"); return(STATUS_ERROR_INCORRECT_FLAGS);
                }
            }
            else if (npd.getVersion() == 3 || (npd.getVersion() == 4))
            {
                if ((data.getFlags() & 0x7EFFFFC0) != 0)
                {
                    Console.WriteLine("ERROR: Incorrect Header Flags"); return(STATUS_ERROR_INCORRECT_FLAGS);
                }
            }
            else
            {
                Console.WriteLine("ERROR: Unsupported EDAT version (need keys)"); return(STATUS_ERROR_INCORRECT_VERSION);
            }

            {
                int keyIndex = 0;
                if (npd.getVersion() == 4)
                {
                    keyIndex = 1;
                }


                i.Read(header, 0, header.Length);
                i.Read(expectedHash, 0, expectedHash.Length);
                Console.WriteLine("Checking header hash:");
                AppLoader a        = new AppLoader();
                int       hashFlag = ((data.getFlags() & FLAG_KEYENCRYPTED) == 0) ? 0x00000002 : 0x10000002;
                if ((data.getFlags() & FLAG_DEBUG) != 0)
                {
                    hashFlag |= 0x01000000;
                }



                //Veryfing header
                bool result = a.doAll(hashFlag, 0x00000001, header, 0, o, 0, header.Length, new byte[0x10], new byte[0x10], rifKey, expectedHash, 0);
                if (!result)
                {
                    Console.WriteLine("Error verifying header. Is rifKey valid?.");
                    return(STATUS_ERROR_HEADERCHECK);
                }
                Console.WriteLine("Checking metadata hash:");
                a = new AppLoader();
                a.doInit(hashFlag, 0x00000001, new byte[0x10], new byte[0x10], rifKey);

                int sectionSize = ((data.getFlags() & FLAG_COMPRESSED) != 0) ? 0x20 : 0x010; //BUG??? What about FLAG0x20??
                //Determine the metadatasection total len
                int numBlocks = (int)((data.getFileLen() + data.getBlockSize() - 11) / data.getBlockSize());

                int readed     = 0;
                int baseOffset = 0x100;
                //baseOffset +=  modifier; //There is an unknown offset to add to the metadatasection... value seen 0
                long remaining = sectionSize * numBlocks;
                while (remaining > 0)
                {
                    int lenToRead = (HEADER_MAX_BLOCKSIZE > remaining) ? (int)remaining : HEADER_MAX_BLOCKSIZE;
                    i.Seek(baseOffset + readed, SeekOrigin.Begin);
                    byte[] content = new byte[lenToRead];
                    o = new byte[lenToRead];
                    i.Read(content, 0, content.Length);
                    a.doUpdate(content, 0, o, 0, lenToRead);
                    readed    += lenToRead;
                    remaining -= lenToRead;
                }
                result = a.doFinal(header, 0x90);


                if (!result)
                {
                    Console.WriteLine("Error verifying metadatasection. Data tampered");
                    return(STATUS_ERROR_HEADERCHECK);
                }
                return(STATUS_OK);
            }
        }
Beispiel #4
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;
 }
Beispiel #5
0
 private int checkHeader(byte[] rifKey, EDATData data, NPD npd, FileStream i)
 {
     int num8;
     i.Seek(0L, SeekOrigin.Begin);
     byte[] buffer = new byte[160];
     byte[] o = new byte[160];
     byte[] buffer3 = new byte[0x10];
     Console.WriteLine("Checking NPD Version:" + npd.getVersion());
     if ((npd.getVersion() == 0L) || (npd.getVersion() == 1L))
     {
         if ((data.getFlags() & 0x7ffffffeL) != 0L)
         {
             Console.WriteLine("ERROR: Incorrect Header Flags");
             return STATUS_ERROR_INCORRECT_FLAGS;
         }
     }
     else if (npd.getVersion() == 2L)
     {
         if ((data.getFlags() & 0x7effffe0L) != 0L)
         {
             Console.WriteLine("ERROR: Incorrect Header Flags");
             return STATUS_ERROR_INCORRECT_FLAGS;
         }
     }
     else if ((npd.getVersion() == 3L) || (npd.getVersion() == 4L))
     {
         if ((data.getFlags() & 0x7effffc0L) != 0L)
         {
             Console.WriteLine("ERROR: Incorrect Header Flags");
             return STATUS_ERROR_INCORRECT_FLAGS;
         }
     }
     else
     {
         Console.WriteLine("ERROR: Unsupported EDAT version (need keys)");
         return STATUS_ERROR_INCORRECT_VERSION;
     }
     if (npd.getVersion() == 4L)
     {
     }
     i.Read(buffer, 0, buffer.Length);
     i.Read(buffer3, 0, buffer3.Length);
     Console.WriteLine("Checking header hash:");
     AppLoader loader = new AppLoader();
     int hashFlag = ((data.getFlags() & FLAG_KEYENCRYPTED) == 0L) ? 2 : 0x10000002;
     if ((data.getFlags() & FLAG_DEBUG) != 0L)
     {
         hashFlag |= 0x1000000;
     }
     if (!loader.doAll(hashFlag, 1, buffer, 0, o, 0, buffer.Length, new byte[0x10], new byte[0x10], rifKey, buffer3, 0))
     {
         Console.WriteLine("Error verifying header. Is rifKey valid?.");
         return STATUS_ERROR_HEADERCHECK;
     }
     Console.WriteLine("Checking metadata hash:");
     loader = new AppLoader();
     loader.doInit(hashFlag, 1, new byte[0x10], new byte[0x10], rifKey);
     int num3 = ((data.getFlags() & FLAG_COMPRESSED) != 0L) ? 0x20 : 0x10;
     int num4 = (int) (((data.getFileLen() + data.getBlockSize()) - 11) / data.getBlockSize());
     int num5 = 0;
     int num6 = 0x100;
     for (long j = num3 * num4; j > 0L; j -= num8)
     {
         num8 = (HEADER_MAX_BLOCKSIZE > j) ? ((int) j) : HEADER_MAX_BLOCKSIZE;
         i.Seek((long) (num6 + num5), SeekOrigin.Begin);
         byte[] buffer4 = new byte[num8];
         o = new byte[num8];
         i.Read(buffer4, 0, buffer4.Length);
         loader.doUpdate(buffer4, 0, o, 0, num8);
         num5 += num8;
     }
     if (!loader.doFinal(buffer, 0x90))
     {
         Console.WriteLine("Error verifying metadatasection. Data tampered");
         return STATUS_ERROR_HEADERCHECK;
     }
     return STATUS_OK;
 }
Beispiel #6
0
        private int checkHeader(byte[] rifKey, EDATData data, NPD npd, FileStream i)
        {
            int num8;

            i.Seek(0L, SeekOrigin.Begin);
            byte[] buffer  = new byte[160];
            byte[] o       = new byte[160];
            byte[] buffer3 = new byte[0x10];
            Console.WriteLine("Checking NPD Version:" + npd.getVersion());
            if ((npd.getVersion() == 0L) || (npd.getVersion() == 1L))
            {
                if ((data.getFlags() & 0x7ffffffeL) != 0L)
                {
                    Console.WriteLine("ERROR: Incorrect Header Flags");
                    return(STATUS_ERROR_INCORRECT_FLAGS);
                }
            }
            else if (npd.getVersion() == 2L)
            {
                if ((data.getFlags() & 0x7effffe0L) != 0L)
                {
                    Console.WriteLine("ERROR: Incorrect Header Flags");
                    return(STATUS_ERROR_INCORRECT_FLAGS);
                }
            }
            else if ((npd.getVersion() == 3L) || (npd.getVersion() == 4L))
            {
                if ((data.getFlags() & 0x7effffc0L) != 0L)
                {
                    Console.WriteLine("ERROR: Incorrect Header Flags");
                    return(STATUS_ERROR_INCORRECT_FLAGS);
                }
            }
            else
            {
                Console.WriteLine("ERROR: Unsupported EDAT version (need keys)");
                return(STATUS_ERROR_INCORRECT_VERSION);
            }
            if (npd.getVersion() == 4L)
            {
            }
            i.Read(buffer, 0, buffer.Length);
            i.Read(buffer3, 0, buffer3.Length);
            Console.WriteLine("Checking header hash:");
            AppLoader loader   = new AppLoader();
            int       hashFlag = ((data.getFlags() & FLAG_KEYENCRYPTED) == 0L) ? 2 : 0x10000002;

            if ((data.getFlags() & FLAG_DEBUG) != 0L)
            {
                hashFlag |= 0x1000000;
            }
            if (!loader.doAll(hashFlag, 1, buffer, 0, o, 0, buffer.Length, new byte[0x10], new byte[0x10], rifKey, buffer3, 0))
            {
                Console.WriteLine("Error verifying header. Is rifKey valid?.");
                return(STATUS_ERROR_HEADERCHECK);
            }
            Console.WriteLine("Checking metadata hash:");
            loader = new AppLoader();
            loader.doInit(hashFlag, 1, new byte[0x10], new byte[0x10], rifKey);
            int num3 = ((data.getFlags() & FLAG_COMPRESSED) != 0L) ? 0x20 : 0x10;
            int num4 = (int)(((data.getFileLen() + data.getBlockSize()) - 11) / data.getBlockSize());
            int num5 = 0;
            int num6 = 0x100;

            for (long j = num3 * num4; j > 0L; j -= num8)
            {
                num8 = (HEADER_MAX_BLOCKSIZE > j) ? ((int)j) : HEADER_MAX_BLOCKSIZE;
                i.Seek((long)(num6 + num5), SeekOrigin.Begin);
                byte[] buffer4 = new byte[num8];
                o = new byte[num8];
                i.Read(buffer4, 0, buffer4.Length);
                loader.doUpdate(buffer4, 0, o, 0, num8);
                num5 += num8;
            }
            if (!loader.doFinal(buffer, 0x90))
            {
                Console.WriteLine("Error verifying metadatasection. Data tampered");
                return(STATUS_ERROR_HEADERCHECK);
            }
            return(STATUS_OK);
        }
Beispiel #7
0
        public int encryptFile(string inFile, string outFile, byte[] devKLic, byte[] keyFromRif, byte[] contentID, byte[] flags, byte[] type, byte[] version)
        {
            int        num2;
            int        num9;
            FileStream fin = File.Open(inFile, FileMode.Open);

            NPD[]      npdPtr = new NPD[1];
            FileStream o      = File.Open(outFile, FileMode.Create);

            string[] strArray = o.Name.Split(new char[] { '\\' });
            byte[]   buffer   = this.writeValidNPD(strArray[strArray.Length - 1], devKLic, npdPtr, fin, contentID, flags, version, type);
            o.Write(buffer, 0, buffer.Length);
            byte[] buffer2 = new byte[] { 0, 0, 0, 0 };
            o.Write(buffer2, 0, 4);
            buffer2[2] = 0x40;
            o.Write(buffer2, 0, 4);
            long length = fin.Length;

            byte[] bytes   = BitConverter.GetBytes(length);
            byte[] buffer4 = new byte[8];
            for (num2 = 0; num2 < 8; num2++)
            {
                buffer4[num2] = 0;
            }
            for (num2 = 0; num2 < bytes.Length; num2++)
            {
                buffer4[7 - num2] = bytes[num2];
            }
            o.Write(buffer4, 0, 8);
            buffer2[0] = 0;
            while (o.Length < 0x100L)
            {
                o.Write(buffer2, 0, 1);
            }
            EDATData data = new EDATData {
                flags     = 0L,
                blockSize = 0x4000L,
                fileLen   = new BigInteger(length)
            };

            byte[] rifkey   = this.getKey(npdPtr[0], data, devKLic, keyFromRif);
            int    hashFlag = 2;

            this.encryptData(fin, o, npdPtr[0], data, rifkey);
            o.Seek(0x90L, SeekOrigin.Begin);
            AppLoader loader = new AppLoader();

            loader.doInit(hashFlag, 1, new byte[0x10], new byte[0x10], rifkey);
            int num4 = ((data.getFlags() & FLAG_COMPRESSED) != 0L) ? 0x20 : 0x10;
            int num5 = (int)(((data.getFileLen() + data.getBlockSize()) - 11) / data.getBlockSize());
            int num6 = 0;
            int num7 = 0x100;

            for (long i = num4 * num5; i > 0L; i -= num9)
            {
                num9 = (HEADER_MAX_BLOCKSIZE > i) ? ((int)i) : HEADER_MAX_BLOCKSIZE;
                o.Seek((long)(num7 + num6), SeekOrigin.Begin);
                byte[] buffer6 = new byte[num9];
                byte[] buffer7 = new byte[num9];
                o.Read(buffer6, 0, buffer6.Length);
                loader.doUpdate(buffer6, 0, buffer7, 0, num9);
                num6 += num9;
            }
            byte[] generatedHash = new byte[0x10];
            loader.doFinalButGetHash(generatedHash);
            o.Seek(0x90L, SeekOrigin.Begin);
            o.Write(generatedHash, 0, generatedHash.Length);
            o.Seek(0L, SeekOrigin.Begin);
            byte[] buffer9  = new byte[160];
            byte[] buffer10 = new byte[160];
            o.Read(buffer9, 0, buffer9.Length);
            AppLoaderReverse reverse = new AppLoaderReverse();

            byte[] buffer11 = new byte[0x10];
            bool   flag     = reverse.doAll(hashFlag, 1, buffer9, 0, buffer10, 0, buffer9.Length, new byte[0x10], new byte[0x10], rifkey, buffer11, 0);

            o.Seek(160L, SeekOrigin.Begin);
            o.Write(buffer11, 0, buffer11.Length);
            while (o.Length < 0x100L)
            {
                o.Write(buffer2, 0, 1);
            }
            o.Close();
            fin.Close();
            return(STATUS_OK);
        }
Beispiel #8
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);
        }