Encryption Techniques for Your PHP Development | WebReference

Encryption Techniques for Your PHP Development

By Leidago Noabeb


Recently, an attacker hacked into my database and stole all the passwords and usernames stored there. Needless to say, I had to change everything and it cost me time and money. What made the crime easy for the attacker is that I never encrypted any of the passwords in the database. I've learned my lesson and now I'm passing along that wisdom with this article about encryption in PHP. Using some of the encryption techniques that PHP offers, you can safeguard your information in various ways.

One-way Encryption

Very few databases offer data encryption, so the developer must encrypt passwords before sending them to the database. One-way encryption is perhaps the safest way of storing and protecting passwords, and it is very widely used in the computer world. One-way encryption means that a password gets encrypted and cannot be decrypted again. So you have to remember the password, because you will not be able to recover it.

Every encryption method uses an algorithm, basically a blueprint of how the encryption works. Algorithms that achieve one-way encryption are called hashing algorithms, and they work by taking a string (for example, JohnDoe) and then creating a unique fingerprint (if you like) from it. PHP offers a hashing algorithm called MD5, which basically takes a string and returns a 128-bit fingerprint of it. To my knowledge, the MD5 algorithm has not been broken yet, because it would take a long time and some formidable computing power. Also, creating two inputs (or more) with the same fingerprints is would be very difficult.

Don't get me wrong, hashing algorithms are not 100% secure. They are subject to brute force attacks and as with any other algorithm can be broken given enough time and effort. Also, the length of time it takes to break a hashing algorithm is dependent on the complexity of the algorithm itself. Shorter passwords won't take very long to break.

I currently use the MD5 algorithm to protect my passwords, mostly because the original string (password) can never be recovered from the resulting fingerprint but we can hash the password when a user logs in and then compare the two.

Let's build a small login site to test the MD5. Use the following code to create a form that takes a password:

When the form sends the password, the code below takes it and turns it into a 32-character hash. Say we entered "HageGeingob" as the password; it would give us the following hash:

What I like about this hashing algorithm (and what makes it particularly useful) is that you can use the hash to compare it with the original password. So no one other than the owner of the password would know the password. This is how we compare the hash with the original password, which should also be hashed. Change the previous form to this:

Now enter the password that we entered before in the field that says "Enter Hash" and press the submit button next to it. You should get a result that says "Passwords match". So, how exactly did we compare the passwords? We hashed the fingerprint of the password called "HageGeingob" and then compared it to the hash value that we received previously:

Then we simply print out the results.

Another one-way encryption function that is fully supported by PHP is the SHA1() function. It works in the same way as MD5, but it produces a slightly longer hash. Let's modify our login form to accommodate the SHA1() function: