예제 #1
0
 public static int Decrypt(string encryptedFile, string outputFile, bool GUI = true)
 {
     using (FileStream fileStream1 = new FileStream(encryptedFile, FileMode.Open))
     {
         using (FileStream fileStream2 = new FileStream(outputFile, FileMode.Create))
         {
             RijndaelManaged rijndaelManaged = new RijndaelManaged();
             rijndaelManaged.Mode      = CipherMode.ECB;
             rijndaelManaged.BlockSize = 128;
             rijndaelManaged.Padding   = PaddingMode.PKCS7;
             using (ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(Crypto.KEY, Crypto.IV))
             {
                 try
                 {
                     Utility.PreventDeepSleep(Utility.PDSMode.Start);
                     using (CryptoStream cryptoStream = new CryptoStream((Stream)fileStream1, decryptor, CryptoStreamMode.Read))
                     {
                         byte[] buffer = new byte[4096];
                         long   num    = 0;
                         int    count;
                         do
                         {
                             Utility.PreventDeepSleep(Utility.PDSMode.Continue);
                             num += (long)(count = cryptoStream.Read(buffer, 0, buffer.Length));
                             fileStream2.Write(buffer, 0, count);
                             if (GUI)
                             {
                                 Crypto.form.SetProgressBar(Utility.GetProgress(num, fileStream1.Length));
                             }
                             else
                             {
                                 CmdLine.SetProgress(Utility.GetProgress(num, fileStream1.Length));
                             }
                         }while (count > 0);
                     }
                 }
                 catch (CryptographicException ex)
                 {
                     Logger.WriteLog("Error decrypting file: Wrong key.", false);
                     return(3);
                 }
                 catch (TargetInvocationException ex)
                 {
                     Logger.WriteLog("Error decrypting file: Please turn off FIPS compliance checking.", false);
                     return(800);
                 }
                 catch (IOException ex)
                 {
                     Logger.WriteLog("Error decrypting file: IOException: " + ex.Message, false);
                     return(3);
                 }
                 finally
                 {
                     Utility.PreventDeepSleep(Utility.PDSMode.Stop);
                 }
             }
         }
     }
     return(0);
 }
예제 #2
0
        private static int ProcessAction()
        {
            int num = -1;

            if (!string.IsNullOrEmpty(CmdLine.file))
            {
                if (!string.IsNullOrEmpty(CmdLine.region) && !string.IsNullOrEmpty(CmdLine.model) && !string.IsNullOrEmpty(CmdLine.version))
                {
                    num = CmdLine.DoDecrypt();
                }
                else if (!string.IsNullOrEmpty(CmdLine.logicValue) && !string.IsNullOrEmpty(CmdLine.version))
                {
                    num = CmdLine.DoDecrypt();
                }
            }
            else if (!string.IsNullOrEmpty(CmdLine.model) && !string.IsNullOrEmpty(CmdLine.region))
            {
                num = !CmdLine.checkonly ? CmdLine.DoDownload() : CmdLine.DoCheck();
            }
            if (num == -1)
            {
                CmdLine.DisplayUsage();
                num = 1;
            }
            return(num);
        }
예제 #3
0
 public static int Main(string[] args)
 {
     Thread.Sleep(200);
     if (CmdLine.InputValidation(args))
     {
         return(CmdLine.ProcessAction());
     }
     CmdLine.DisplayUsage();
     return(1);
 }
예제 #4
0
 private static int Main(string[] args)
 {
     if (args.Length == 0)
     {
         Imports.FreeConsole();
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run((Form) new Form1());
     }
     else
     {
         Utility.run_by_cmd = true;
         Environment.Exit(CmdLine.Main(args));
     }
     return(0);
 }
예제 #5
0
 private static int DoDecrypt()
 {
     Logger.WriteLog("========== hadesFirm Firmware Decrypter ==========\n", false);
     Logger.WriteLog("Decrypting file " + CmdLine.file + "...", false);
     CmdLine.CreateProgressbar();
     if (CmdLine.file.EndsWith(".enc2"))
     {
         Crypto.SetDecryptKey(CmdLine.region, CmdLine.model, CmdLine.version);
     }
     else if (CmdLine.file.EndsWith(".enc4"))
     {
         Crypto.SetDecryptKey(CmdLine.version, CmdLine.logicValue);
     }
     if (Crypto.Decrypt(CmdLine.file, Path.GetFileNameWithoutExtension(CmdLine.file), false) != 0)
     {
         Logger.WriteLog("\nError decrypting file", false);
         Logger.WriteLog("Please make sure the filename is not modified and verify the version / logicValue argument", false);
         File.Delete(Path.GetFileNameWithoutExtension(CmdLine.file));
         return(3);
     }
     Logger.WriteLog("\nDecrypting successful", false);
     return(0);
 }
예제 #6
0
 private static bool InputValidation(string[] args)
 {
     if (!CmdLine.ParseArgs(args))
     {
         Logger.WriteLog("Error parsing arguments\n", false);
         return(false);
     }
     if (!string.IsNullOrEmpty(CmdLine.file) && !File.Exists(CmdLine.file))
     {
         Logger.WriteLog("File " + CmdLine.file + " does not exist\n", false);
         return(false);
     }
     if (!string.IsNullOrEmpty(CmdLine.file) && !CmdLine.ParseFileName())
     {
         Logger.WriteLog("Could not parse filename. Make sure the filename was not edited\n", false);
         return(false);
     }
     if (string.IsNullOrEmpty(CmdLine.folder) || Directory.Exists(CmdLine.folder))
     {
         return(true);
     }
     Logger.WriteLog("Folder " + CmdLine.folder + " does not exist\n", false);
     return(false);
 }
예제 #7
0
        private static int DoDownload()
        {
            Logger.WriteLog("========== hadesFirm Firmware Downloader ==========\n", false);
            Command.Firmware fw;
            if (string.IsNullOrEmpty(CmdLine.version))
            {
                fw = Command.UpdateCheckAuto(CmdLine.model, CmdLine.region, CmdLine.binary);
                if (fw.FetchAttempts == 0)
                {
                    return(5);
                }
            }
            else
            {
                fw = Command.UpdateCheck(CmdLine.model, CmdLine.region, CmdLine.version, CmdLine.binary, false);
            }
            if (fw.Version == null)
            {
                return(2);
            }
            string str = Path.Combine(CmdLine.folder, fw.Filename);

            Logger.WriteLog("Downloading...\n", false);
            CmdLine.CreateProgressbar();
            int num1;

            do
            {
                Utility.ReconnectCmdLine();
                Utility.ReconnectDownload = false;
                num1 = Command.Download(fw.Path, fw.Filename, fw.Version, fw.Region, fw.Model_Type, str, fw.Size, false);
            }while (Utility.ReconnectDownload);
            if (num1 != 200 && num1 != 206)
            {
                Logger.WriteLog("Error: " + (object)num1, false);
                return(4);
            }
            if (CmdLine.autodecrypt)
            {
                if (str.EndsWith(".enc2"))
                {
                    Crypto.SetDecryptKey(fw.Region, fw.Model, fw.Version);
                }
                else if (str.EndsWith(".enc4"))
                {
                    if (fw.BinaryNature == 1)
                    {
                        Crypto.SetDecryptKey(fw.Version, fw.LogicValueFactory);
                    }
                    else
                    {
                        Crypto.SetDecryptKey(fw.Version, fw.LogicValueHome);
                    }
                }
                Logger.WriteLog("\nDecrypting...\n", false);
                CmdLine.CreateProgressbar();
                CmdLine.fwdest = Path.Combine(Path.GetDirectoryName(str), Path.GetFileNameWithoutExtension(fw.Filename));
                int num2 = Crypto.Decrypt(str, CmdLine.fwdest, false);
                File.Delete(str);
                if (num2 != 0)
                {
                    File.Delete(CmdLine.fwdest);
                    return(3);
                }
            }
            if (!string.IsNullOrEmpty(CmdLine.metafile))
            {
                CmdLine.SaveMeta(fw);
            }
            Logger.WriteLog("\nFinished", false);
            return(0);
        }
예제 #8
0
        public static int DownloadBinary(
            string path,
            string file,
            string saveTo,
            string size,
            bool GUI = true)
        {
            long           num = 0;
            HttpWebRequest wr  = KiesRequest.Create("http://cloud-neofussvr.sslcs.cdngc.net/NF_DownloadBinaryForMass.do?file=" + path + file);

            wr.Method = "GET";
            wr.Headers["Authorization"] = Imports.GetAuthorization(Web.Nonce).Replace("Authorization: ", "").Replace("nonce=\"", "nonce=\"" + Web.Nonce);
            wr.Timeout          = 25000;
            wr.ReadWriteTimeout = 25000;
            if (System.IO.File.Exists(saveTo))
            {
                long length = new FileInfo(saveTo).Length;
                if (long.Parse(size) == length)
                {
                    Logger.WriteLog("File already downloaded.", false);
                    return(200);
                }
                Logger.WriteLog("File exists. Resuming download...", false);
                wr.AddRange((int)length);
                num = length;
            }
            using (HttpWebResponse responseFus = (HttpWebResponse)wr.GetResponseFUS())
            {
                if (responseFus == null)
                {
                    Logger.WriteLog("Error downloading: response is null", false);
                    return(901);
                }
                if (responseFus.StatusCode != HttpStatusCode.OK && responseFus.StatusCode != HttpStatusCode.PartialContent)
                {
                    Logger.WriteLog("Error downloading: " + (object)(int)responseFus.StatusCode, false);
                }
                else
                {
                    long total = long.Parse(responseFus.GetResponseHeader("content-length")) + num;
                    if (!System.IO.File.Exists(saveTo) || new FileInfo(saveTo).Length != total)
                    {
                        byte[]    buffer = new byte[8192];
                        Stopwatch sw     = new Stopwatch();
                        Utility.ResetSpeed(num);
                        try
                        {
                            Utility.PreventDeepSleep(Utility.PDSMode.Start);
                            using (BinaryWriter binaryWriter = new BinaryWriter((Stream) new FileStream(saveTo, FileMode.Append)))
                            {
                                int count;
                                do
                                {
                                    Utility.PreventDeepSleep(Utility.PDSMode.Continue);
                                    if (GUI)
                                    {
                                        if (Web.form.PauseDownload)
                                        {
                                            break;
                                        }
                                    }
                                    num += (long)(count = responseFus.GetResponseStream().Read(buffer, 0, buffer.Length));
                                    if (count > 0)
                                    {
                                        binaryWriter.Write(buffer, 0, count);
                                        if (GUI)
                                        {
                                            int dlspeed = Utility.DownloadSpeed(num, sw);
                                            if (dlspeed != -1)
                                            {
                                                Web.form.lbl_speed.Invoke((Delegate)((Action)(() => Web.form.lbl_speed.Text = dlspeed.ToString() + "kB/s")));
                                            }
                                        }
                                    }
                                    if (GUI)
                                    {
                                        Web.form.SetProgressBar(Utility.GetProgress(num, total));
                                    }
                                    else
                                    {
                                        CmdLine.SetProgress(Utility.GetProgress(num, total));
                                    }
                                }while (count > 0);
                            }
                        }
                        catch (IOException ex)
                        {
                            Logger.WriteLog("Error: Can't access output file " + saveTo, false);
                            if (GUI)
                            {
                                Web.form.PauseDownload = true;
                            }
                            Logger.WriteLog(ex.ToString(), false);
                            return(-1);
                        }
                        catch (WebException ex)
                        {
                            Logger.WriteLog("Error: Connection interrupted", false);
                            Web.SetReconnect();
                        }
                        finally
                        {
                            Utility.PreventDeepSleep(Utility.PDSMode.Stop);
                            if (GUI)
                            {
                                Web.form.lbl_speed.Invoke((Delegate)((Action)(() => Web.form.lbl_speed.Text = "0kB/s")));
                            }
                        }
                    }
                }
                return((int)responseFus.StatusCode);
            }
        }