Beispiel #1
0
        private async Task DecodeFile(IEnumerable <string> encfiles, string rootpath = null)
        {
            foreach (var file in encfiles)
            {
                if (!File.Exists(file))
                {
                    Log("file not found : {0}", file);
                    continue;
                }
                var decfilepath = rootpath;
                if (decfilepath == null)
                {
                    decfilepath = Path.GetDirectoryName(file);
                }

                var    encfilename = Path.GetFileName(file);
                string decfilename = null;
                if (encfilename.EndsWith(CryptRclone.encryptedSuffix))
                {
                    decfilename = Path.GetFileNameWithoutExtension(encfilename);
                }
                else
                {
                    if (CryptRclone.IsNameEncrypted(encfilename))
                    {
                        decfilename = CryptRclone.DecryptName(encfilename);
                    }
                    if (string.IsNullOrEmpty(decfilename))
                    {
                        Log("filename decode error : {0}", file);
                        continue;
                    }
                }

                var decfile = Path.Combine(decfilepath, decfilename);
                if (File.Exists(decfile))
                {
                    Log("Exists : {0}", decfile);
                    continue;
                }

                try
                {
                    using (var efile = File.OpenRead(file))
                        using (var dfile = File.OpenWrite(decfile))
                            using (var cfile = new CryptRclone.CryptRclone_DeryptStream(efile))
                            {
                                await cfile.CopyToAsync(dfile, 81920, cts.Token);
                            }
                }
                catch (Exception ex)
                {
                    Log("Decode Error : {0}->{1} {2}", file, decfile, ex.Message);
                    continue;
                }
                Log("OK : {0}->{1}", file, decfile);
            }
        }
Beispiel #2
0
        private async Task DecodeFolder(IEnumerable <string> encfolder, string rootpath = null)
        {
            foreach (var folder in encfolder)
            {
                if (!Directory.Exists(folder))
                {
                    Log("folder not found : {0}", folder);
                    continue;
                }
                var decparent = rootpath;
                if (decparent == null)
                {
                    decparent = Directory.GetParent(folder).FullName;
                }

                var encfoldername = (folder.EndsWith("\\")) ? Path.GetDirectoryName(folder) : folder;
                var i             = encfoldername.LastIndexOf('\\');
                if (i >= 0)
                {
                    encfoldername = encfoldername.Substring(i + 1);
                }

                string decfoldername = null;
                if (CryptRclone.IsNameEncrypted(encfoldername))
                {
                    decfoldername = CryptRclone.DecryptName(encfoldername);
                }
                string decfolder = Path.Combine(decparent, encfoldername);
                if (!string.IsNullOrEmpty(decfoldername))
                {
                    decfolder = Path.Combine(decparent, decfoldername);
                    if (Directory.Exists(decfolder))
                    {
                        Log("Exists : {0}", decfolder);
                        continue;
                    }

                    try
                    {
                        Directory.CreateDirectory(decfolder);
                    }
                    catch (Exception ex)
                    {
                        Log("CreateDirectory failed : {0} {1}", decfolder, ex.Message);
                        continue;
                    }
                    Log("OK : {0}->{1}", folder, decfolder);
                }

                // subitems
                var subfiles = Directory.GetFiles(folder);
                var subdirs  = Directory.GetDirectories(folder);

                await DecodeFile(subfiles, decfolder);
                await DecodeFolder(subdirs, decfolder);
            }
        }
Beispiel #3
0
        private async void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            var decodes = fileNames.Where(item => CryptRclone.IsNameEncrypted(Path.GetFileName(item)));
            var encodes = fileNames.Except(decodes);

            await DecodeFile(decodes.Where(item => File.Exists(item)));
            await DecodeFolder(decodes.Where(item => Directory.Exists(item)));
            await EncodeFile(encodes.Where(item => File.Exists(item)));
            await EncodeFolder(encodes.Where(item => Directory.Exists(item)));

            Log("Done.");
        }