Ejemplo n.º 1
0
        public static void Decrypt(int I, string file)
        {
            KKtIO reader = KKtIO.OpenReader(file);

            if (reader.ReadInt64() != 0x454C494641564944)
            {
                reader.Close();
                Encrypt(I, file);
            }
            else
            {
                Console.Title = "PD_Tool: DIVAFILE Decryptor - File: " + Path.GetFileName(file);
                reader.ReadUInt32();
                int    StreamLenght = (int)reader.Length;
                int    FileLenght   = reader.ReadInt32();
                byte[] decrypted    = new byte[StreamLenght];
                reader.Seek(0, 0);
                using (AesManaged crypto = new AesManaged())
                {
                    crypto.Key     = Key;
                    crypto.IV      = new byte[16];
                    crypto.Mode    = CipherMode.ECB;
                    crypto.Padding = PaddingMode.Zeros;
                    using (CryptoStream cryptoData = new CryptoStream(reader.BaseStream,
                                                                      crypto.CreateDecryptor(crypto.Key, crypto.IV), CryptoStreamMode.Read))
                        cryptoData.Read(decrypted, 0, StreamLenght);
                }
                KKtIO writer = KKtIO.OpenWriter(file, FileLenght);
                for (int i = 0x10; i < StreamLenght && i < FileLenght + 0x10; i++)
                {
                    writer.Write(decrypted[i]);
                }
                writer.Close();
            }
            Console.Title = "PD_Tool";
        }
Ejemplo n.º 2
0
        public void UnPack(string file, bool SaveToDisk)
        {
            Files         = null;
            Signature     = Farc.FArC;
            FT            = false;
            Console.Title = "PD_Tool: FARC Extractor - Archive: " + Path.GetFileName(file);
            if (File.Exists(file))
            {
                KKtIO  reader    = KKtIO.OpenReader(file);
                string directory = Path.GetFullPath(file).Replace(Path.GetExtension(file), "");
                Signature = (Farc)reader.ReadInt32Endian(true);
                if (Signature == Farc.FARC)
                {
                    Directory.CreateDirectory(directory);
                    int HeaderLenght = reader.ReadInt32Endian(true);
                    int Mode         = reader.ReadInt32Endian(true);
                    reader.ReadUInt32();
                    bool GZip = (Mode & 2) == 2;
                    bool ECB  = (Mode & 4) == 4;

                    int FARCType = reader.ReadInt32Endian(true);
                    FT = FARCType == 0x10;
                    bool CBC = !FT && FARCType != 0x40;
                    if (ECB && CBC)
                    {
                        byte[] Header = new byte[HeaderLenght - 0x08];
                        FT = true;
                        reader.Close();
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(0x10, 0);

                        using (CryptoStream cryptoStream = new CryptoStream(stream,
                                                                            GetAes(true, null).CreateDecryptor(), CryptoStreamMode.Read))
                            cryptoStream.Read(Header, 0x00, HeaderLenght - 0x08);
                        Header = SkipData(Header, 0x10);
                        KKtIO CBCreader = new KKtIO(new MemoryStream(Header));
                        CBCreader.BaseStream.Seek(0, 0);

                        FARCType = CBCreader.ReadInt32Endian(true);
                        FT       = FARCType == 0x10;
                        if (CBCreader.ReadInt32Endian(true) == 1)
                        {
                            Files = new FARCFile[CBCreader.ReadInt32Endian(true)];
                        }
                        CBCreader.ReadUInt32();
                        HeaderReader(HeaderLenght, ref Files, ref CBCreader);
                        CBCreader.Close();
                    }
                    else
                    {
                        if (reader.ReadInt32Endian(true) == 1)
                        {
                            Files = new FARCFile[reader.ReadInt32Endian(true)];
                        }
                        reader.ReadUInt32();
                        HeaderReader(HeaderLenght, ref Files, ref reader);
                        reader.Close();
                    }

                    for (int i = 0; i < Files.Length; i++)
                    {
                        int FileSize = ECB || Files[i].ECB ? (int)Main.
                                       Align(Files[i].SizeComp, 0x10) : Files[i].SizeComp;
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(Files[i].Offset, 0);
                        Files[i].Data = new byte[FileSize];

                        bool Encrypted = false;
                        if (ECB)
                        {
                            if ((FT && Files[i].ECB) || CBC)
                            {
                                using (CryptoStream cryptoStream = new CryptoStream(stream,
                                                                                    GetAes(true, null).CreateDecryptor(), CryptoStreamMode.Read))
                                    cryptoStream.Read(Files[i].Data, 0, FileSize);
                                Files[i].Data = SkipData(Files[i].Data, 0x10);
                            }
                            else
                            {
                                using (CryptoStream cryptoStream = new CryptoStream(stream,
                                                                                    GetAes(false, null).CreateDecryptor(), CryptoStreamMode.Read))
                                    cryptoStream.Read(Files[i].Data, 0, FileSize);
                            }
                            Encrypted = true;
                        }

                        bool Compressed = false;
                        bool LocalGZip  = (FT && Files[i].GZip) || GZip && Files[i].SizeUnc != 0;
                        if (LocalGZip)
                        {
                            GZipStream gZipStream;
                            if (Encrypted)
                            {
                                gZipStream = new GZipStream(new MemoryStream(
                                                                Files[i].Data), CompressionMode.Decompress);
                                stream.Close();
                            }
                            else
                            {
                                gZipStream = new GZipStream(stream, CompressionMode.Decompress);
                            }
                            Files[i].Data = new byte[Files[i].SizeUnc];
                            gZipStream.Read(Files[i].Data, 0, Files[i].SizeUnc);

                            Compressed = true;
                        }

                        if (!Encrypted && !Compressed)
                        {
                            Files[i].Data = new byte[Files[i].SizeUnc];
                            stream.Read(Files[i].Data, 0, Files[i].SizeUnc);
                            stream.Close();
                        }

                        if (SaveToDisk)
                        {
                            KKtIO writer = KKtIO.OpenWriter(Path.Combine(directory, Files[i].Name), true);
                            writer.Write(Files[i].Data);
                            writer.Close();
                            Files[i].Data = null;
                        }
                    }
                }
                else if (Signature == Farc.FArC)
                {
                    Directory.CreateDirectory(directory);
                    int HeaderLength = reader.ReadInt32Endian(true);
                    reader.ReadUInt32();
                    HeaderReader(HeaderLength, ref Files, ref reader);
                    reader.Close();

                    for (int i = 0; i < Files.Length; i++)
                    {
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(Files[i].Offset, 0);
                        Files[i].Data = new byte[Files[i].SizeComp];
                        stream.Read(Files[i].Data, 0, Files[i].SizeComp);
                        stream.Close();

                        using (MemoryStream memorystream = new MemoryStream(Files[i].Data))
                        {
                            GZipStream gZipStream = new GZipStream(memorystream, CompressionMode.Decompress);
                            Files[i].Data = new byte[Files[i].SizeUnc];
                            gZipStream.Read(Files[i].Data, 0, Files[i].SizeUnc);
                        }

                        KKtIO writer = KKtIO.OpenWriter(Path.Combine(directory, Files[i].Name), true);
                        writer.Write(Files[i].Data);
                        writer.Close();
                        Files[i].Data = null;
                    }
                }
                else if (Signature == Farc.FArc)
                {
                    Directory.CreateDirectory(directory);
                    int HeaderLength = reader.ReadInt32Endian(true);
                    reader.ReadUInt32();

                    HeaderReader(HeaderLength, ref Files, ref reader);
                    for (int i = 0; i < Files.Length; i++)
                    {
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(Files[i].Offset, 0);
                        Files[i].Data = new byte[Files[i].SizeUnc];
                        stream.Read(Files[i].Data, 0, Files[i].SizeUnc);
                        stream.Close();

                        KKtIO writer = KKtIO.OpenWriter(Path.Combine(directory, Files[i].Name), true);
                        writer.Write(Files[i].Data);
                        writer.Close();
                        Files[i].Data = null;
                    }
                }
                else
                {
                    Console.WriteLine("Unknown signature");
                    reader.Close();
                }
            }
            else
            {
                Console.WriteLine("File {0} doesn't exist.", Path.GetFileName(file));
            }
            Console.Clear();
            Console.Title = "PD_Tool: FARC Extractor";
        }