/// <summary> /// Transforms a file into its cryptographically strong equivalent using AES256 in CBC mode. /// </summary> /// <param name="originalFile">The path to the file to be cryptographically transformed.</param> /// <param name="encryptedFile">The path where the new file will be created.</param> /// <param name="password">The key to be used during the cryptographic transformation.</param> /// <returns>A flag determining whether or not the operation was successful.</returns> public static bool EncryptFile(string originalFile, string encryptedFile, string password) { try { string originalFilename = originalFile; string encryptedFilename = encryptedFile; using (var originalStream = LongFile.Open(originalFile, FileMode.Open)) using (var encryptedStream = LongFile.Create(encryptedFile, 8192)) using (var encTransform = new EtM_EncryptTransform(key: password.ToBytes())) using (var crypoStream = new CryptoStream(encryptedStream, encTransform, CryptoStreamMode.Write)) { originalStream.CopyTo(crypoStream); } return(true); } catch { return(false); } }
/// <summary> /// Asynchronously transforms a file into its cryptographically strong equivalent using AES256 in CBC mode. /// </summary> /// <param name="originalFile">The path to the file to be cryptographically transformed.</param> /// <param name="encryptedFile">The path where the new file will be created.</param> /// <param name="password">The key to be used during the cryptographic transformation.</param> /// <returns>A flag determining whether or not the operation was successful.</returns> public static async Task <bool> EncryptFileAsync(string originalFile, string encryptedFile, byte[] password) { try { string originalFilename = originalFile; string encryptedFilename = encryptedFile; using (var originalStream = LongFile.Open(originalFile, FileMode.Open)) using (var encryptedStream = LongFile.Create(encryptedFile, 8192)) using (var encTransform = new EtM_EncryptTransform(key: password)) using (var crypoStream = new CryptoStream(encryptedStream, encTransform, CryptoStreamMode.Write)) { await originalStream.CopyToAsync(crypoStream); return(true); } } catch { return(false); } }