开发者

How to compare encrypted password in sql

I already got my password encrypted and store it in database but now I want to compare the encrypted value to the password that a user type upon loading a page. Consider this code:

string userName = txtusername.Text;
string password = txtpassword.Text;
Encryptor en = new Encryptor(EncryptionAlgorithm.Rc2, CreateRandomPassword(7));
password = en.Encrypt(password);            
DataTable dt = uMManager.ValidateUser(userName, password);

CreateRandomPassword Method

private static string CreateRandomPassword(int passwordLength)
{
  string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
  char[] chars = new char[passwordLength];
  Random rd = new Random();

  for (int i = 0; i < passwordLength; i++)
  {
     chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
  }
  return new string(chars);
}

Encryptor Class

public class Encryptor
    {
        EncryptEngine engin;
        public byte[] IV;

        public Encryptor(EncryptionAlgorithm algID, string key)
        {
            engin = new EncryptEngine(algID, key);
        }

        public EncryptEngine EncryptEngine
        {
            get
            {
                return engin;
            }
            set
            {
                engin = value;
            }
        }

        public string Encrypt(string MainString)
        {
            MemoryStream memory = new MemoryStream();
            CryptoStream stream = new CryptoStream(memory, engin.GetCryptTransform(), CryptoStreamMode.Write);
            StreamWriter streamwriter = new StreamWriter(stream);
            streamwriter.WriteLine(MainString);
            streamwriter.Close();
            stream.Close();
            IV = engin.Vector;
            byte[] buffer = memory.ToArray();
            memory.Close();
            return Convert.ToBase64Stri开发者_运维问答ng(buffer);

        }
    }

I made a local method to generate random string for RC2 encryption. EncryptionAlgorithm is a Enums for the types of encryption. Now how can I compare 'password' to the password field in my database to check if the credential is correct


You can't check if the credential is correct, since you've encrypted it with a key you've thrown away. If you store the key along with the password, the encryption serves no purpose. If you don't, you can't verify.

Instead of trying to create a new way to store passwords, why not use one of the ways that's known to work?


Don't encrypt passwords. Hash them. Encryption allows for retrieval of the plaintext password, which is a Bad Thing. Hashing still allows you to check if what the user inputs matches with what he did before.


Here is the flow of the program:

  • When user register new account -> You encrypt his password -> Save it in database
  • When user login -> Encrypt input password -> Get user with password in database -> If user not null -> Login successful -> Else -> Login fail


it looks like you are using every time a random key to encrypt your password

so if

u encrypt "test" the first time
and then u encrypt "test" a second time. the result of the two encryption is not the same.

u should simply use a hash algorithm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜