Intro to Encryption Techniques in PHP
-----------------------------------------------
Written by Giovanni Tropeano 11/2004
<<< Table of Contents >>>
- ...Preface
- ...One-Way Encryption
- ...Symmetric Encryption
- ...Asymmetric Encryption
- ...Summary
::: Preface :::
PHP uses cryptography to encrypt and decrypt text. Cryptography is a process that uses various mathematical formulas to encode and decode data. It uses algorithms to encrypt different modes of communication, such as text, messages, and signals. Plain text is the term used to refer to the message that is to be transmitted. Encrypted text is plain text in encrypted form.
PHP supports three types of encryption techniques: one-way, symmetric, and asymmetric. In all three techniques, PHP provides several methods to encrypt and decrypt data.
I decided to write this today (Sat Nov 6 2004) because I struggled (still am) with one of the challenges here (trying to do it in PHP). What better way to learn than to teach. I think some ancient chinese guy said that or something, didn't he?
Let's begin...
::: One-Way Encryption :::
In the one-way encryption technique, a string of text is only encoded, not decoded. The algorithms for one-way encryption are called hash algorithms. PHP uses the Message Digest (MD) hash algorithm, MD5, for one-way encryption. MD5 accepts a string as input and converts it to a unique 128-bit fingerprint of the message. MD5 is an irreversible process because it is not possible to decipher a message after it is converted into 128-bit fingerprint. Let's take a look at a diagram:
PHP implements the MD5 hash algorithm using the md5 function. Listing 1 shows how to use the md5 function:
Listing 1: Using the md5 Function
<?php
$msg = " Trope stinks at Geek Challenges ";
$encrypted_text = md5 ($msg);
echo("<b>Plain Text : </b>");
echo($msg);
echo("<p><b>Encrypted Text : </b>");
echo($encrypted_text);
?>
In the above listing, a string argument, Trope stinks at Geek Challenges, is passed to the md5 function. The encrypted 128-bit fingerprint value is returned to the variable encrypted_text. Figure 2 shows the 128-bit fingerprint of the message:
PHP also uses cyclic redundancy checksum (CRC32), which is another hash algorithm, for one-way encryption. The CRC32 algorithm converts a plain text message into a 32-bit fingerprint. PHP provides a mhash library that consists of functions such as md5. The mhash library function is used for implementing the algorithms in the mhash library. The mhash library function accepts two arguments - the first is a hashing constant and the second is the string to be encrypted. Table 1-1 lists the hashing constants used for hash algorithms:
The following listing shows how to use the mhash library function in the CRC32 algorithm:
Listing 2: Implementing the mhash Library Function
<?php
echo("<h3> Implementing mhash </h3>");
$plain_text = "Damn why won't PHP come naturally to me!";
$encrypted_text = mhash(MHASH_CRC32, $plain_text);
echo("<p><b> Encrypted text is : </b>");
echo($encrypted_text);
?>
The above listing shows how a plain text is encrypted using the mhash function. In the listing, the variable $plain_text stores the string to be converted. The mhash function accepts the MHASH_CRC32 constant as the first argument and the $plain_text variable as the second argument and returns a 32-bit fingerprint of the message stored in the variable $encrypted_text.
::: Symmetric Encryption :::
Symmetric encryption uses a special code called a key. The hash algorithm uses the key on the plain text to generate the encrypted text and on the encrypted text to decrypt it into the original plain text. The sender and receiver of the encrypted message should know the value of the key to be able to send or receive messages. The drawback of the symmetric encryption technique is that if the key is available to anyone other than the sender or receiver of the message, the entire encryption process fails. To ensure the security of the message, it is essential to ensure the secrecy of the key. Figure 3 illustrates the process of symmetric encryption:
Figure 3: Symmetric Encryption
This figure illustrates the symmetric encryption technique. The plain text is converted to encrypted text using the encryption algorithm and the key value. The encrypted text is converted back to plain text using the decryption algorithm and key value.
PHP provides several algorithms for symmetric encryption in the mcrypt library. It also provides the mcrypt_ecb function to implement the algorithms of the mcrypt library.
Listing 3: Encrypting Data Using the mcrypt_ecb Function
<?php
echo("<h3> Symmetric Encryption </h3>");
$key_value = "KEYVALUE";
$plain_text = "PLAINTEXT";
$encrypted_text = mcrypt_ecb(MCRYPT_DES, $key_value, $plain_text, MCRYPT_ENCRYPT);
echo ("<p><b> Text after encryption : </b>");
echo ( $encrypted_text );
$decrypted_text = mcrypt_ecb(MCRYPT_DES, $key_value, $encrypted_text, MCRYPT_DECRYPT);
echo ("<p><b> Text after decryption : </b>");
echo ( $decrypted_text );
?>
This listing uses the mcrypt_ecb function to encrypt as well as decrypt text. The mcrypt_ecb function accepts four arguments. The first argument is the algorithm constant, the second is the key, and the third is the string to be encrypted. The fourth argument is MCRYPT_ENCRYPT when the message is encrypted and MCRYPT_DECRYPT when the message is decrypted.
Figure 4 shows the encrypted and decrypted texts using the key value in the symmetric encryption technique:
::: Asymmetric Encryption :::
Symmetric encryption uses a special code called a key. The hash algorithm uses the key on the plain text to generate the encrypted text and on the encrypted text to decrypt it into the original plain text. The sender and receiver of the encrypted message should know the value of the key to be able to send or receive messages. The drawback of the symmetric encryption technique is that if the key is available to anyone other than the sender or receiver of the message, the entire encryption process fails. To ensure the security of the message, it is essential to ensure the secrecy of the key. Figure 5 illustrates the process of symmetric encryption:
::: SUMMARY :::
You should now have a good high level understanding of how you can encrypt data using PHP. Of course it's beyond the scope of this article to go into detail on each encryption algorithm, but you can search and find tons of info on each.
Ciao!
TroPe |