-1

I am using following way to encrypt user's passwords which is AES Encryption. I just want to know is this a good practice or is there any other method should I follow. Because someone made me confused saying "Why don't you use "SALT". Can I use SALT AES Encryption? with Bellow is a sample query I am using.

INSERT INTO user(id,passowrd) VALUES (1,AES_ENCRYPT('mypassword','key'));
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Prasad Rajapaksha
  • 6,118
  • 10
  • 36
  • 52

4 Answers4

8
  • I hope your database column isn't named "passowrd".

That aside..

Salt isn't an encryption method, it's a random bit you need to add to each password before hashing to protect against dictionary attacks.

You should not be storing user passwords using AES encryption, which is reversible. You don't care what the user's password is, only that they know it: so don't store the password in a reversible form.

Instead, you use a secure one-way hash, such as SHA256 or even bcrypt, of the password plus some random data. This random junk, the "salt", is generated for each user and appended to the password before hashing. It is then stored in the database along with the hashed data to enable you to compare the passwords later.

This way, an attacker with what's called a rainbow table (a big dictionary of hash-to-plaintext mappings) can't come along and see "oh look, Joe User decided to make his password 'password'!". This also protects against someone brute-forcing all the passwords in your database, since if each account has its own independent salt, each password must be independently attacked.

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • 3
    +1 Salted one-way hashes should be used in almost all cases. Any reversible encryption creates the possibility that the wrong person will access the password (even if it's just your own employees, for example, who have access to the encryption/decryption algorithm). Given most users' (unfortunate) propensity to recycle passwords, this presents a significant risk. – Ben D Feb 26 '12 at 04:38
  • Thanks Borealid for your answer. – Prasad Rajapaksha Feb 26 '12 at 04:40
1

You wouldn't use "SALT" with AES, typically it's used with SHA.

Great details and explanation can be found at:

https://stackoverflow.com/a/4351749/1232478

Community
  • 1
  • 1
conrad10781
  • 2,223
  • 19
  • 17
0

A salt is a sequence of characters added to a string about to being hashed, so it's harder for an attacker to obtain the original string.

However, for a cipher algorithm that is not based on hash, a salt isn't necessary, because the attackers will obtain anyway the original string with the salt, and it's just needed logic to remove it.

Adonais
  • 1,294
  • 6
  • 12
  • BTW, as Borealid suggested, why don't you use instead a hash? It's more secure than encryption for strings you don't need to recover later but just compare, like passwords. – Adonais Feb 26 '12 at 04:29
0

With AES_ENCRYPT the key you're using IS the salt. I prefer one-way hashes like MD5 or SHA, but many like decryptable passwords like you're storing.

davidethell
  • 11,708
  • 6
  • 43
  • 63