コード例 #1
0
 static bool Convert(uint?key, string path)
 {
     if (!key.HasValue)
     {
         Console.WriteLine("Key not valid.");
         return(false);
     }
     if (!File.Exists(path))
     {
         Console.WriteLine($"File:{path} not exists.");
         return(false);
     }
     try
     {
         PsbFile psb = new PsbFile(path);
         if (psb.Version > 2)
         {
             if (psb.TestHeaderEncrypted()) //Decrypt
             {
                 psb.EncodeToFile(key.Value, path + ".decrypted", EncodeMode.Decrypt);
             }
             else
             {
                 psb.EncodeToFile(key.Value, path + ".encrypted", EncodeMode.Encrypt);
             }
         }
         else
         {
             if (psb.TestBodyEncrypted()) //Decrypt
             {
                 psb.EncodeToFile(key.Value, path + ".decrypted", EncodeMode.Decrypt);
             }
             else
             {
                 psb.EncodeToFile(key.Value, path + ".encrypted", EncodeMode.Encrypt);
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("Error: This file is not valid.");
         Console.WriteLine(e);
         return(false);
     }
     return(true);
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: mailwl/FreeMote
        static bool Convert(uint?key, string path)
        {
            if (!File.Exists(path))
            {
                Console.WriteLine($"File:{path} not exists.");
                return(false);
            }

            try
            {
                using (var fs = new FileStream(path, FileMode.Open))
                {
                    Stream stream   = fs;
                    string type     = null;
                    var    ms       = FreeMount.CreateContext().OpenFromShell(fs, ref type);
                    bool   hasShell = false;
                    if (ms != null)
                    {
                        Console.WriteLine($"Shell type: {type}");
                        stream   = ms;
                        hasShell = true;
                    }

                    BinaryReader br = new BinaryReader(stream, Encoding.UTF8);
                    if (!key.HasValue)
                    {
                        var k = FreeMount.CreateContext().GetKey(stream);
                        if (k != null)
                        {
                            key = k;
                            Console.WriteLine($"Using key: {key}");
                        }
                        else if (hasShell)
                        {
                            File.WriteAllBytes(Path.ChangeExtension(path, ".decompressed.psb"), ms.ToArray());
                            return(true);
                        }
                        else
                        {
                            Console.WriteLine("No Key and No Shell.");
                            return(false);
                        }
                    }

                    var header = PsbHeader.Load(br);

                    using (var outMs = new MemoryStream((int)stream.Length))
                    {
                        if (header.Version > 2)
                        {
                            if (PsbFile.TestHeaderEncrypted(stream, header)) //Decrypt
                            {
                                //psb.EncodeToFile(key.Value, path + ".decrypted", EncodeMode.Decrypt);
                                PsbFile.Encode(key.Value, EncodeMode.Decrypt, EncodePosition.Auto, stream, outMs);
                                File.WriteAllBytes(Path.ChangeExtension(path, ".pure.psb"), outMs.ToArray());
                            }
                            else
                            {
                                //psb.EncodeToFile(key.Value, path + ".encrypted", EncodeMode.Encrypt);
                                PsbFile.Encode(key.Value, EncodeMode.Encrypt, EncodePosition.Auto, stream, outMs);
                                File.WriteAllBytes(Path.ChangeExtension(path, ".encrypted.psb"), outMs.ToArray());
                            }
                        }
                        else
                        {
                            if (PsbFile.TestBodyEncrypted(br, header)) //Decrypt
                            {
                                PsbFile.Encode(key.Value, EncodeMode.Decrypt, EncodePosition.Auto, stream, outMs);
                                File.WriteAllBytes(Path.ChangeExtension(path, ".pure.psb"), outMs.ToArray());
                            }
                            else
                            {
                                PsbFile.Encode(key.Value, EncodeMode.Encrypt, EncodePosition.Auto, stream, outMs);
                                File.WriteAllBytes(Path.ChangeExtension(path, ".encrypted.psb"), outMs.ToArray());
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: This file is not valid.");
                Console.WriteLine(e);
                return(false);
            }

            return(true);
        }