I can't seem to figure out how to use the DES or TripleDES classes in .NET. I want to encrypt 64 bits (such that a 64-bit block is returned) but whilst I can get it to encrypt data, it either doesn't decrypt it (in the first example below), or returns 128 bits instead (second example). The third example just causes an exception.

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

namespace EncTest {
    
    public class Program {
        
        private static Int32 _blockSize;
        
        public static void Main(String[] args) {
            
            SymmetricAlgorithm algo = 
//                DES.Create();
//                Rijndael.Create();
                TripleDES.Create();
            
            Console.WriteLine("Using: {0}", algo.GetType().Name );
            
            algo.GenerateKey();
            algo.GenerateIV();
            
            Console.WriteLine("Key: {0}\nIV: {1}", Convert.ToBase64String( algo.Key ), Convert.ToBase64String( algo.IV ) );
            
            _blockSize = algo.LegalBlockSizes[0].MaxSize / 8;
            
            Console.WriteLine("Encrypt this:");
            Byte[] inputBytes = GetBlock(_blockSize);
            
            Encrypt1( algo, inputBytes ); Console.WriteLine("----------------");
            Encrypt2( algo, inputBytes ); Console.WriteLine("----------------");
            Encrypt3( algo, inputBytes );
            
            Console.ReadLine();
        }
        
        private static void Encrypt1(SymmetricAlgorithm algo, Byte[] clearInput) {
            
            ICryptoTransform enc = algo.CreateEncryptor();
            ICryptoTransform dec = algo.CreateDecryptor();
            
            Byte[] encryptedBytes = new Byte[_blockSize];
            int m = enc.TransformBlock( clearInput, 0, clearInput.Length, encryptedBytes, 0 );
            Console.WriteLine("Encrypted in {0,2} bytes as \"{1}\"", m, Convert.ToBase64String( encryptedBytes ) );
            
            Byte[] decryptedBytes = new Byte[_blockSize];
            m = dec.TransformBlock( encryptedBytes, 0, encryptedBytes.Length, decryptedBytes, 0 );
            Console.WriteLine("Decrypted in {0,2} bytes as \"{1}\"", m, Encoding.UTF8.GetString( decryptedBytes ) );
        }
        
        private static void Encrypt2(SymmetricAlgorithm algo, Byte[] clearInput) {
            
            ICryptoTransform enc = algo.CreateEncryptor();
            ICryptoTransform dec = algo.CreateDecryptor();
            
            Byte[] encryptedBytes = enc.TransformFinalBlock( clearInput, 0, clearInput.Length );
            
            Console.WriteLine("Encrypted in {0,2} bytes as \"{1}\"", encryptedBytes.Length, Convert.ToBase64String( encryptedBytes ) );
            
            Byte[] decryptedBytes = dec.TransformFinalBlock( encryptedBytes, 0, encryptedBytes.Length );
            
            Console.WriteLine("Decrypted in {0,2} bytes as \"{1}\"", decryptedBytes.Length, Encoding.UTF8.GetString( decryptedBytes ) );
        }
        
        private static void Encrypt3(SymmetricAlgorithm algo, Byte[] clearInput) {
            
            ICryptoTransform enc = algo.CreateEncryptor();
            ICryptoTransform dec = algo.CreateDecryptor();
            
            MemoryStream ms  = new MemoryStream();
            CryptoStream csw = new CryptoStream( ms, enc, CryptoStreamMode.Write );
            
            csw.Write( clearInput, 0, clearInput.Length );
            csw.Flush(); // at this point, 'ms' reports 8 bytes were written
            csw.Close();
            
            Byte[] encdata = ms.ToArray();
            ms = new MemoryStream( encdata );
            
            Console.WriteLine("Encrypted in {0,2} bytes as \"{1}\"", encdata.Length, Convert.ToBase64String( encdata ) );
            
            Byte[] decryptedBytes = new Byte[ _blockSize ];
            
            CryptoStream csr = new CryptoStream( ms, dec, CryptoStreamMode.Read );
            int br = csr.Read( decryptedBytes, 0, decryptedBytes.Length ); // TODO: "Bad Data" exception thrown here
            csr.Close();
            
            Console.WriteLine("Decrypted in {0,2} bytes as \"{1}\" ({2} bytes read)", decryptedBytes.Length, Encoding.UTF8.GetString( decryptedBytes ), br );
        }
        
        private static Byte[] GetBlock(int blockSize) {
            
            while(true) {
                
                String input = Console.ReadLine();
                Byte[] inputBytes = Encoding.UTF8.GetBytes( input );
                if( inputBytes.Length > blockSize ) continue;
                
                Byte[] ret = new Byte[blockSize];
                Array.Copy( inputBytes, ret, input.Length );
                return ret;
            }
            
        }
        
    }
}

This code should compile with .NET 2.0 or later (maybe even 1.1 too). Can anyone tell me where I'm going wrong?

Ta