Function source Code
Copy CodeThe code is as follows:
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);
}
Calling functions
Copy CodeThe code is as follows:
$key = ' okyo.cn ';
$data = array (' id ' = +, ' username ' = ' customer ', ' password ' = ' e10adc3949ba59abbe56e057f20f883e ');
$snarr = serialize ($data);
$en = Encrypt ($data, $key);
$de = Decrypt ($en, $key);
echo "Crypto prototype:";
Print_r ($data);
echo "
Key: $key
Encryption Result: $en
Decryption result: ";
Print_r ($de);
http://www.bkjia.com/PHPjc/326127.html www.bkjia.com true http://www.bkjia.com/PHPjc/326127.html techarticle The code of the function source code is copied as follows: Functions Encrypt ($data, $key) {$prep _code = serialize ($data); $block = Mcrypt_get_block_size (' des ', ' ECB '); if ($pad = $block-(strlen ($PR ...