示例#1
0
        /// <summary>
        /// Build the decrypted title key based on title id bytes.
        /// </summary>
        private void DecryptTitleKey()
        {
            this.IV = new byte[0x10];
            Array.Copy(this.TitleIdBytes, 0, this.IV, 0, 8);

            switch (this.CommonKeyIndex)
            {
            case 1:
                if (this.KoreanCommonKey == null)
                {
                    this.KoreanCommonKey = NintendoWiiOpticalDisc.GetKeyFromFile(NintendoWiiOpticalDisc.KOREAN_KEY_PATH);
                }

                this.DecryptedTitleKey = AESEngine.Decrypt(this.EncryptedTitleKey,
                                                           this.KoreanCommonKey, this.IV,
                                                           CipherMode.CBC, PaddingMode.Zeros);

                break;

            case 0:
            default:
                if (this.CommonKey == null)
                {
                    this.CommonKey = NintendoWiiOpticalDisc.GetKeyFromFile(NintendoWiiOpticalDisc.COMMON_KEY_PATH);
                }

                this.DecryptedTitleKey = AESEngine.Decrypt(this.EncryptedTitleKey,
                                                           this.CommonKey, this.IV,
                                                           CipherMode.CBC, PaddingMode.Zeros);

                break;
            } // switch (this.CommonKeyIndex)
        }
示例#2
0
            private static byte[] DecryptData(int dataSize, long dataRelativeOffset, Stream encrPKGReadStream, BinaryReader brEncrPKG)
            {
                int size = dataSize % 16;

                if (size > 0)
                {
                    size = ((dataSize / 16) + 1) * 16;
                }
                else
                {
                    size = dataSize;
                }
                byte[] PKGFileKeyConsec = new byte[size], incPKGFileKey = new byte[PKGFileKey.Length];
                Array.Copy(PKGFileKey, incPKGFileKey, PKGFileKey.Length);
                encrPKGReadStream.Seek(dataRelativeOffset + uiEncryptedFileStartOffset, SeekOrigin.Begin);
                byte[] EncryptedData = brEncrPKG.ReadBytes(size);
                for (long pos = 0; pos < dataRelativeOffset; pos += 16)
                {
                    IncrementArray(ref incPKGFileKey, PKGFileKey.Length - 1);
                }

                for (long pos = 0; pos < size; pos += 16)
                {
                    Array.Copy(incPKGFileKey, 0, PKGFileKeyConsec, pos, PKGFileKey.Length);
                    IncrementArray(ref incPKGFileKey, PKGFileKey.Length - 1);
                }
                byte[] PKGXorKeyConsec = AESEngine.Encrypt(PKGFileKeyConsec, AesKey, AesKey, CipherMode.ECB, PaddingMode.None);
                return(XOREngine.XOR(EncryptedData, 0, PKGXorKeyConsec.Length, PKGXorKeyConsec));
            }
示例#3
0
        private byte[] DecryptData(int dataSize, long dataRelativeOffset, long pkgEncryptedFileStartOffset, byte[] AesKey, Stream encrPKGReadStream, BinaryReader brEncrPKG)
        {
            int num2;
            int count = dataSize % 0x10;

            if (count > 0)
            {
                count = ((dataSize / 0x10) + 1) * 0x10;
            }
            else
            {
                count = dataSize;
            }
            byte[] inByteArray      = new byte[count];
            byte[] buffer2          = new byte[count];
            byte[] destinationArray = new byte[count];
            byte[] xORKey           = new byte[count];
            byte[] buffer5          = new byte[this.PKGFileKey.Length];
            Array.Copy(this.PKGFileKey, buffer5, this.PKGFileKey.Length);
            encrPKGReadStream.Seek(dataRelativeOffset + pkgEncryptedFileStartOffset, SeekOrigin.Begin);
            inByteArray = brEncrPKG.ReadBytes(count);
            for (num2 = 0; num2 < dataRelativeOffset; num2 += 0x10)
            {
                this.IncrementArray(ref buffer5, this.PKGFileKey.Length - 1);
            }
            for (num2 = 0; num2 < count; num2 += 0x10)
            {
                Array.Copy(buffer5, 0, destinationArray, num2, this.PKGFileKey.Length);
                this.IncrementArray(ref buffer5, this.PKGFileKey.Length - 1);
            }
            xORKey = AESEngine.Encrypt(destinationArray, AesKey, AesKey, CipherMode.ECB, PaddingMode.None);
            return(XOREngine.XOR(inByteArray, 0, xORKey.Length, xORKey));
        }
示例#4
0
        public ConnectionFactory()
        {
            //Simplify this code
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            IConfigurationRoot configuration = builder.Build();

            encryptedConnectionString = configuration["AppSettings:ConnectionString"];
            connectionString          = AESEngine.Decrypt(encryptedConnectionString);
        }
示例#5
0
        private void btn_decryption_Click(object sender, EventArgs e)
        {
            string _sourceFileContent = LoadFileContent(txt_sourcefile.Text);

            txt_filedisplayEncry.Text = _sourceFileContent;


            var decryptedString = AESEngine.DecryptString(key, _sourceFileContent);

            txt_filedisplay.Text = decryptedString;
        }
示例#6
0
        private void btn_entryption_Click(object sender, EventArgs e)
        {
            var str = Console.ReadLine();

            string _sourceFileContent = LoadFileContent(txt_sourcefile.Text);

            txt_filedisplay.Text = _sourceFileContent;

            var encryptedString = AESEngine.EncryptString(key, _sourceFileContent);

            WriteToFile(encryptedString, txt_destinationfile.Text);

            txt_filedisplayEncry.Text = encryptedString;
        }
示例#7
0
    private byte[] DecryptData(int dataSize, long dataRelativeOffset, long pkgEncryptedFileStartOffset, byte[] AesKey, Stream encrPKGReadStream, BinaryReader brEncrPKG)
    {
        int size = dataSize % 16;

        if (size > 0)
        {
            size = ((dataSize / 16) + 1) * 16;
        }
        else
        {
            size = dataSize;
        }

        byte[] EncryptedData    = new byte[size];
        byte[] DecryptedData    = new byte[size];
        byte[] PKGFileKeyConsec = new byte[size];
        byte[] PKGXorKeyConsec  = new byte[size];
        byte[] incPKGFileKey    = new byte[PKGFileKey.Length];
        Array.Copy(PKGFileKey, incPKGFileKey, PKGFileKey.Length);

        encrPKGReadStream.Seek(dataRelativeOffset + pkgEncryptedFileStartOffset, SeekOrigin.Begin);
        EncryptedData = brEncrPKG.ReadBytes(size);

        for (int pos = 0; pos < dataRelativeOffset; pos += 16)
        {
            IncrementArray(ref incPKGFileKey, PKGFileKey.Length - 1);
        }

        for (int pos = 0; pos < size; pos += 16)
        {
            Array.Copy(incPKGFileKey, 0, PKGFileKeyConsec, pos, PKGFileKey.Length);

            IncrementArray(ref incPKGFileKey, PKGFileKey.Length - 1);
        }

        //the incremented "file" key have to be encrypted with a "global AES key" to generate the "xor" key
        //PSP uses CipherMode.ECB, PaddingMode.None that doesn't need IV
        PKGXorKeyConsec = AESEngine.Encrypt(PKGFileKeyConsec, AesKey, AesKey, CipherMode.ECB, PaddingMode.None);

        //XOR Decrypt and save every 16 bytes of data:
        DecryptedData = XOREngine.XOR(EncryptedData, 0, PKGXorKeyConsec.Length, PKGXorKeyConsec);

        return(DecryptedData);
    }
示例#8
0
    private string DecryptPKGFile(string PKGFileName)
    {
        try
        {
            int    moltiplicator = 65536;
            byte[] EncryptedData = new byte[AesKey.Length * moltiplicator];
            byte[] DecryptedData = new byte[AesKey.Length * moltiplicator];

            byte[] PKGXorKey = new byte[AesKey.Length];
            byte[] EncryptedFileStartOffset = new byte[4];
            byte[] EncryptedFileLenght      = new byte[4];

            Stream       PKGReadStream = new FileStream(PKGFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            BinaryReader brPKG         = new BinaryReader(PKGReadStream);

            PKGReadStream.Seek(0x00, SeekOrigin.Begin);
            byte[] pkgMagic = brPKG.ReadBytes(4);
            if (pkgMagic[0x00] != 0x7F || pkgMagic[0x01] != 0x50 || pkgMagic[0x02] != 0x4B || pkgMagic[0x03] != 0x47)
            {
                Console.WriteLine("ERROR: Selected file isn't a .pkg file.");
                return(string.Empty);
            }

            //Finalized byte
            PKGReadStream.Seek(0x04, SeekOrigin.Begin);
            byte pkgFinalized = brPKG.ReadByte();

            if (pkgFinalized != 0x80)
            {
                Console.WriteLine("Detected Debug PKG. Currently fixing tool. Please be patient");
            }

            //PKG Type PSP/PS3
            PKGReadStream.Seek(0x07, SeekOrigin.Begin);
            byte pkgType = brPKG.ReadByte();

            switch (pkgType)
            {
            case 0x01:
                //PS3
                AesKey = PS3AesKey;
                break;

            case 0x02:
                //PSP
                AesKey = PSPAesKey;
                break;

            default:
                Console.WriteLine("ERROR: Type of .pkg file is unknown.");
                return(string.Empty);
            }

            //0x24 Store the start Address of the encrypted file to decrypt
            PKGReadStream.Seek(0x24, SeekOrigin.Begin);
            EncryptedFileStartOffset = brPKG.ReadBytes((int)EncryptedFileStartOffset.Length);
            Array.Reverse(EncryptedFileStartOffset);
            uiEncryptedFileStartOffset = BitConverter.ToUInt32(EncryptedFileStartOffset, 0);

            //0x1C Store the length of the whole pkg file

            //0x2C Store the length of the encrypted file to decrypt
            PKGReadStream.Seek(0x2C, SeekOrigin.Begin);
            EncryptedFileLenght = brPKG.ReadBytes((int)EncryptedFileLenght.Length);
            Array.Reverse(EncryptedFileLenght);
            uint uiEncryptedFileLenght = BitConverter.ToUInt32(EncryptedFileLenght, 0);

            //0x70 Store the PKG file Key.
            PKGReadStream.Seek(0x70, SeekOrigin.Begin);
            PKGFileKey = brPKG.ReadBytes(16);
            byte[] incPKGFileKey = new byte[16];
            Array.Copy(PKGFileKey, incPKGFileKey, PKGFileKey.Length);

            //the "file" key at 0x70 have to be encrypted with a "global AES key" to generate the "xor" key
            //PSP uses CipherMode.ECB, PaddingMode.None that doesn't need IV
            PKGXorKey = AESEngine.Encrypt(PKGFileKey, AesKey, AesKey, CipherMode.ECB, PaddingMode.None);

            // Pieces calculation
            double division = (double)uiEncryptedFileLenght / (double)AesKey.Length;
            UInt64 pieces   = (UInt64)Math.Floor(division);
            UInt64 mod      = (UInt64)uiEncryptedFileLenght / (UInt64)AesKey.Length;
            if (mod > 0)
            {
                pieces += 1;
            }

            if (File.Exists(PKGFileName + ".Dec"))
            {
                File.Delete(PKGFileName + ".Dec");
            }

            //Write File
            FileStream   DecryptedFileWriteStream = new FileStream(PKGFileName + ".Dec", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
            BinaryWriter bwDecryptedFile          = new BinaryWriter(DecryptedFileWriteStream);

            //Put the read pointer on the encrypted starting point.
            PKGReadStream.Seek((int)uiEncryptedFileStartOffset, SeekOrigin.Begin);

            // Pieces calculation
            double filedivision = (double)uiEncryptedFileLenght / (double)(AesKey.Length * moltiplicator);
            UInt64 filepieces   = (UInt64)Math.Floor(filedivision);
            UInt64 filemod      = (UInt64)uiEncryptedFileLenght % (UInt64)(AesKey.Length * moltiplicator);
            if (filemod > 0)
            {
                filepieces += 1;
            }

            for (UInt64 i = 0; i < filepieces; i++)
            {
                //If we have a mod and this is the last piece then...
                if ((filemod > 0) && (i == (filepieces - 1)))
                {
                    EncryptedData = new byte[filemod];
                    DecryptedData = new byte[filemod];
                }

                //Read 16 bytes of Encrypted data
                EncryptedData = brPKG.ReadBytes(EncryptedData.Length);

                //In order to retrieve a fast AES Encryption we pre-Increment the array
                byte[] PKGFileKeyConsec = new byte[EncryptedData.Length];
                byte[] PKGXorKeyConsec  = new byte[EncryptedData.Length];

                for (int pos = 0; pos < EncryptedData.Length; pos += AesKey.Length)
                {
                    Array.Copy(incPKGFileKey, 0, PKGFileKeyConsec, pos, PKGFileKey.Length);

                    IncrementArray(ref incPKGFileKey, PKGFileKey.Length - 1);
                }

                //the incremented "file" key have to be encrypted with a "global AES key" to generate the "xor" key
                //PSP uses CipherMode.ECB, PaddingMode.None that doesn't need IV
                PKGXorKeyConsec = AESEngine.Encrypt(PKGFileKeyConsec, AesKey, AesKey, CipherMode.ECB, PaddingMode.None);

                //XOR Decrypt and save every 16 bytes of data:
                DecryptedData = XOREngine.XOR(EncryptedData, 0, PKGXorKeyConsec.Length, PKGXorKeyConsec);

                Console.Write("Decrypting block {0} of {1}...", i + 1, filepieces);
                Console.SetCursorPosition(0, Console.CursorTop);

                bwDecryptedFile.Write(DecryptedData);
            }

            Console.WriteLine();

            DecryptedFileWriteStream.Close();
            bwDecryptedFile.Close();

            return(PKGFileName + ".Dec");
        }
        catch (Exception ex)
        {
            Console.WriteLine("\nERROR: An error occurred during the decryption process.\n\nMessage:\n{0}", ex.ToString());
            return(string.Empty);
        }
    }
        private string DecryptPKGFile(string PKGFileName, FileInfo ini)
        {
            try
            {
                int    moltiplicator = 65536;
                byte[] EncryptedData = new byte[AesKey.Length * moltiplicator];
                byte[] DecryptedData = new byte[AesKey.Length * moltiplicator];

                byte[] PKGXorKey = new byte[AesKey.Length];
                byte[] EncryptedFileStartOffset = new byte[4];
                byte[] EncryptedFileLenght      = new byte[4];

                Stream       PKGReadStream = new FileStream(PKGFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader brPKG         = new BinaryReader(PKGReadStream);

                PKGReadStream.Seek(0x00, SeekOrigin.Begin);
                byte[] pkgMagic = brPKG.ReadBytes(4);
                if (pkgMagic[0x00] != 0x7F || pkgMagic[0x01] != 0x50 || pkgMagic[0x02] != 0x4B || pkgMagic[0x03] != 0x47)
                {
                    txtResults.Text += "ERROR: Selected file isn't a Pkg file.";
                    SystemSounds.Beep.Play();
                    return(string.Empty);
                }

                //Finalized byte
                PKGReadStream.Seek(0x04, SeekOrigin.Begin);
                byte pkgFinalized = brPKG.ReadByte();

                if (pkgFinalized != 0x80)
                {
                    string com     = ini.Name;
                    string command = "pkg " + com;
                    CMD(command);
                    if (File.Exists("PARAM.SFO") == true)
                    {
                        string    dir         = Application.StartupPath + @"\UnPacked";
                        PARAM_SFO parrameters = new PARAM_SFO("PARAM.SFO");
                        string    pkgid       = parrameters.TitleID;
                        pkgID.Add(pkgid);
                        string pkgdir = dir + "\\" + pkgid;
                        Directory.CreateDirectory(pkgdir);
                        if (Directory.Exists("USRDIR") == true)
                        {
                            Directory.Move("USRDIR", pkgdir + "\\USRDIR");
                        }
                        if (Directory.Exists("TROPDIR") == true)
                        {
                            Directory.Move("TROPDIR", pkgdir + "\\TROPDIR");
                        }
                        if (File.Exists("PARAM.SFO") == true)
                        {
                            File.Move("PARAM.SFO", pkgdir + "\\PARAM.SFO");
                        }
                        if (File.Exists("ICON0.PNG") == true)
                        {
                            File.Move("ICON0.PNG", pkgdir + "\\ICON0.PNG");
                        }
                        if (File.Exists("PIC1.PNG") == true)
                        {
                            File.Move("PIC1.PNG", pkgdir + "\\PIC1.PNG");
                        }
                        if (File.Exists("PS3LOGO.DAT") == true)
                        {
                            if (File.Exists("PARAM.HIP") == true)
                            {
                                File.Move("PARAM.HIP", pkgdir + "\\PARAM.HIP");
                            }
                        }
                        if (File.Exists("PS3LOGO.DAT") == true)
                        {
                            File.Move("PS3LOGO.DAT", pkgdir + "\\PS3LOGO.DAT");
                        }
                    }

                    SystemSounds.Beep.Play();
                    return(string.Empty);
                }

                //PKG Type PSP/PS3
                PKGReadStream.Seek(0x07, SeekOrigin.Begin);
                byte pkgType = brPKG.ReadByte();

                switch (pkgType)
                {
                case 0x01:
                    //PS3
                    AesKey = PS3AesKey;
                    break;

                case 0x02:
                    //PSP
                    AesKey = PSPAesKey;
                    break;

                default:
                    txtResults.Text += "ERROR: Selected pkg isn't Valid.";
                    SystemSounds.Beep.Play();
                    return(string.Empty);
                }

                //0x24 Store the start Address of the encrypted file to decrypt
                PKGReadStream.Seek(0x24, SeekOrigin.Begin);
                EncryptedFileStartOffset = brPKG.ReadBytes((int)EncryptedFileStartOffset.Length);
                Array.Reverse(EncryptedFileStartOffset);
                uiEncryptedFileStartOffset = BitConverter.ToUInt32(EncryptedFileStartOffset, 0);

                //0x1C Store the length of the whole pkg file

                //0x2C Store the length of the encrypted file to decrypt
                PKGReadStream.Seek(0x2C, SeekOrigin.Begin);
                EncryptedFileLenght = brPKG.ReadBytes((int)EncryptedFileLenght.Length);
                Array.Reverse(EncryptedFileLenght);
                uint uiEncryptedFileLenght = BitConverter.ToUInt32(EncryptedFileLenght, 0);

                //0x70 Store the PKG file Key.
                PKGReadStream.Seek(0x70, SeekOrigin.Begin);
                PKGFileKey = brPKG.ReadBytes(16);
                byte[] incPKGFileKey = new byte[16];
                Array.Copy(PKGFileKey, incPKGFileKey, PKGFileKey.Length);

                //the "file" key at 0x70 have to be encrypted with a "global AES key" to generate the "xor" key
                //PSP uses CipherMode.ECB, PaddingMode.None that doesn't need IV
                PKGXorKey = AESEngine.Encrypt(PKGFileKey, AesKey, AesKey, CipherMode.ECB, PaddingMode.None);

                // Pieces calculation
                double division = (double)uiEncryptedFileLenght / (double)AesKey.Length;
                UInt64 pieces   = (UInt64)Math.Floor(division);
                UInt64 mod      = (UInt64)uiEncryptedFileLenght / (UInt64)AesKey.Length;
                if (mod > 0)
                {
                    pieces += 1;
                }

                if (File.Exists(PKGFileName + ".Dec"))
                {
                    File.Delete(PKGFileName + ".Dec");
                }

                //Write File
                FileStream   DecryptedFileWriteStream = new FileStream(PKGFileName + ".Dec", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
                BinaryWriter bwDecryptedFile          = new BinaryWriter(DecryptedFileWriteStream);

                //Put the read pointer on the encrypted starting point.
                PKGReadStream.Seek((int)uiEncryptedFileStartOffset, SeekOrigin.Begin);

                // Pieces calculation
                double filedivision = (double)uiEncryptedFileLenght / (double)(AesKey.Length * moltiplicator);
                UInt64 filepieces   = (UInt64)Math.Floor(filedivision);
                UInt64 filemod      = (UInt64)uiEncryptedFileLenght % (UInt64)(AesKey.Length * moltiplicator);
                if (filemod > 0)
                {
                    filepieces += 1;
                }

                Application.DoEvents();

                for (UInt64 i = 0; i < filepieces; i++)
                {
                    //If we have a mod and this is the last piece then...
                    if ((filemod > 0) && (i == (filepieces - 1)))
                    {
                        EncryptedData = new byte[filemod];
                        DecryptedData = new byte[filemod];
                    }

                    //Read 16 bytes of Encrypted data
                    EncryptedData = brPKG.ReadBytes(EncryptedData.Length);

                    //In order to retrieve a fast AES Encryption we pre-Increment the array
                    byte[] PKGFileKeyConsec = new byte[EncryptedData.Length];
                    byte[] PKGXorKeyConsec  = new byte[EncryptedData.Length];

                    for (int pos = 0; pos < EncryptedData.Length; pos += AesKey.Length)
                    {
                        Array.Copy(incPKGFileKey, 0, PKGFileKeyConsec, pos, PKGFileKey.Length);

                        IncrementArray(ref incPKGFileKey, PKGFileKey.Length - 1);
                    }

                    //the incremented "file" key have to be encrypted with a "global AES key" to generate the "xor" key
                    //PSP uses CipherMode.ECB, PaddingMode.None that doesn't need IV
                    PKGXorKeyConsec = AESEngine.Encrypt(PKGFileKeyConsec, AesKey, AesKey, CipherMode.ECB, PaddingMode.None);

                    //XOR Decrypt and save every 16 bytes of data:
                    DecryptedData = XOREngine.XOR(EncryptedData, 0, PKGXorKeyConsec.Length, PKGXorKeyConsec);

                    Application.DoEvents();

                    bwDecryptedFile.Write(DecryptedData);
                }
                Application.DoEvents();

                DecryptedFileWriteStream.Close();
                bwDecryptedFile.Close();

                return(PKGFileName + ".Dec");
            }
            catch (Exception ex)
            {
                txtResults.Text += "ERROR: An error occured during the decrypting process ";
                SystemSounds.Beep.Play();
                return(string.Empty);
            }
        }
示例#10
0
            public static void DecryptPKGFile(string PKGFileName)
            {
                byte[] EncryptedFileStartOffset = new byte[8];

                Stream       PKGReadStream = new FileStream(PKGFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader brPKG         = new BinaryReader(PKGReadStream);

                PKGReadStream.Seek(0x07, SeekOrigin.Begin);
                byte pkgType = brPKG.ReadByte();

                switch (pkgType)
                {
                case 0x01:
                    break;

                default:
                    G.Exit("This is not a PS3 PKG.");
                    break;
                }

                PKGReadStream.Seek(0x14, SeekOrigin.Begin);
                byte[] FileChunks = new byte[4];
                FileChunks = brPKG.ReadBytes(FileChunks.Length);
                Array.Reverse(FileChunks);
                uint uiFileChunks = BitConverter.ToUInt32(FileChunks, 0);

                PKGReadStream.Seek(0x20, SeekOrigin.Begin);
                EncryptedFileStartOffset = brPKG.ReadBytes(EncryptedFileStartOffset.Length);
                Array.Reverse(EncryptedFileStartOffset);
                uiEncryptedFileStartOffset = BitConverter.ToUInt32(EncryptedFileStartOffset, 0);

                PKGReadStream.Seek(0x70, SeekOrigin.Begin);
                PKGFileKey = brPKG.ReadBytes(16);
                byte[] incPKGFileKey = new byte[16];
                Array.Copy(PKGFileKey, incPKGFileKey, PKGFileKey.Length);

                if (File.Exists(G.DECPath))
                {
                    File.Delete(G.DECPath);
                }

                FileStream   DecryptedFileWriteStream = new FileStream(G.DECPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
                BinaryWriter bwDecryptedFile          = new BinaryWriter(DecryptedFileWriteStream);

                PKGReadStream.Seek((int)uiEncryptedFileStartOffset, SeekOrigin.Begin);

                byte[] EncryptedDataList = brPKG.ReadBytes((int)(uiFileChunks * 0x20)),
                PKGFileKeyConsec = new byte[EncryptedDataList.Length], PKGXorKeyConsec;

                for (int pos = 0; pos < EncryptedDataList.Length; pos += AesKey.Length)
                {
                    Array.Copy(incPKGFileKey, 0, PKGFileKeyConsec, pos, PKGFileKey.Length);
                    IncrementArray(ref incPKGFileKey, PKGFileKey.Length - 1);
                }
                PKGXorKeyConsec = AESEngine.Encrypt(PKGFileKeyConsec, AesKey, AesKey, CipherMode.ECB, PaddingMode.None);

                byte[] DecryptedDataList = XOREngine.XOR(EncryptedDataList, 0, PKGXorKeyConsec.Length, PKGXorKeyConsec);

                bwDecryptedFile.Write(DecryptedDataList);

                for (uint i = 0; i < uiFileChunks; i++)
                {
                    uint size = BitConverter.ToUInt32(DecryptedDataList, (int)(i * 0x20) + 4);
                    size = (size & 0x000000FFU) << 24 | (size & 0x0000FF00U) << 8 | (size & 0x00FF0000U) >> 8 | (size & 0xFF000000U) >> 24;
                    size = (size & 0xFFFFFFF0U) + 0x10;
                    byte[] EncryptedDataEntry = brPKG.ReadBytes((int)size);
                    PKGFileKeyConsec = new byte[EncryptedDataEntry.Length];

                    for (int pos = 0; pos < EncryptedDataEntry.Length; pos += AesKey.Length)
                    {
                        Array.Copy(incPKGFileKey, 0, PKGFileKeyConsec, pos, PKGFileKey.Length);
                        IncrementArray(ref incPKGFileKey, PKGFileKey.Length - 1);
                    }
                    PKGXorKeyConsec = AESEngine.Encrypt(PKGFileKeyConsec, AesKey, AesKey, CipherMode.ECB, PaddingMode.None);

                    byte[] DecryptedDataEntry = XOREngine.XOR(EncryptedDataEntry, 0, PKGXorKeyConsec.Length, PKGXorKeyConsec);
                    bwDecryptedFile.Write(DecryptedDataEntry);
                }
                bwDecryptedFile.Close();
            }
示例#11
0
        public string MagicKey([FromBody] string cs)
        {
            string encryptedValue = AESEngine.Encrypt(cs);

            return(encryptedValue);
        }
示例#12
0
        public string DecryptPKGFile(string PKGFileName)
        {
            if (!File.Exists(PKGFileName))
            {
                Console.WriteLine(PKGFileName + " not found");
                return(PKGFileName);
            }
            try
            {
                int          num         = 0x10000;
                byte[]       inByteArray = new byte[this.AesKey.Length * num];
                byte[]       buffer      = new byte[this.AesKey.Length * num];
                byte[]       buffer3     = new byte[this.AesKey.Length];
                byte[]       array       = new byte[4];
                byte[]       buffer5     = new byte[4];
                Stream       input       = new FileStream(PKGFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader reader      = new BinaryReader(input);
                input.Seek(0L, SeekOrigin.Begin);
                byte[] buffer6 = reader.ReadBytes(4);
                if ((((buffer6[0] != 0x7f) || (buffer6[1] != 80)) || (buffer6[2] != 0x4b)) || (buffer6[3] != 0x47))
                {
                    SystemSounds.Beep.Play();
                    return(string.Empty);
                }
                input.Seek(4L, SeekOrigin.Begin);
                if (reader.ReadByte() != 0x80)
                {
                    SystemSounds.Beep.Play();
                    SystemSounds.Beep.Play();
                    //throw new expetion
                    throw new Exception("this will only work with debug eboots");
                }
                input.Seek(7L, SeekOrigin.Begin);
                switch (reader.ReadByte())
                {
                case 1:
                    this.AesKey = this.PS3AesKey;
                    break;

                case 2:
                    this.AesKey = this.PSPAesKey;
                    break;

                default:
                    SystemSounds.Beep.Play();
                    return(string.Empty);
                }
                input.Seek(0x24L, SeekOrigin.Begin);
                array = reader.ReadBytes(array.Length);
                Array.Reverse(array);
                this.uiEncryptedFileStartOffset = BitConverter.ToUInt32(array, 0);
                input.Seek(0x2cL, SeekOrigin.Begin);
                buffer5 = reader.ReadBytes(buffer5.Length);
                Array.Reverse(buffer5);
                uint num4 = BitConverter.ToUInt32(buffer5, 0);
                input.Seek(0x70L, SeekOrigin.Begin);
                this.PKGFileKey = reader.ReadBytes(0x10);
                byte[] destinationArray = new byte[0x10];
                Array.Copy(this.PKGFileKey, destinationArray, this.PKGFileKey.Length);
                buffer3 = AESEngine.Encrypt(this.PKGFileKey, this.AesKey, this.AesKey, CipherMode.ECB, PaddingMode.None);
                double d    = ((double)num4) / ((double)this.AesKey.Length);
                ulong  num6 = (ulong)Math.Floor(d);
                ulong  num7 = ((ulong)num4) / ((ulong)this.AesKey.Length);
                if (num7 > 0L)
                {
                    num6 += (ulong)1L;
                }
                if (File.Exists(PKGFileName + ".Dec"))
                {
                    File.Delete(PKGFileName + ".Dec");
                }
                FileStream   output = new FileStream(PKGFileName + ".Dec", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
                BinaryWriter writer = new BinaryWriter(output);
                input.Seek((long)this.uiEncryptedFileStartOffset, SeekOrigin.Begin);
                double num8  = ((double)num4) / ((double)(this.AesKey.Length * num));
                ulong  num9  = (ulong)Math.Floor(num8);
                ulong  num10 = ((ulong)num4) % ((ulong)(this.AesKey.Length * num));
                if (num10 > 0L)
                {
                    num9 += (ulong)1L;
                }
                for (ulong i = 0L; i < 10L; i += (ulong)1L)
                {
                    if ((num10 > 0L) && (i == (num9 - ((ulong)1L))))
                    {
                        inByteArray = new byte[num10];
                        buffer      = new byte[num10];
                    }
                    inByteArray = reader.ReadBytes(inByteArray.Length);
                    byte[] buffer8 = new byte[inByteArray.Length];
                    byte[] xORKey  = new byte[inByteArray.Length];
                    Console.WriteLine("Decrypting");
                    for (int j = 0; j < inByteArray.Length; j += this.AesKey.Length)
                    {
                        Array.Copy(destinationArray, 0, buffer8, j, this.PKGFileKey.Length);
                        this.IncrementArray(ref destinationArray, this.PKGFileKey.Length - 1);
                    }
                    xORKey = AESEngine.Encrypt(buffer8, this.AesKey, this.AesKey, CipherMode.ECB, PaddingMode.None);
                    buffer = XOREngine.XOR(inByteArray, 0, xORKey.Length, xORKey);
                    writer.Write(buffer);
                }
                output.Close();
                writer.Close();
                return(this.ExtractFiles(PKGFileName + ".Dec", PKGFileName));
            }
            catch (Exception)
            {
                return(string.Empty);
            }
        }