- using System;
- using System.IO;
- using System.Security.Cryptography;
- public class Encrypt
- {
- public static void Main()
- {
- FileStream fStream = File.Create(@"C:\text.txt");
- TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
- CryptoStream cStream = new CryptoStream(fStream, tdes.CreateEncryptor(), CryptoStreamMode.Write);
- StreamWriter sWriter = new StreamWriter(cStream);
- sWriter.WriteLine("Please encrypt me.");
- sWriter.Flush();
- sWriter.Close();
- FileStream fStreamKey = File.Create(@"C:\key.key");
- BinaryWriter bWriter = new BinaryWriter(fStreamKey);
- bWriter.Write( tdes.Key );
- bWriter.Write( tdes.IV );
- bWriter.Flush();
- bWriter.Close();
- }
- }
Encrypt Text Using TripleDESCryptoServiceProvider In C#
This snippet uses the TripleDESCryptoServiceProvider Class to encrypt a string and save it to a text file. The key to decrypt the file is also saved into a seperate text file.
No comments:
Post a Comment