Home arrow PHP arrow Page 3 - Secure Encrypting and Decrypting for Your PHP Website

Data Encryption - PHP

In this conclusion to a three-part series on secure PHP programming, you'll learn how to validate inputs, handle hashing, use the MCrypt package, and more. This article is excerpted from chapter 21 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

  1. Secure Encrypting and Decrypting for Your PHP Website
  2. Taking Advantage of PEAR: Validate
  3. Data Encryption
  4. The MCrypt Package
By: Apress Publishing
Rating: starstarstarstarstar / 1
August 26, 2010

print this article



Encryption can be defined as the translation of data into a format that is intended to be unreadable by anyone except the intended party. The intended party can then decode, or decrypt, the encrypted data through the use of some secret—typically a secret key or password. PHP offers support for several encryption algorithms. Several of the more prominent ones are described here.

Tip  For more information about encryption, pick up the book Applied Cryptography: Protocols, Algorithms, and Source Code in C, Second Edition by Bruce Schneier (John Wiley & Sons, 1995). 

PHP’s Encryption Functions

Prior to delving into an overview of PHP’s encryption capabilities, it’s worth discussing one caveat to their usage, which applies regardless of the solution. Encryption over the Web is largely useless unless the scripts running the encryption schemes are operating on an SSL-enabled server. Why? PHP is a server-side scripting language, so information must be sent to the server in plain-text format before it can be encrypted. There are many ways that an unwanted third party can watch this information as it is transmitted from the user to the server if the user is not operating via a secured connection. For more information about setting up a secure Apache server, check out http://www.apache-ssl.org . If you’re using a different Web server, refer to your documentation. Chances are that there is at least one, if not several, security solutions for your particular server. With that caveat out of the way, let’s review PHP’s encryption functions.

Encrypting Data with the md5() Hash Function

The md5() function uses MD5, which is a third-party hash algorithm often used for creating digital signatures (among other things). Digital signatures can, in turn, be used to uniquely identify the sending party. MD5 is considered to be a one-way hashing algorithm, which means there is no way to dehash data that has been hashed using md5() . Its prototype looks like this:

string md5(string str)

The MD5 algorithm can also be used as a password verification system. Because it is (in theory) extremely difficult to retrieve the original string that has been hashed using the MD5 algorithm, you could hash a given password using MD5 and then compare that encrypted password against those that a user enters to gain access to restricted information.

For example, assume that your secret password toystore has an MD5 hash of 745e2abd7c52ee1dd7c14ae0d71b9d76 . You can store this hashed value on the server and compare it to the MD5 hash equivalent of the password the user attempts to enter. Even if an intruder gets hold of the encrypted password, it wouldn’t make much difference because that intruder can’t return the string to its original format through conventional means. An example of hashing a string using md5() follows:

$val = "secret";
$hash_val = md5 ($val);
// $hash_val = "5ebe2294ecd0e0f08eab7690d2a6ee69";

Remember that to store a complete hash, you need to set the field length to 32 characters.

The md5() function will satisfy most hashing needs. There is another much more powerful hashing alternative available via the mhash library. This library is introduced in the next section.

Using the mhash Library

mhash is an open source library that offers an interface to a wide number of hash algorithms. Authored by Nikos Mavroyanopoulos and Sascha Schumann, mhash can significantly extend PHP’s hashing capabilities. Integrating the mhash module into your PHP distribution is rather simple:

  1. Go to http://mhash.sourceforge.net and download the package source.
  2. Extract the contents of the compressed distribution and follow the installation instructions as specified in the INSTALL document. 
  3. Compile PHP with the --with-mhash option.   

On completion of the installation process, you have the functionality offered by mhash at your disposal. This section introduces mhash() , the most prominent of the five functions made available to PHP when the mhash extension is included.

Hashing Data with mhash

The function mhash() offers support for a number of hashing algorithms, allowing developers to incorporate checksums, message digests, and various other digital signatures into their PHP applications. Its prototype follows:

string mhash(int hash, string data [, string key])

Hashes are also used for storing passwords. mhash() currently supports the hashing algorithms listed here:

  1. ADLER32 
  2. CRC32 
  3. CRC32B 
  4. GOST 
  5. HAVAL 
  6. MD4 
  7. MD5 
  8. RIPEMD128 
  9. RIPEMD160 
  10. SHA1 
  11. SNEFRU 
  12. TIGER

Consider an example. Suppose you want to immediately encrypt a user’s chosen password at the time of registration (which is typically a good idea). You could use mhash() to do so, setting the hash parameter to your chosen hashing algorithm, and data to the password you want to hash:

$userpswd = "mysecretpswd";
$pswdhash = mhash(MHASH_SHA1, $userpswd);
echo "The hashed password is: ".bin2hex($pswdhash);

This returns the following:

The hashed password is: 07c45f62d68d6e63a9cc18a5e1871438ba8485c2

Note that you must use the bin2hex() function to convert the hash from binary mode to hexadecimal so that it can be formatted in a fashion easily viewable within a browser.

Via the optional parameter key , mhash() is also capable of determining message integrity and authenticity. If you pass in the message’s secret key, mhash() will validate whether the message has been tampered with by returning the message’s Hashed Message Authentication Code (HMAC). You can think of the HMAC as a checksum for encrypted data. If the HMAC matches the one that would be published along with the message, the message has arrived undisturbed.

>>> More PHP Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: