Solved

Encrypt and decrypt passwords in php

Posted on 2016-09-22
7
27 Views
Last Modified: 2016-09-22
What is the best way to encrypt a password to store in mysql databese? I mean in both ways ( encryption and decryption)?

I read about md5 is not enough since it's easly be decrypted....

What do you suggest i should do to hash passwords and decrypt it?
0
Comment
Question by:Braveheartli
7 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 41811555
The best way? The best way is not to store passwords. Store a salted hash instead.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41811564
Hashes like MD5 and SHA are one-way functions.  MD5 takes some serious computing power to break it.  SHA takes more.  Where there is no financial reward for 'breaking' the passwords, MD5 is still used because it is good enough.

This page addresses what PHP provides: http://php.net/manual/en/faq.passwords.php  And more info: http://phpsec.org/articles/2005/password-hashing.html
0
 
LVL 20

Assisted Solution

by:Russ Suter
Russ Suter earned 333 total points
ID: 41811567
There are 3 ways to handle credential validation: They are (in order of best to worst solution)

1. Store a hashed value using a strong hashing algorithm like SHA-256. When a user provides his/her credentials, hash the input and compare the hashes. If they match then you can authenticate the user. This is the best practice and should be used in 99% of cases.

2. Store the password in an encrypted format. When a user provides credentials, decrypt the stored value and compare it to the plain-text input. Use this only when necessary for some reason. If you have a requirement that passwords are recoverable this is the solution.

3. Store the password in plain-text. Use this method only if you're a complete idiot.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 167 total points
ID: 41811574
MD5 cannot be decrypted. Hashes are one-way functions. MD5 is considered weaker than other hashes, and you have databases out there that try to "decrypt" hashes by simply storing billions of varieties of hashes so you can find a matching one. Salting the hash refers to adding a known prefix like "Braveheartli" to the beginning of every string before it is hashed. That way, even if you do find a matching hash in some database out there, it won't work once the salting takes place because the salting makes the string different.

You should never depend on decrypting passwords. Any kind of decryption is weak. As ste5an said, use a salted hash. You can use salted MD5 or SHA1 or something stronger, but it's generally your best way to go.
0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41811596
Thank you all,
So i should use salted hash and to store passwords, when i check the passwords i actually check the hash version of the password to compare if it is valid or not?

Right?
0
 
LVL 20

Accepted Solution

by:
Russ Suter earned 333 total points
ID: 41811612
To clarify what you wrote above. You don't store the passwords anywhere. You use a hashing function with the password as your input and store the resulting hashed value. You then hash any user input in the same manner and compare the resulting hashes to determine if there is a match. The only known attack on a hash is brute force. MD5 has been around a long time and is one of the weaker hashing algorithms. SHA-256 is the preferred method these days. Regardless of which hashing algorithm you use it is guaranteed to be more secure than storing encrypted data.
0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41811656
Thank you all
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question