1. protected void Page_Load(object sender, EventArgs e)  
  2.        {  
  3.            if (!IsPostBack)  
  4.            {  
  5.                RijndaelManaged rjm = new RijndaelManaged();  
  6.                //创建  
  7.                CreateFile();  
  8.                //文件内容  
  9.                GetFileContent("加密前:","d:/test.txt");  
  10.                //加密  
  11.                EncryptFile(rjm);  
  12.                //加密后的内容  
  13.                GetFileContent("加密后:","d:/encoded.txt");  
  14.                //解密,并且解密后的内容  
  15.                DecryptFile(rjm);  
  16.            }  
  17.  
  18.        }  
  19.        //创建文件  
  20.        public void CreateFile()  
  21.        {  
  22.            FileStream fs = new FileStream("d:/test.txt", FileMode.Create);  
  23.            StreamWriter sw = new StreamWriter(fs);  
  24.            sw.WriteLine("123456789");  
  25.            sw.Close();  
  26.            fs.Close();  
  27.        }  
  28.  
  29.        //读取文件  
  30.        public void GetFileContent(string info,string filepath)  
  31.        {  
  32.            FileStream fs = new FileStream(filepath, FileMode.Open);  
  33.            StreamReader sr = new StreamReader(fs);  
  34.            string str = sr.ReadToEnd();  
  35.            //Response.Write("<script>alert("+str+")</script>");  
  36.            Response.Write(info + str +"<br/>");  
  37.            sr.Close();  
  38.            fs.Close();  
  39.        }  
  40.  
  41.        //加密文件,读取加密后的内容  
  42.        public void EncryptFile(RijndaelManaged rjm)  
  43.        {  
  44.            FileStream fs = new FileStream("d:/test.txt", FileMode.Open);  
  45.            ICryptoTransform itf = rjm.CreateEncryptor();  
  46.  
  47.            CryptoStream cs = new CryptoStream(fs, itf, CryptoStreamMode.Read);  
  48.            FileStream outfile = new FileStream("d:/encoded.txt", FileMode.Create);  
  49.            int i = 0;  
  50.            while ((i = cs.ReadByte()) != -1)  
  51.            {  
  52.                outfile.WriteByte((byte)i);  
  53.            }  
  54.            cs.Close();  
  55.            outfile.Close();  
  56.        }  
  57.  
  58.  
  59.        //解密文件,读取解密后的文件内容  
  60.        public void DecryptFile(RijndaelManaged rjm)  
  61.        {  
  62.            FileStream fs = new FileStream("d:/encoded.txt", FileMode.Open);  
  63.            ICryptoTransform itf = rjm.CreateDecryptor();  
  64.            CryptoStream cs = new CryptoStream(fs, itf, CryptoStreamMode.Read);  
  65.            int i = 0;  
  66.            while ((i = cs.ReadByte()) != -1)  
  67.            {  
  68.                Response.Write((char)i);  
  69.            }  
  70.            cs.Close();  
  71.            fs.Close();  
  72.        }