コード例 #1
0
        private async Task DecryptAsync()
        {
            await Task.Factory.StartNew(() =>
            {
                try
                {
                    if (writeLogs)
                    {
                        Logs.WriteLog("Decrypting has been started!");
                    }

                    ButtonEnabled = false;

                    string output = $@"{yPath}\{Path.GetFileNameWithoutExtension(xPath)}";

                    Progress     = 1;
                    TextProgress = "1/7 part - data reading...(slow)";
                    if (writeLogs)
                    {
                        Logs.WriteLog("1/7 part - data reading...");
                    }

                    string data = File.ReadAllText(xPath);

                    Progress     = 2;
                    TextProgress = "2/7 part - decrypting...(slow)";
                    if (writeLogs)
                    {
                        Logs.WriteLog("2/7 part - decrypting...");
                    }

                    string newData = new string(data.ToCharArray().Reverse().ToArray());
                    byte[] bytes   = Encoding.Default.GetBytes(newData);

                    Progress     = 3;
                    TextProgress = "3/7 part - cache creating...(slow)";
                    if (writeLogs)
                    {
                        Logs.WriteLog("3/7 part - cache creating...");
                    }

                    File.WriteAllBytes($"{output}.zip", bytes);
                    File.SetAttributes($"{output}.zip", FileAttributes.Hidden);

                    try
                    {
                        using (ZipFile zip = ZipFile.Read($"{output}.zip"))
                        {
                            Directory.CreateDirectory(output);

                            Progress     = 4;
                            TextProgress = "4/7 part - decrypting...(not fast)";
                            if (writeLogs)
                            {
                                Logs.WriteLog("4/7 part - decrypting...");
                            }

                            foreach (ZipEntry e in zip)
                            {
                                e.Extract(output, ExtractExistingFileAction.OverwriteSilently);
                            }
                        }

                        Progress     = 5;
                        TextProgress = "5/7 part - cache deleting...(very fast)";
                        if (writeLogs)
                        {
                            Logs.WriteLog("5/7 part - cache deleting...");
                        }

                        File.Delete($"{output}.zip");
                    }
                    catch (Exception ex)
                    {
                        SystemSounds.Hand.Play();

                        Progress     = 0;
                        TextProgress = "ERROR-Decrypt uncknown file format!";

                        ButtonEnabled = true;

                        if (writeLogs)
                        {
                            Logs.WriteLog("ERROR-Decrypt uncknown file format!");
                        }

                        ErrorWriter.WriteError(ex);

                        File.Delete($"{output}.zip");
                        return;
                    }

                    Progress     = 6;
                    TextProgress = "6/7 part - deleting old files if you need...(very fast)";
                    if (writeLogs)
                    {
                        Logs.WriteLog("6/7 part - deleting old files if you need...");
                    }
                    if (oldDataDel)
                    {
                        File.Delete(xPath);
                        if (writeLogs)
                        {
                            Logs.WriteLog("Old directory has been deleted!");
                        }
                    }

                    Progress     = 7;
                    TextProgress = "7/7 part - decrypting complete!";
                    if (writeLogs)
                    {
                        Logs.WriteLog("7/7 part - decrypting complete!");
                    }

                    if (operationWrite)
                    {
                        string guid = Guid.NewGuid().ToString();
                        List <Operation> list;
                        if (File.Exists($@"{App.MainPath}\File Protect\appcache.json"))
                        {
                            list = OperationManipulator.Read($@"{App.MainPath}\File Protect\appcache.json");
                            Logs.WriteLog($"\"{App.MainPath}\\File Protect\\appcache.json\" has been readed!");
                        }
                        else
                        {
                            list = new List <Operation>();
                        }
                        Operation operation = new Operation(guid, OperationType.Decrypt, DateTime.Now, xPath, output);
                        list.Add(operation);

                        OperationManipulator.Write($@"{App.MainPath}\File Protect\appcache.json", list);
                        Logs.WriteLog($"\"{App.MainPath}\\File Protect\\appcache.json\" has been writed!");
                    }

                    ButtonEnabled = true;

                    if (writeLogs)
                    {
                        Logs.WriteLog("Decoding complete!");
                    }
                }
                catch (FileNotFoundException)
                {
                    ButtonEnabled = true;

                    Progress     = 0;
                    TextProgress = "This file don't exist!";
                }
                catch (Exception ex)
                {
                    ButtonEnabled = true;

                    Progress     = 0;
                    TextProgress = "Unknown error. Send files to email in settings page to help fix bugs! thx";

                    ErrorWriter.WriteError(ex);
                }
            });
        }
コード例 #2
0
        private async Task EncryptAsync(string path)
        {
            try
            {
                await Task.Factory.StartNew(() =>
                {
                    try
                    {
                        ZipFile zip = new ZipFile();
                        zip.AddDirectory(path);
                        zip.Save($"{path}.zip");

                        File.SetAttributes($"{path}.zip", FileAttributes.Hidden);

                        if (writeLogs)
                        {
                            Logs.WriteLog("Cache of encrypt procces has been created");
                        }

                        Progress     = 3;
                        TextProgress = "3/10 part - rate cache size(fast)";
                        if (writeLogs)
                        {
                            Logs.WriteLog("3/10 part - rate cache size");
                        }

                        FileInfo info = new FileInfo($"{ path }.zip");
                        long size     = info.Length;

                        if (size > 2000000000)
                        {
                            ButtonEnabled = true;
                            if (writeLogs)
                            {
                                Logs.WriteLog("ERROR-Folder should not be more than 2 GB!");
                            }

                            Progress     = 0;
                            TextProgress = "Encoding error... Try again with smaller file(not be more 2 GB).";
                            File.Delete($"{path}.zip");
                            return;
                        }

                        Progress     = 4;
                        TextProgress = "4/10 part - reading...(fast)";
                        if (writeLogs)
                        {
                            Logs.WriteLog("4/10 part - reading...");
                        }

                        byte[] data = File.ReadAllBytes($"{path}.zip");

                        Progress     = 5;
                        TextProgress = "5/10 part - encoding...(not fast)";
                        if (writeLogs)
                        {
                            Logs.WriteLog("5/10 part - encoding...");
                        }

                        string zipData = Encoding.Default.GetString(data);

                        Progress     = 6;
                        TextProgress = "6/10 part - encoding...(not fast)";
                        if (writeLogs)
                        {
                            Logs.WriteLog("6/10 part - encoding...");
                        }

                        string newData = new string(zipData.ToCharArray().Reverse().ToArray());

                        Progress     = 7;
                        TextProgress = "7/10 part - writing...(slow)";
                        if (writeLogs)
                        {
                            Logs.WriteLog("7/10 part - writing...");
                        }

                        File.Create($"{path}.{extencion}").Close();
                        File.WriteAllText($"{path}.{extencion}", newData);

                        Progress     = 8;
                        TextProgress = "8/10 part - deleting cache(fast)";
                        if (writeLogs)
                        {
                            Logs.WriteLog("8/10 part - deleting cache");
                        }

                        File.Delete($"{path}.zip");

                        if (writeLogs)
                        {
                            Logs.WriteLog("Cache of encrypt procces has been deleted!");
                        }

                        Progress     = 9;
                        TextProgress = "9/10 part - deleting old file if you want(fast)";
                        if (writeLogs)
                        {
                            Logs.WriteLog("9/10 part - deleting old file if you want");
                        }

                        if (oldDataDel)
                        {
                            Directory.Delete(xPath);
                            if (writeLogs)
                            {
                                Logs.WriteLog("Old directory has been deleted!");
                            }
                        }

                        zip.Dispose();


                        if (operationWrite)
                        {
                            string guid = Guid.NewGuid().ToString();
                            List <Operation> list;
                            if (File.Exists($@"{App.MainPath}\File Protect\appcache.json"))
                            {
                                list = OperationManipulator.Read($@"{App.MainPath}\File Protect\appcache.json");
                                Logs.WriteLog($"\"{App.MainPath}\\File Protect\\appcache.json\" has been readed!");
                            }
                            else
                            {
                                list = new List <Operation>();
                            }
                            Operation operation = new Operation(guid, OperationType.Encrypt, DateTime.Now, xPath, $"{path}.{extencion}");
                            list.Add(operation);

                            OperationManipulator.Write($@"{App.MainPath}\File Protect\appcache.json", list);
                            Logs.WriteLog($"\"{App.MainPath}\\File Protect\\appcache.json\" has been writed!");
                        }

                        Progress      = 10;
                        TextProgress  = "10/10 part - encoding complete!";
                        ButtonEnabled = true;

                        if (writeLogs)
                        {
                            Logs.WriteLog("Encoding complete!");
                        }
                    }
                    catch (FileNotFoundException)
                    {
                        ButtonEnabled = true;

                        Progress     = 0;
                        TextProgress = "This file don't exist!";
                    }
                    catch (Exception ex)
                    {
                        Progress      = 0;
                        TextProgress  = "Unknown error. Send files to email in settings page to help fix bugs! thx";
                        ButtonEnabled = true;
                        ErrorWriter.WriteError(ex);
                    }
                });
            }
            catch (Exception ex)
            {
                ButtonEnabled = true;
                ErrorWriter.WriteError(ex);
            }
        }