示例#1
0
        public override void doUpdate(byte[] i, int inOffset, int len)
        {
            byte[] data;
            if (nonProcessed != null)
            {
                int totalLen = len + nonProcessed.Length;
                data = new byte[totalLen];
                ConversionUtils.arraycopy(nonProcessed, 0, data, 0, nonProcessed.Length);
                ConversionUtils.arraycopy(i, inOffset, data, nonProcessed.Length, len);
            }
            else
            {
                data = new byte[len];
                ConversionUtils.arraycopy(i, inOffset, data, 0, len);
            }
            int count = 0;

            while (count < data.Length - 0x10)
            {
                byte[] aux = new byte[0x10];
                ConversionUtils.arraycopy(data, count, aux, 0, aux.Length);
                ToolsImpl.XOR(aux, aux, previous);
                ToolsImpl.aesecbEncrypt(key, aux, 0, previous, 0, aux.Length);
                count += 0x10;
            }
            nonProcessed = new byte[data.Length - count];
            ConversionUtils.arraycopy(data, count, nonProcessed, 0, nonProcessed.Length);
        }
示例#2
0
        public override void doUpdate(byte[] i, int inOffset, int len)
        {
            byte[] buffer;
            if (this.nonProcessed != null)
            {
                int num = len + this.nonProcessed.Length;
                buffer = new byte[num];
                ConversionUtils.arraycopy(this.nonProcessed, 0, buffer, 0L, this.nonProcessed.Length);
                ConversionUtils.arraycopy(i, inOffset, buffer, (long)this.nonProcessed.Length, len);
            }
            else
            {
                buffer = new byte[len];
                ConversionUtils.arraycopy(i, inOffset, buffer, 0L, len);
            }
            int srcPos = 0;

            while (srcPos < (buffer.Length - 0x10))
            {
                byte[] dest = new byte[0x10];
                ConversionUtils.arraycopy(buffer, srcPos, dest, 0L, dest.Length);
                ToolsImpl.XOR(dest, dest, this.previous);
                ToolsImpl.aesecbEncrypt(this.key, dest, 0, this.previous, 0, dest.Length);
                srcPos += 0x10;
            }
            this.nonProcessed = new byte[buffer.Length - srcPos];
            ConversionUtils.arraycopy(buffer, srcPos, this.nonProcessed, 0L, this.nonProcessed.Length);
        }
示例#3
0
        private static byte[] getPerConsoleKey(String IDPSFile)
        {
            FileStream raf = File.Open(IDPSFile, FileMode.Open);

            byte[] idps = new byte[0x10];
            raf.Read(idps, 0, idps.Length);
            raf.Close();
            byte[] result = new byte[0x10];
            ToolsImpl.aesecbEncrypt(idps, ACTDAT_KEY, 0, result, 0, ACTDAT_KEY.Length);
            return(result);
        }
示例#4
0
        private static byte[] getPerConsoleKey(string IDPSFile)
        {
            FileStream stream = File.Open(IDPSFile, FileMode.Open);

            byte[] buffer = new byte[0x10];
            stream.Read(buffer, 0, buffer.Length);
            stream.Close();
            byte[] o = new byte[0x10];
            ToolsImpl.aesecbEncrypt(buffer, ACTDAT_KEY, 0, o, 0, ACTDAT_KEY.Length);
            return(o);
        }
        private static void calculateSubkey(byte[] key, byte[] K1, byte[] K2)
        {
            byte[] zero = new byte[0x10];
            byte[] L    = new byte[0x10];
            ToolsImpl.aesecbEncrypt(key, zero, 0, L, 0, zero.Length);
            BigInteger aux = new BigInteger(ConversionUtils.reverseByteWithSizeFIX(L));

            if ((L[0] & 0x80) != 0)
            {
                //Case MSB is set
                aux = (aux << 1) ^ (new BigInteger(0x87));
            }
            else
            {
                aux = aux << 1;
            }
            byte[] aux1 = ConversionUtils.reverseByteWithSizeFIX(aux.ToByteArray());
            if (aux1.Length >= 0x10)
            {
                ConversionUtils.arraycopy(aux1, aux1.Length - 0x10, K1, 0, 0x10);
            }
            else
            {
                ConversionUtils.arraycopy(zero, 0, K1, 0, zero.Length);
                ConversionUtils.arraycopy(aux1, 0, K1, 0x10 - aux1.Length, aux1.Length);
            }
            aux = new BigInteger(ConversionUtils.reverseByteWithSizeFIX(K1));

            if ((K1[0] & 0x80) != 0)
            {
                aux = (aux << 1) ^ (new BigInteger(0x87));
            }
            else
            {
                aux = aux << 1;
            }
            aux1 = ConversionUtils.reverseByteWithSizeFIX(aux.ToByteArray());
            if (aux1.Length >= 0x10)
            {
                ConversionUtils.arraycopy(aux1, aux1.Length - 0x10, K2, 0, 0x10);
            }
            else
            {
                ConversionUtils.arraycopy(zero, 0, K2, 0, zero.Length);
                ConversionUtils.arraycopy(aux1, 0, K2, 0x10 - aux1.Length, aux1.Length);
            }
        }
示例#6
0
 public override bool doFinal(byte[] generateHash)
 {
     byte[] aux = new byte[0x10];
     ConversionUtils.arraycopy(nonProcessed, 0, aux, 0, nonProcessed.Length);
     if (nonProcessed.Length == 0x10)
     {
         ToolsImpl.XOR(aux, aux, K1);
     }
     else
     {
         aux[nonProcessed.Length] = (byte)0x80;
         ToolsImpl.XOR(aux, aux, K2);
     }
     ToolsImpl.XOR(aux, aux, previous);
     ToolsImpl.aesecbEncrypt(key, aux, 0, generateHash, 0, aux.Length);
     return(true);
 }
示例#7
0
 public override bool doFinal(byte[] generateHash)
 {
     byte[] dest = new byte[0x10];
     ConversionUtils.arraycopy(this.nonProcessed, 0, dest, 0L, this.nonProcessed.Length);
     if (this.nonProcessed.Length == 0x10)
     {
         ToolsImpl.XOR(dest, dest, this.K1);
     }
     else
     {
         dest[this.nonProcessed.Length] = 0x80;
         ToolsImpl.XOR(dest, dest, this.K2);
     }
     ToolsImpl.XOR(dest, dest, this.previous);
     ToolsImpl.aesecbEncrypt(this.key, dest, 0, generateHash, 0, dest.Length);
     return(true);
 }
示例#8
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);
        }
示例#9
0
        private void calculateSubkey(byte[] key, byte[] K1, byte[] K2)
        {
            byte[] i = new byte[0x10];
            byte[] o = new byte[0x10];
            ToolsImpl.aesecbEncrypt(key, i, 0, o, 0, i.Length);
            BigInteger integer = new BigInteger(ConversionUtils.reverseByteWithSizeFIX(o));

            if ((o[0] & 0x80) != 0)
            {
                integer = (integer << 1) ^ new BigInteger(0x87);
            }
            else
            {
                integer = integer << 1;
            }
            byte[] src = ConversionUtils.reverseByteWithSizeFIX(integer.ToByteArray());
            if (src.Length >= 0x10)
            {
                ConversionUtils.arraycopy(src, src.Length - 0x10, K1, 0L, 0x10);
            }
            else
            {
                ConversionUtils.arraycopy(i, 0, K1, 0L, i.Length);
                ConversionUtils.arraycopy(src, 0, K1, (long)(0x10 - src.Length), src.Length);
            }
            integer = new BigInteger(ConversionUtils.reverseByteWithSizeFIX(K1));
            if ((K1[0] & 0x80) != 0)
            {
                integer = (integer << 1) ^ new BigInteger(0x87);
            }
            else
            {
                integer = integer << 1;
            }
            src = ConversionUtils.reverseByteWithSizeFIX(integer.ToByteArray());
            if (src.Length >= 0x10)
            {
                ConversionUtils.arraycopy(src, src.Length - 0x10, K2, 0L, 0x10);
            }
            else
            {
                ConversionUtils.arraycopy(i, 0, K2, 0L, i.Length);
                ConversionUtils.arraycopy(src, 0, K2, (long)(0x10 - src.Length), src.Length);
            }
        }
示例#10
0
 public override bool doFinal(byte[] expectedhash, int hashOffset, bool hashDebug)
 {
     byte[] aux = new byte[0x10];
     ConversionUtils.arraycopy(nonProcessed, 0, aux, 0, nonProcessed.Length);
     if (nonProcessed.Length == 0x10)
     {
         ToolsImpl.XOR(aux, aux, K1);
     }
     else
     {
         aux[nonProcessed.Length] = (byte)0x80;
         ToolsImpl.XOR(aux, aux, K2);
     }
     ToolsImpl.XOR(aux, aux, previous);
     byte[] calculatedhash = new byte[0x10];
     ToolsImpl.aesecbEncrypt(key, aux, 0, calculatedhash, 0, aux.Length);
     return(hashDebug || compareBytes(expectedhash, hashOffset, calculatedhash, 0, hashLen));
 }
示例#11
0
 public override bool doFinal(byte[] expectedhash, int hashOffset, bool hashDebug)
 {
     byte[] dest = new byte[0x10];
     ConversionUtils.arraycopy(this.nonProcessed, 0, dest, 0L, this.nonProcessed.Length);
     if (this.nonProcessed.Length == 0x10)
     {
         ToolsImpl.XOR(dest, dest, this.K1);
     }
     else
     {
         dest[this.nonProcessed.Length] = 0x80;
         ToolsImpl.XOR(dest, dest, this.K2);
     }
     ToolsImpl.XOR(dest, dest, this.previous);
     byte[] o = new byte[0x10];
     ToolsImpl.aesecbEncrypt(this.key, dest, 0, o, 0, dest.Length);
     return(hashDebug || base.compareBytes(expectedhash, hashOffset, o, 0, this.hashLen));
 }
        public static byte[] CMAC128(byte[] key, byte[] i, int inOffset, int len)
        {
            byte[] K1 = new byte[0x10];
            byte[] K2 = new byte[0x10];
            calculateSubkey(key, K1, K2);
            byte[] input         = new byte[0x10];
            byte[] previous      = new byte[0x10];
            int    currentOffset = inOffset;
            int    remaining     = len;

            //System.out.println(ConversionUtils.getHexString(key));
            while (remaining > 0x10)
            {
                ConversionUtils.arraycopy(i, currentOffset, input, 0, 0x10);
                //System.out.println(ConversionUtils.getHexString(previous) + " - " + ConversionUtils.getHexString(input));
                XOR(input, input, previous);

                ToolsImpl.aesecbEncrypt(key, input, 0, previous, 0, input.Length);
                currentOffset += 0x10;
                remaining     -= 0x10;
            }
            //System.out.println(ConversionUtils.getHexString(previous));
            input = new byte[0x10]; //Memset 0
            ConversionUtils.arraycopy(i, currentOffset, input, 0, remaining);
            if (remaining == 0x10)
            {
                XOR(input, input, previous);
                XOR(input, input, K1);
            }
            else
            {
                input[remaining] = (byte)0x80;
                XOR(input, input, previous);
                XOR(input, input, K2);
            }
            //System.out.println(" - " + ConversionUtils.getHexString(input));
            ToolsImpl.aesecbEncrypt(key, input, 0, previous, 0, input.Length);
            return(previous);
        }
示例#13
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);
        }
示例#14
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);
        }
示例#15
0
        public string makerif(String inFile, String outFile)
        {
            if (!File.Exists(inFile))
            {
                Console.WriteLine(inFile + " not found");
                return(inFile);
            }
            else
            {
                String strAppDir = Path.GetDirectoryName(
                    Assembly.GetExecutingAssembly().GetName().CodeBase);
                string path   = strAppDir.Replace("file:\\", "");
                string actIn1 = strAppDir + "/data/act.dat";
                string idps1  = strAppDir + "/data/idps";
                string actIn  = actIn1.Replace("file:\\", "");
                string idps   = idps1.Replace("file:\\", "");

                FileStream actFile = File.Open(actIn, FileMode.Open);
                byte[]     actid   = new byte[0x08];
                actFile.Seek(0x08, SeekOrigin.Begin);
                actFile.Read(actid, 0, 0x8);
                actFile.Close();

                string cid = GetSubstringByString('\\', ".", inFile);

                if (path != null)
                {
                    outFile = path + "/rifs/" + cid + ".rif";
                }
                else
                {
                    outFile = "rifs/" + cid + ".rif";
                }

                byte[] content_id = new byte[0x30];
                byte[] encrif0x40 = new byte[0x10];
                byte[] rif0x40    = new byte[0x10];
                byte[] rif0x50    = null;

                byte[]        keyFromRif = getKey(inFile);
                DirectoryInfo di         = Directory.CreateDirectory(path + "/rifs");

                FileStream o = File.Open(outFile, FileMode.Create);

                o.Write(rif_header, 0, 0x08);
                o.Write(actid, 0, 0x08);
                byte[] CID = Encoding.UTF8.GetBytes(cid);
                ConversionUtils.arraycopy(CID, 0, content_id, 0, CID.Length);

                o.Write(content_id, 0, 0x30);

                ToolsImpl.aesecbEncrypt(RIFKEY, rif0x40, 0x00, encrif0x40, 0, 0x10);
                o.Write(encrif0x40, 0, 0x10);
                long   index  = 0;
                byte[] actDat = decryptACTDAT(actIn, idps);
                byte[] datKey = new byte[0x10];
                rif0x50 = new byte[0x10];
                byte[] signature = new byte[0x28];

                ConversionUtils.arraycopy(actDat, (int)index * 16, datKey, 0, 0x10);
                ToolsImpl.aesecbEncrypt(datKey, keyFromRif, 0, rif0x50, 0, 0x10);
                o.Write(rif0x50, 0, 0x10);
                o.Write(rif_footer, 0, 0x10);

                while (o.Length < 0x98)
                {
                    o.Write(rif_junk, 0, 0x1);
                }

                o.Close();
                return(outFile);
            }
        }
示例#16
0
        public string getKey(String rifIn)
        {
            if (!File.Exists(rifIn))
            {
                Console.WriteLine(rifIn + " not found");
                return(rifIn);
            }
            else
            {
                String strAppDir = Path.GetDirectoryName(
                    Assembly.GetExecutingAssembly().GetName().CodeBase);
                string path   = strAppDir.Replace("file:\\", "");
                string actIn1 = strAppDir + "/data/act.dat";
                string idps1  = strAppDir + "/data/idps";
                string actIn  = actIn1.Replace("file:\\", "");
                string idps   = idps1.Replace("file:\\", "");

                byte[] intermediate = getrifKey(rifIn, actIn, idps);
                for (int loop = 0; loop < 5; loop++)
                {
                    int acum = 0;
                    for (int loop2 = 0; loop2 < 0x10; loop2++)
                    {
                        int  index   = indexTable[loop2];
                        byte current = (byte)(intermediate[index] + acum);
                        if (acum != 1 || current != 0xFF)
                        {
                            int aux1 = (current + key2[index]) & 0xFF;
                            int aux2 = key2[index] & 0xFF;
                            acum = (aux1 < aux2) ? 1 : 0;
                            intermediate[index] = (byte)(current + key2[index]);
                        }
                        else if (current == 0xFF)
                        {
                            intermediate[index] = (byte)(current + key2[index]);
                        }
                        else
                        {
                            intermediate[index] = current;
                        }
                        //  Console.WriteLine(index + " " + current);
                    }
                    for (int loop2 = 1; loop2 < 0x10; loop2++)
                    {
                        int index1 = indexTable[loop2];
                        int index2 = indexTable[loop2 - 1];
                        //intermediate[index1] = (byte)(intermediate[index1] ^ intermediate[index2]);
                        intermediate[index1] = (byte)(intermediate[index2] ^ intermediate[index1]);
                        //  Console.WriteLine(index1 + " " + intermediate[index1]);
                    }
                    for (int loop2 = 0; loop2 < 0x10; loop2++)
                    {
                        int index = indexTable[loop2];
                        // intermediate[index] = (byte)(intermediate[index] ^ key1[index]);
                        intermediate[index] = (byte)(key1[index] ^ intermediate[index]);
                    }
                }
                string cid     = GetSubstringByString('\\', ".", rifIn);
                string outFile = null;
                if (path != null)
                {
                    outFile = path + "/raps/" + cid + ".rap";
                }
                else
                {
                    outFile = "raps/" + cid + ".rap";
                }
                byte[] rk = new byte[0x10];
                ToolsImpl.aesecbEncrypt(rapKey, intermediate, 0, rk, 0, 0x10);
                DirectoryInfo di = Directory.CreateDirectory("raps");
                FileStream    o  = File.Open(outFile, FileMode.Create);
                o.Write(rk, 0, 0x10);

                o.Close();
                if (Directory.Exists("temp"))
                {
                    Directory.Delete("temp", true);
                }

                return(outFile);
            }
        }
示例#17
0
        public string getKey(string rifIn)
        {
            if (!File.Exists(rifIn))
            {
                Console.WriteLine(rifIn + " not found");
                return(rifIn);
            }
            string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            string str2          = directoryName.Replace(@"file:\", "");
            string str3          = directoryName + "/data/act.dat";
            string str4          = directoryName + "/data/idps";
            string actIn         = str3.Replace(@"file:\", "");
            string idps          = str4.Replace(@"file:\", "");

            byte[] i = this.getrifKey(rifIn, actIn, idps);
            for (int j = 0; j < 5; j++)
            {
                int num4;
                int num2  = 0;
                int index = 0;
                while (index < 0x10)
                {
                    num4 = indexTable[index];
                    byte num5 = (byte)(i[num4] + num2);
                    if ((num2 != 1) || (num5 != 0xff))
                    {
                        int num6 = (num5 + key2[num4]) & 0xff;
                        int num7 = key2[num4] & 0xff;
                        num2    = (num6 < num7) ? 1 : 0;
                        i[num4] = (byte)(num5 + key2[num4]);
                    }
                    else if (num5 == 0xff)
                    {
                        i[num4] = (byte)(num5 + key2[num4]);
                    }
                    else
                    {
                        i[num4] = num5;
                    }
                    index++;
                }
                index = 1;
                while (index < 0x10)
                {
                    int num8 = indexTable[index];
                    int num9 = indexTable[index - 1];
                    i[num8] = (byte)(i[num9] ^ i[num8]);
                    index++;
                }
                for (index = 0; index < 0x10; index++)
                {
                    num4    = indexTable[index];
                    i[num4] = (byte)(key1[num4] ^ i[num4]);
                }
            }
            string str7 = this.GetSubstringByString('\\', ".", rifIn);
            string path = null;

            if (str2 != null)
            {
                path = str2 + "/raps/" + str7 + ".rap";
            }
            else
            {
                path = "raps/" + str7 + ".rap";
            }
            byte[] o = new byte[0x10];
            ToolsImpl.aesecbEncrypt(rapKey, i, 0, o, 0, 0x10);
            DirectoryInfo info   = Directory.CreateDirectory("raps");
            FileStream    stream = File.Open(path, FileMode.Create);

            stream.Write(o, 0, 0x10);
            stream.Close();
            if (Directory.Exists("temp"))
            {
                Directory.Delete("temp", true);
            }
            return(path);
        }
示例#18
0
        public string makerap(string rifIn)
        {
            if (!File.Exists(rifIn))
            {
                Console.WriteLine(rifIn + " not found");
                return(rifIn);
            }
            string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            string str2          = directoryName.Replace(@"file:\", "");
            string str3          = directoryName + "/data/act.dat";
            string str4          = directoryName + "/data/idps";
            string actIn         = str3.Replace(@"file:\", "");
            string idps          = str4.Replace(@"file:\", "");

            byte[] i = this.getrifKey(rifIn, actIn, idps);
            for (int j = 0; j < 5; j++)
            {
                int num5;
                int num2  = 0;
                int num3  = 0;
                int index = 0;
                while (index < 0x10)
                {
                    num5 = indexTable[index];
                    byte num6 = (byte)((i[num5] + num2) + num3);
                    if ((num2 != 1) || (num6 != 0xff))
                    {
                        int num7 = (num6 + key2[num5]) & 0xff;
                        int num8 = key2[num5] & 0xff;
                        int num9 = i[num5];
                        num2    = (num7 < num8) ? 1 : 0;
                        num3    = (num9 == 0xff) ? 1 : 0;
                        i[num5] = (byte)(num6 + key2[num5]);
                    }
                    else if (num6 == 0xff)
                    {
                        i[num5] = (byte)(num6 + key2[num5]);
                    }
                    else
                    {
                        i[num5] = num6;
                    }
                    index++;
                }
                index = 1;
                while (index < 0x10)
                {
                    int num10 = indexTable[index];
                    int num11 = indexTable[index - 1];
                    i[num10] = (byte)(i[num11] ^ i[num10]);
                    index++;
                }
                for (index = 0; index < 0x10; index++)
                {
                    num5    = indexTable[index];
                    i[num5] = (byte)(key1[num5] ^ i[num5]);
                }
            }
            string str7 = this.GetSubstringByString('\\', ".", rifIn);
            string path = null;

            if (str2 != null)
            {
                path = str2 + "/raps/" + str7 + ".rap";
            }
            else
            {
                path = "raps/" + str7 + ".rap";
            }
            byte[] o = new byte[0x10];
            ToolsImpl.aesecbEncrypt(rapKey, i, 0, o, 0, 0x10);
            DirectoryInfo info   = Directory.CreateDirectory(str2 + "/raps");
            FileStream    stream = File.Open(path, FileMode.Create);

            stream.Write(o, 0, 0x10);
            stream.Close();
            while (this.i == 0)
            {
                string outFile = "test.rif";
                this.rif = new edatat.raptest().makerif(path, outFile);
                this.i++;
            }
            int num12 = 0;

            byte[] buffer3 = this.getrifKey(rifIn, actIn, idps);
            byte[] buffer4 = this.getrifKey(this.rif, actIn, idps);
            if ((((((buffer3[0] != buffer4[0]) || (buffer3[1] != buffer4[1])) || ((buffer3[2] != buffer4[2]) || (buffer3[3] != buffer4[3]))) || (((buffer3[4] != buffer4[4]) || (buffer3[5] != buffer4[5])) || ((buffer3[6] != buffer4[6]) || (buffer3[7] != buffer4[7])))) || ((((buffer3[8] != buffer4[8]) || (buffer3[9] != buffer4[9])) || ((buffer3[10] != buffer4[10]) || (buffer3[11] != buffer4[11]))) || (((buffer3[12] != buffer4[12]) || (buffer3[13] != buffer4[13])) || (buffer3[14] != buffer4[14])))) || (buffer3[15] != buffer4[15]))
            {
                this.getKey(rifIn);
                num12++;
                if (Directory.Exists("temp"))
                {
                    Directory.Delete("temp", true);
                }
            }
            return(path);
        }
示例#19
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);
        }
示例#20
0
        public string makerif(string inFile, string outFile)
        {
            if (!File.Exists(inFile))
            {
                Console.WriteLine(inFile + " not found");
                return(inFile);
            }
            string     directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            string     str2          = directoryName.Replace(@"file:\", "");
            string     str3          = directoryName + "/data/act.dat";
            string     str4          = directoryName + "/data/idps";
            string     path          = str3.Replace(@"file:\", "");
            string     iDPSFile      = str4.Replace(@"file:\", "");
            FileStream stream        = File.Open(path, FileMode.Open);

            byte[] buffer = new byte[8];
            stream.Seek(8L, SeekOrigin.Begin);
            stream.Read(buffer, 0, 8);
            stream.Close();
            string s = this.GetSubstringByString('/', ".", inFile);

            if (str2 != null)
            {
                outFile = str2 + "/temp/" + s + ".rif";
            }
            else
            {
                outFile = "temp/" + s + ".rif";
            }
            byte[]        dest    = new byte[0x30];
            byte[]        o       = new byte[0x10];
            byte[]        i       = new byte[0x10];
            byte[]        buffer5 = null;
            byte[]        buffer6 = this.getKey(inFile);
            DirectoryInfo info    = Directory.CreateDirectory(str2 + "/temp");
            FileStream    stream2 = File.Open(outFile, FileMode.Create);

            stream2.Write(rif_header, 0, 8);
            stream2.Write(buffer, 0, 8);
            byte[] bytes = Encoding.UTF8.GetBytes(s);
            ConversionUtils.arraycopy(bytes, 0, dest, 0L, bytes.Length);
            stream2.Write(dest, 0, 0x30);
            ToolsImpl.aesecbEncrypt(RIFKEY, i, 0, o, 0, 0x10);
            stream2.Write(o, 0, 0x10);
            long num = 0L;

            byte[] src     = decryptACTDAT(path, iDPSFile);
            byte[] buffer9 = new byte[0x10];
            buffer5 = new byte[0x10];
            byte[] buffer10 = new byte[40];
            ConversionUtils.arraycopy(src, ((int)num) * 0x10, buffer9, 0L, 0x10);
            ToolsImpl.aesecbEncrypt(buffer9, buffer6, 0, buffer5, 0, 0x10);
            stream2.Write(buffer5, 0, 0x10);
            stream2.Write(rif_footer, 0, 0x10);
            while (stream2.Length < 0x98L)
            {
                stream2.Write(rif_junk, 0, 1);
            }
            stream2.Close();
            string str8 = this.GetSubstringByString('/', ".", outFile);

            return(outFile);
        }
示例#21
0
        public string makerap(String rifIn)
        {
            if (!File.Exists(rifIn))
            {
                Console.WriteLine(rifIn + " not found");
                return(rifIn);
            }
            else
            {
                String strAppDir = Path.GetDirectoryName(
                    Assembly.GetExecutingAssembly().GetName().CodeBase);
                string path         = strAppDir.Replace("file:\\", "");
                string actIn1       = strAppDir + "/data/act.dat";
                string idps1        = strAppDir + "/data/idps";
                string actIn        = actIn1.Replace("file:\\", "");
                string idps         = idps1.Replace("file:\\", "");
                byte[] intermediate = getrifKey(rifIn, actIn, idps);

                for (int loop = 0; loop < 5; loop++)
                {
                    int acum  = 0;
                    int acum2 = 0;
                    for (int loop2 = 0; loop2 < 0x10; loop2++)
                    {
                        int index = indexTable[loop2];

                        byte current = (byte)(intermediate[index] + acum + acum2);
                        if (acum != 1 || current != 0xFF)
                        {
                            int aux1 = (current + key2[index]) & 0xFF;
                            int aux2 = key2[index] & 0xFF;
                            int aux3 = (intermediate[index]);
                            acum  = (aux1 < aux2) ? 1 : 0;
                            acum2 = (aux3 == 0xFF) ? 1 : 0;
                            intermediate[index] = (byte)(current + key2[index]);
                        }
                        else if (current == 0xFF)
                        {
                            intermediate[index] = (byte)(current + key2[index]);
                        }
                        else
                        {
                            intermediate[index] = current;
                        }
                    }
                    for (int loop2 = 1; loop2 < 0x10; loop2++)
                    {
                        int index1 = indexTable[loop2];
                        int index2 = indexTable[loop2 - 1];
                        intermediate[index1] = (byte)(intermediate[index2] ^ intermediate[index1]);
                    }
                    for (int loop2 = 0; loop2 < 0x10; loop2++)
                    {
                        int index = indexTable[loop2];
                        intermediate[index] = (byte)(key1[index] ^ intermediate[index]);
                    }
                }
                string cid = GetSubstringByString('\\', ".", rifIn);

                string outFile = null;
                if (path != null)
                {
                    outFile = path + "/raps/" + cid + ".rap";
                }
                else
                {
                    outFile = "raps/" + cid + ".rap";
                }

                byte[] rk = new byte[0x10];
                ToolsImpl.aesecbEncrypt(rapKey, intermediate, 0, rk, 0, 0x10);
                DirectoryInfo di = Directory.CreateDirectory(path + "/raps");
                FileStream    o  = File.Open(outFile, FileMode.Create);
                o.Write(rk, 0, 0x10);
                o.Close();



                while (i == 0)
                {
                    string  outFile2 = "test.rif";
                    raptest instance = new raptest();
                    rif = instance.makerif(outFile, outFile2);
                    i++;
                }

                byte[] first  = getrifKey(rifIn, actIn, idps);
                byte[] second = getrifKey(rif, actIn, idps);
                if (first[0] == second[0] && first[1] == second[1] && first[2] == second[2] && first[3] == second[3] && first[4] == second[4] && first[5] == second[5] && first[6] == second[6] && first[7] == second[7] && first[8] == second[8] && first[9] == second[9] && first[10] == second[10] && first[11] == second[11] && first[12] == second[12] && first[13] == second[13] && first[14] == second[14] && first[15] == second[15])
                {
                    return(outFile);
                }
                else
                {
                    getKey(rifIn);
                }


                if (Directory.Exists("temp"))
                {
                    Directory.Delete("temp", true);
                }
                return(outFile);
            }
        }