我是否在ASP.NET中正确加密我的密码

时间:2011-03-11 08:06:28

标签: asp.net encryption

我有一个安全类:

 public class security
{
    private static string createSalt(int size)
    {
        //Generate a random cryptographic number
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        byte[] b = new byte[size];
        rng.GetBytes(b);

        //Convert to Base64
        return Convert.ToBase64String(b);
    }

    /// <summary>
    /// Generate a hashed password for comparison or create a new one
    /// </summary>
    /// <param name="pwd">Users password</param>
    /// <returns></returns>
    public static string createPasswordHash(string pwd)
    {
        string salt = "(removed)";
        string saltAndPwd = string.Concat(pwd, salt);
        string hashedPwd =
            FormsAuthentication.HashPasswordForStoringInConfigFile(
            saltAndPwd, "sha1");

        return hashedPwd;
    }
}

这很好用,但我想知道它是否足够。

此外,下一个代码块更好吗?矫枉过正?

static byte[] encrInitVector = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

static string encrKey = "(removed)";

public static string EncryptString(string s)
{
    byte[] key;

    try
    {
        key = Encoding.UTF8.GetBytes(encrKey.Substring(0, 8));

        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

        byte[] inputByteArray = Encoding.UTF8.GetBytes(s);

        MemoryStream ms = new MemoryStream();

        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, encrInitVector), CryptoStreamMode.Write);

        cs.Write(inputByteArray, 0, inputByteArray.Length);

        cs.FlushFinalBlock();

        return Convert.ToBase64String(ms.ToArray());
    }
    catch (Exception e)
    {
        throw e;
    }

1 个答案:

答案 0 :(得分:0)

我会说最后一次是最好的,矫枉过正总是好的,但只是看看这张图片。

  • enter image description here

如果您的网站不是大型购物商店或银行,或者您需要强大的安全性,那么您的第一个代码就足够了。 (如果你的用户安全性很高,那么我也会推荐使用SSL。)