Encrypt a file with DES in CSharp

Description

The following code shows how to encrypt a file with DES.

Example


         //from   w ww .  j  a  v  a 2 s.c o  m

using System;
using System.IO;
using System.Security.Cryptography;

class MainClass
{
  public static void Main() 
  {
    FileStream fsOut = File.Create(@"c:\encrypted.txt");

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

    CryptoStream cs = new CryptoStream(fsOut, tdes.CreateEncryptor(), CryptoStreamMode.Write);

    StreamWriter sw = new StreamWriter(cs);

    sw.WriteLine("str");
    sw.Flush();
    sw.Close();

    FileStream fsKeyOut = File.Create(@"c:\encrypted.key");
    
    BinaryWriter bw = new BinaryWriter(fsKeyOut);

    bw.Write( tdes.Key );
    bw.Write( tdes.IV );

    bw.Flush();
    bw.Close();
  }
}




















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip