参考一下我写的这个例子,没有时间给你改了,要睡觉了. :)
//--------------------------
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Text;namespace EncryptDecrypt
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
byte[] data = ASCIIEncoding.ASCII.GetBytes("This is a test data of for Encrypt and Decrypt");
System.Console.WriteLine("{0} : length is {1}",
ASCIIEncoding.ASCII.GetString(data,0,data.Length),
data.Length.ToString()); byte[] encrypted = DesEncrypt(data);
System.Console.WriteLine("{0} : length is {1}",
ASCIIEncoding.ASCII.GetString(encrypted,0,encrypted.Length), 
encrypted.Length.ToString()); byte[] decrypted = DesDecrypt(encrypted);
System.Console.WriteLine("{0} : length is {1}",
ASCIIEncoding.ASCII.GetString(decrypted,0,decrypted.Length),
decrypted.Length.ToString());
Console.ReadLine();
} static byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes("password");
static byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes("password1"); public static byte[] DesEncrypt(byte[] data)
{
MemoryStream ms = new MemoryStream(4096); DES des = new DESCryptoServiceProvider() ; CryptoStream encStream = new CryptoStream(ms,
des.CreateEncryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Write); encStream.Write(data,0,data.Length);
encStream.FlushFinalBlock(); //calculate the length of the encrypted data
byte[] bResult = new byte[ms.Position];
ms.Position = 0;
ms.Read(bResult, 0, bResult.Length) ;
   
encStream.Close();
return bResult;
} public static byte[] DesDecrypt ( byte[] data )
{
MemoryStream ms = new MemoryStream(data.Length); DES des = new DESCryptoServiceProvider() ; CryptoStream encStream = new CryptoStream(ms,
des.CreateDecryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Read);
   
ms.Write(data,0,data.Length);
ms.Position = 0;
   
string strResult = new StreamReader(encStream).ReadToEnd();
   
encStream.Close(); return ASCIIEncoding.ASCII.GetBytes(strResult);
}
}
}//--------