public bool Crypt() { GetAllFiles(_sourceFolder); using var keysFileStream = File.Open(_keysPath, FileMode.OpenOrCreate); using var zipToOpen = new FileStream(_destinationPath + "/" + _sourceFolder + ".zip", FileMode.OpenOrCreate); using var archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update); for (var i = 0; i < _allPaths.Count; i++) { var resultCryptFile = CryptFile.EncryptFile(_allPaths[i]); var entry = archive.CreateEntry(_allPaths[i], CompressionLevel.NoCompression); using var entryWriter = new StreamWriter(entry.Open()); entryWriter.Write(resultCryptFile.Result); keysFileStream.Write( Encoding.UTF8.GetBytes($"{resultCryptFile.Key}#{Encoding.UTF8.GetString(resultCryptFile.Iv)}\n")); ProgressBar(i, _allPaths.Count); } return(true); }
public static bool Decrypt(Cryptor cryptor, string archivePath, string destinationPath, string keysFile) { ZipFile.ExtractToDirectory(archivePath, destinationPath); cryptor.GetAllFilesForDecrypt(destinationPath); using var keysFileStream = File.Open(keysFile, FileMode.Open); using var memoryStream = new MemoryStream(); keysFileStream.CopyTo(memoryStream); var keysAndIv = Encoding.UTF8.GetString(memoryStream.ToArray()).Split("\n"); keysAndIv = keysAndIv.Take(keysAndIv.Count() - 1).ToArray(); var keyAndIvSplit = new List <string[]>(); foreach (var kiv in keysAndIv) { keyAndIvSplit.Add(kiv.Split("#")); } for (var index = 0; index < cryptor._allPathsForDecrypt.Count; index++) { var filePath = cryptor._allPathsForDecrypt[index]; var keyIv = keyAndIvSplit[index]; var key = keyIv[0]; var iv = keyIv[1]; Console.Write(CryptFile.DecryptFile(filePath, key, Encoding.Unicode.GetBytes(iv))); } return(true); }