Esempio n. 1
0
        public IActionResult GetFile(IFormFile file)
        {
            string type       = file.Headers.Values.Last();
            string path       = @"D:\Labs\Serv\Server\Upload";
            string root       = @"D:\Labs\Serv\Server\Upload\UncryptFile" + type;
            string sign       = @"D:\Labs\Serv\Server\Upload\SignedFile.cig";
            string cryptppath = @"D:\Labs\Serv\Server\Upload\CryptZip.zip";
            string zipstr     = @"D:\Labs\Serv\Server\Upload\ZIP.zip";
            string filePath   = Path.Combine(path, file.FileName);

            using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
            {
                file.CopyToAsync(fileStream);
            }

            Crypted.DecryptFile(filePath, root);
            FileInfo Delete = new FileInfo(filePath);

            Delete.Delete();
            FileStream stream = new FileStream(root, FileMode.Open, FileAccess.Read);

            using (stream)
            {
                byte[] array = new byte[stream.Length];
                stream.Read(array, 0, array.Length);
                byte[] signed = SignDoc.Sign(array);
                SignDoc.WriteFile(sign, signed);
            }
            using (ZipFile zip = new ZipFile())
            {
                zip.AddDirectory(path + "\\");
                zip.Save(zipstr);
            }
            Crypted.EncryptFile(zipstr, cryptppath);
            byte[]     result;
            FileStream rstream = new FileStream(cryptppath, FileMode.Open, FileAccess.Read);

            using (rstream)
            {
                byte[] array = new byte[rstream.Length];
                rstream.Read(array, 0, array.Length);
                Delete = new FileInfo(root);
                Delete.Delete();
                Delete = new FileInfo(sign);
                Delete.Delete();
                Delete = new FileInfo(zipstr);
                Delete.Delete();
                result = array;
            }
            Delete = new FileInfo(cryptppath);
            Delete.Delete();
            return(File(result, "application/zip", "CryptZip.zip"));
        }
Esempio n. 2
0
        static async Task Main(string[] args)
        {
            //D:\Labs\Serv\Text.txt
            Console.WriteLine("Введите путь к файлу: ");
            string   path    = Convert.ToString(Console.ReadLine());
            FileInfo fileInf = new FileInfo(path);

            if (fileInf.Exists)
            {
                string cryptppath   = fileInf.DirectoryName + "\\" + "Crypted" + fileInf.Extension;
                string uncryptppath = fileInf.DirectoryName + "\\" + "FromServerCrypt.Zip";
                string decryptppath = fileInf.DirectoryName + "\\" + "FromServerDeCrypted.Zip";
                Crypted.EncryptFile(path, cryptppath);
                HttpClient httpClient = new HttpClient();
                var        content    = new MultipartFormDataContent();
                using (var fstream = File.OpenRead(cryptppath))
                {
                    var streamContent = new StreamContent(fstream);
                    streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {
                        Name = "file"
                        ,
                        FileName = Path.GetFileName(cryptppath),
                    };
                    streamContent.Headers.Add("type", fileInf.Extension);
                    content.Add(streamContent);
                    HttpResponseMessage response = await httpClient.PostAsync("https://localhost:44308/GetFile", content);

                    response.EnsureSuccessStatusCode();
                    httpClient.Dispose();
                    string sd = await response.Content.ReadAsStringAsync();

                    using (FileStream stream = new FileStream(uncryptppath, FileMode.OpenOrCreate))
                    {
                        byte[] array = await response.Content.ReadAsByteArrayAsync();

                        stream.Write(array, 0, array.Length);
                    }
                }
                FileInfo Delete = new FileInfo(cryptppath);
                Delete.Delete();
                Crypted.DecryptFile(uncryptppath, decryptppath);
                Delete = new FileInfo(uncryptppath);
                Delete.Delete();
                string file = "UncryptFile" + fileInf.Extension;
                string cig  = "SignedFile.cig";
                using (var zip = ZipFile.Open(decryptppath, ZipArchiveMode.Update))
                {
                    var entfile  = zip.GetEntry(file);
                    var entcig   = zip.GetEntry(cig);
                    var tempFile = Path.GetTempFileName();
                    var tempCig  = Path.GetTempFileName();
                    entfile.ExtractToFile(tempFile, true);
                    entcig.ExtractToFile(tempCig, true);
                    byte[] hash     = File.ReadAllBytes(tempFile);
                    byte[] signhash = File.ReadAllBytes(tempCig);
                    if (SignDoc.Verify(hash, signhash) == true)
                    {
                        Console.WriteLine("\n1.Файл 2.Его подпись. Подпись: подтверждена");
                    }
                    else
                    {
                        Console.WriteLine("Операция провалена");
                    }
                }
            }
            Console.ReadKey();
        }