Listing A
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace com.billdawson.crypto
{
       ///<remarks>
       ///USAGE: TextFileCrypt [path to file]
       /// If path includes whitespace, enclose in quotes.
       ///
       /// The Main in this class will...
       /// 1. Open the file you specify.
       /// 2. Encrypt and write its contents out to a new temp file.
       /// 3. Reopen the new file and decrypt it.
       /// 4. Display the decrypted contents to console.
       ///</remarks>
      class TextFileCrypt
       {
              public static void Main(string[] args)
              {
                     const string USAGE = "USAGE:\n"
                           + "TextFileCrypt [path to file]\n\n"
                           + "If path has whitespace, use quotes.";
                     if (args.Length!=1)
                     {
                           Console.WriteLine(USAGE);
                           return;
                     }
                    
                     string file   = args[0];
                     string tempfile = Path.GetTempFileName();
                    
                     FileStream fsIn = File.Open(file,FileMode.Open,
                                                FileAccess.Read);
                     FileStream fsOut = File.Open(tempfile, FileMode.Open,
                                                 FileAccess.Write);
                    
                     SymmetricAlgorithm symm = new RijndaelManaged();
                     ICryptoTransform transform = symm.CreateEncryptor();
                     CryptoStream cstream = new CryptoStream(fsOut,
                                                            transform,
                                                           
ryptoStreamMode.Write);
      
                    BinaryReader br = new BinaryReader(fsIn);
                    
                    // Read all of the orig file bytes into the
                     // crypto stream, which is attached to the
                     // outbound (temp) file's stream.
                    cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length);
                    cstream.FlushFinalBlock();
                     cstream.Close();
                     fsIn.Close();
                     fsOut.Close();
                    
                     Console.WriteLine("created encrypted file {0}", tempfile);
                     Console.WriteLine("will now decrypt and show contents");
                    
                     // Reverse the transform now, using the Decryptor
                     // on the temp file just created.
                     fsIn = File.Open(tempfile,FileMode.Open,FileAccess.Read);
                     transform = symm.CreateDecryptor();
                     cstream = new CryptoStream(fsIn,
                                               transform,
                                               CryptoStreamMode.Read);
                     // The stream reader will control the underlying CryptoStream
                     StreamReader sr = new StreamReader(cstream);
                     Console.WriteLine("decrypted file text: " + sr.ReadToEnd());
                     fsIn.Close();
              }
       }
}