private static void CopyEncrypt(string source, string destination) { var data = File.ReadAllBytes(source); var hash = CalculateHash(ref data); if (File.Exists(destination)) { var dest = new byte[80]; using (var fs = new FileStream(destination, FileMode.Open, FileAccess.Read)) fs.Read(dest, 0, dest.Length); var head = new CryptedFileHeader(ref dest); if (head.Hash == Convert.ToBase64String(hash)) { return; } } using (var fs = new FileStream(destination, FileMode.Create, FileAccess.Write)) { var salt = Guid.NewGuid(); var buffer = salt.ToByteArray(); fs.Write(buffer, 0, buffer.Length); fs.Write(hash, 0, hash.Length); using (var crypter = GenerateRijndael($"{salt:N}{Password}").CreateEncryptor()) buffer = crypter.TransformFinalBlock(data, 0, data.Length); fs.Write(buffer, 0, buffer.Length); } }
private static void CopyDecrypt(string source, string destination) { var data = File.ReadAllBytes(source); var head = new CryptedFileHeader(ref data); if (File.Exists(destination)) { var dest = File.ReadAllBytes(destination); if (head.Hash == Convert.ToBase64String(CalculateHash(ref dest))) { return; } } using (var crypter = GenerateRijndael($"{head.Salt}{Password}").CreateDecryptor()) File.WriteAllBytes(destination, crypter.TransformFinalBlock(data, 80, data.Length - 80)); }