Skip to content

Instantly share code, notes, and snippets.

@ccpu
Forked from PartTimeLegend/encryptfiles.cs
Created June 30, 2017 08:29
Show Gist options
  • Save ccpu/79aa175654e25fffdee9df4155ac8985 to your computer and use it in GitHub Desktop.
Save ccpu/79aa175654e25fffdee9df4155ac8985 to your computer and use it in GitHub Desktop.
Encrypt Files C#
public void EncrpytFiles(IList<Documents> documentsList)
{
foreach (var document in documentsList)
{
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Padding = (PaddingMode.ISO10126);
tdes.Mode = CipherMode.CBC;
tdes.GenerateIV();
tdes.GenerateKey();
tdes.CreateEncryptor();
FileStream fStream = File.Open(document.ToString(), FileMode.OpenOrCreate);
CryptoStream cStream = new CryptoStream(fStream,
new TripleDESCryptoServiceProvider().CreateEncryptor(tdes.Key, tdes.Key),
CryptoStreamMode.Write);
StreamWriter sWriter = new StreamWriter(cStream);
sWriter.WriteLine(fStream);
sWriter.Close();
cStream.Close();
fStream.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment