how to encrypt the password column

后端 未结 6 1747
温柔的废话
温柔的废话 2021-01-12 08:16

I have user table in SQL Server 2008 r2. Nothing there is encrypted yet but I would like to at the least encrypt the passwords until the app is ready that will handle this b

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 08:36

    You should not encrypt passwords if your only task is to verify that the password the user entered is correct. You should hash them instead. You could use any algorithm to hash them, but I recommend using MD5 because it is very secure.1 :)

    for example:

    public string EncodePassword(string originalPassword)
    {
    //Declarations
    Byte[] originalBytes;
    Byte[] encodedBytes;
    MD5 md5;
    
    //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash    (encoded password)
    md5 = new MD5CryptoServiceProvider();
    originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
    encodedBytes = md5.ComputeHash(originalBytes);
    
    //Convert encoded bytes back to a 'readable' string
    return BitConverter.ToString(encodedBytes);
    }
    

    1 Edit (not original answer author): MD5 for passwords is considered insecure and more robust algorithms should be used. You should do research into the contemporary algorithms at the point of reading this. This post might be a good starting point.

提交回复
热议问题