把密码加密以后再存入数据库

解决方案 »

  1.   

    可以把密码用加密。.net中提供了几种现成的加密算法。在System.Security.Cryptography名字空间下,自己查查msdn嘛。这里有一段源码,使用的是Rijndael对称算法,希望有用:
    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);
    // 读取源文件到cryptostream 
    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");// 反向操作--解密刚才加密的临时文件
    fsIn = File.Open(tempfile,FileMode.Open,FileAccess.Read);
    transform = symm.CreateDecryptor();
    cstream = new CryptoStream(fsIn,transform,
    CryptoStreamMode.Read);StreamReader sr = new StreamReader(cstream);
    Console.WriteLine("decrypted file text: " + sr.ReadToEnd());
    fsIn.Close();
      

  2.   

    密码放在数据库中!但是要加密,建议使用不可逆的加密算法,例如MD5或你自己设计一个稀奇古怪的算法,只要保证生成的加密后的串在不同密码明文出现相同加密结果的几率足够小即可。验证时按照同样的算法生成加密串,与数据库中的保存的加密串比较即可,不用解密出密码原文(因为不存在逆向算法),可保证密码的安全。