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

                var encfilename = Path.GetFileName(file);
                var enckey      = Path.GetFileNameWithoutExtension(encfilename);
                if (!Regex.IsMatch(encfilename, ".*?\\.[a-z0-9]{8}\\.enc$"))
                {
                    Log("filename decode error : {0}", file);
                    continue;
                }
                var decfilename = Path.GetFileNameWithoutExtension(enckey);

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

                using (var efile = File.OpenRead(file))
                    using (var dfile = File.OpenWrite(decfile))
                        using (var cfile = new CryptCTR.AES256CTR_CryptStream(efile, enckey))
                        {
                            try
                            {
                                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);
            }
        }
Пример #2
0
        private async Task EncodeFile(IEnumerable <string> plainfiles)
        {
            foreach (var file in plainfiles)
            {
                if (!File.Exists(file))
                {
                    Log("file not found : {0}", file);
                    continue;
                }
                var encfilepath = Path.GetDirectoryName(file);

                var plainfilename = Path.GetFileName(file);
                var enckey        = plainfilename + "." + Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
                var encfilename   = enckey + ".enc";

                var encfile = Path.Combine(encfilepath, encfilename);
                if (File.Exists(encfile))
                {
                    Log("Exists : {0}", encfile);
                    continue;
                }

                using (var pfile = File.OpenRead(file))
                    using (var efile = File.OpenWrite(encfile))
                        using (var cfile = new CryptCTR.AES256CTR_CryptStream(pfile, enckey))
                        {
                            try
                            {
                                await cfile.CopyToAsync(efile, 81920, cts.Token);
                            }
                            catch (Exception ex)
                            {
                                Log("Encode Error : {0}->{1} {2}", file, encfile, ex.Message);
                                continue;
                            }
                        }
                Log("OK : {0}->{1}", file, encfile);
            }
        }