How to encrypt and decrypt string in PHP

Sometimes we should encrypt some data, and save these data in client computer, however, we also should decrypt these data when we need to use. and how to create encrypt and decrypt? Source code below gives us an example:

<?php

function encrypt($data, $key) {
 $prep_code = serialize($data);
 $block = mcrypt_get_block_size('des', 'ecb');
 if (($pad = $block - (strlen($prep_code) % $block)) < $block) {
 $prep_code .= str_repeat(chr($pad), $pad);
 }
 $encrypt = mcrypt_encrypt(MCRYPT_DES, $key, $prep_code, MCRYPT_MODE_ECB);
 return base64_encode($encrypt);
 }

function decrypt($str, $key) {
 $str = base64_decode($str);
 $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
 $block = mcrypt_get_block_size('des', 'ecb');
 $pad = ord($str[($len = strlen($str)) - 1]);
 if ($pad && $pad < $block && preg_match('/' . chr($pad) . '{' . $pad . '}$/', $str)) {
 $str = substr($str, 0, strlen($str) - $pad);
 }
 return unserialize($str);
 }

?>

and we can use these tow function like this:

<?php

 $key = 'tutorialinsight.com';
 $data = array('id' => 100, 'username' => 'customer', 'password' => 'e10adc3949ba59abbe56e057f20f883e');
 $snarr = serialize($data);
 $en = encrypt($data, $key);
 $de = decrypt($en, $key);
 echo "encrypt origin:";
 print_r($data);
 echo "
 key:$key

encrypt result:$en

decrypt result:";
 print_r($de);

?>