Treehouse: Grow your CSS skills. Land your dream job.

Censor Password in Database?

  • # March 24, 2012 at 6:29 am

    Is there any kind of code that I can use either before putting the password into the database or a setting inside the database (phpMyAdmin), where I can censor and of course uncensor passwords, so that they won’t be readable in the database?

    # March 24, 2012 at 6:46 am

    You can use mcrypt on the string prior to passing it to the database…you of course need to make sure it is installed on the server, more than likely it is…

    < ?php
    //NOTE: 'saltnpeppa' can be changed to any string you wish
    // salting your password will make it incredibly strong.
    define('PW_SALT', 'saltnpeppa');

    function encrypt($the_text)
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, PW_SALT, $the_text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));

    function decrypt($text)
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, PW_SALT, base64_decode($the_text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
    //obviously, pass your posted data into a variable
    $password = $_POST;
    //then encrypt it...
    $enc_pwd = encrypt($password);

    // execute the php to query the db and append the encrypted data....

    //decrypt is simple your case this would be a db call of course...
    echo decrypt($enc_pwd);

    # March 24, 2012 at 6:49 am

    Thank you so much! :)

    # March 25, 2012 at 1:00 am

    Is there a specific reason you want to store the passwords in a form that can be decrypted back to plain text? This can be a security risk.

    What would be best is to hash the password using one of the many cryptographic hashing algorithms, such as SHA-256, and store the resulting hash (plus random salt value per password) in the database. This way, if your database is compromised, no actual passwords would be vulnerable.

    In addition, when a user enters their password, you again run it thru the hash function and compare the resulting hash with the one in the database (remember to add the salt).

    I suggest downloading the PHP-BB forum software and look at their code, as last I checked they did this very thing.

    # March 25, 2012 at 4:45 am

    Never store your password in the database in a form that can be decrypted as it is a security risk, no matter how hard you think it’ll be to decrypt.

    Always go for 1 way encryption using SHA with a salt in the password string.

    If a user forgets their password, they’ll just have to request a new one

    # March 25, 2012 at 7:17 am

    It’s for login, I’m basically getting the password from the form, and then inserting it in the database when they register, and validating when they login. And I wanted to censor it in the database so I couldn’t see them.

    # March 25, 2012 at 8:00 am

    what I would do is hash it using the md5() function then store it hashed. When a user inputs their login details you hash the password input they made again and compare it to the hashed stored password.

    This isn’t unbreakable though.

    If you want more secure then you should look into which will give you more options :) – but that’s the basic idea anyway :)

    # March 26, 2012 at 12:44 am

    Yes, hashing is the proper way to do this. While an encrypted password may seem safe, it isn’t. All a hacker would need to do is get a copy of the password fields and then run a brute force cracker on the data to figure out your encryption key.

    Hashing is more secure in that no matter what the hacker does with the hashed data, he may have no way of knowing what process lead to the original password becoming the final hash value.

    You could have salted it as such…


    …or many other ways.

    I know you want to prevent yourself from seeing them, but honestly, there is a big difference between you seeing them and acting upon seeing them. You have control of the database, so seeing a users password is the least thing to be concerned with. You should be trying to prevent people other than yourself from seeing those passwords, or ever getting their hands on them.

    When it comes to encryption and hashes, follow these…

    – Only encrypt individual things, and use different encryption methods for multiple things. Eg, do not encrypt each password with the same encryption method, because if one can be decrypted, they all can be decrypted.
    – use hashes for comparing sensitive data, rather than using the raw data itself. Use multiple hashing algorithms if you want to be extra sure a comparison is current. Search for SHA, md5 and cryptographic hashing in general

    # March 26, 2012 at 7:59 am

    Thank you all so much, I’ll try to look into all of this and I’m sure I’ll figure something out :)

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.