我是个c#新手 在写个后台对新闻管理对txt文档的读取加密存储 解密读取  我知道用FileStream 类 读取 用MD5CryptoServiceProvider类加密 但写了很多次老是整部队  想写成一个类 传入一个路径  请各位帮我写下  谢谢

解决方案 »

  1.   

    MD5是散列算法,不是加密。简单点,你用对称密钥加解密(就是加密、和解密都是同一个对密钥)。
    在计算机专网系统中广泛使用的对称加密算法有DES和IDEA等。美国国家标准局倡导的AES即将作为新标准取代DES。
      具体算法:
      3DES算法,Blowfish算法,RC5算法。比如:
    RC2CryptoServiceProvider
      RijndaelManaged
      TripleDESCryptoServiceProvider
      //例加密文本文件(RijndaelManaged )
      byte[] key = { 24, 55, 102,24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24,98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
      byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24,55, 102, 24, 98, 26, 67, 29, 99 };
      RijndaelManaged myRijndael = new RijndaelManaged();
      FileStream fsOut = File.Open(strOutName, FileMode.Create,FileAccess.Write);//strOutName文件名及路径 FileStream fsIn = File.Open(strPath, FileMode.Open,FileAccess.Read);
      CryptoStream csDecrypt=new CryptoStream(fsOut,myRijndael.CreateEncryptor(key, IV),CryptoStreamMode.Write);//读加密文本
      BinaryReader br = new BinaryReader(fsIn);
      csDecrypt.Write(br.ReadBytes((int)fsIn.Length),0, (int)fsIn.Length);
      csDecrypt.FlushFinalBlock();
      csDecrypt.Close();
      fsIn.Close();
      fsOut.Close();
      //解密文件
      byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118,104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92};
      byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26,67, 29, 99 };
      RijndaelManaged myRijndael = new RijndaelManaged();
      FileStream fsOut = File.Open(strPath, FileMode.Open, FileAccess.Read);
      CryptoStream csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key,IV), CryptoStreamMode.Read);
      StreamReader sr = new StreamReader(csDecrypt);//把文件读出来
      StreamWriter sw = new StreamWriter(strInName);//解密后文件写入一个新的文件
      sw.Write(sr.ReadToEnd());
      sw.Flush();
      sw.Close();
      sr.Close();f
      sOut.Close();
    复杂点,你可以用非对称密钥加解密(就是加密、和解密都是用不同的2对密钥)。比如:RSA,ECC等。