This example describes the DES encryption and decryption instance implemented by PHP. Share to everyone for your reference, specific as follows:
<?php $key = ' very important data ';
function Jiami ($key, $STR) {/* Open module, and create IV */$td = Mcrypt_module_open (' des ', ', ', ' ECB ', ');
$TD = Mcrypt_module_open (Mcrypt_des, ', MCRYPT_MODE_CBC, ');
$TD = Mcrypt_module_open (' des ', ', ', ' CBC ', ');
$key = substr ($key, 0, Mcrypt_enc_get_key_size ($TD));
$iv _size = mcrypt_enc_get_iv_size ($TD);
$iv = Mcrypt_create_iv ($iv _size, Mcrypt_rand);
/* Initialize Encryption Handle */if (Mcrypt_generic_init ($TD, $key, $iv) = = 1) {return FALSE;
}/* Encrypt data */$c _t = Mcrypt_generic ($TD, $STR);
/* Clean up */mcrypt_generic_deinit ($TD);
Mcrypt_module_close ($TD);
return $c _t;
function Jiemi ($key, $STR) {/* Open module, and create IV */$td = Mcrypt_module_open (' des ', ', ', ' ECB ', ');
$TD = Mcrypt_module_open (Mcrypt_des, ', MCRYPT_MODE_CBC, ');
$TD = Mcrypt_module_open (' des ', ', ', ' CBC ', ');
$key = substr ($key, 0, Mcrypt_enc_get_key_size ($TD)); $iv _size = mcrypt_enc_get_iV_size ($TD);
$iv = Mcrypt_create_iv ($iv _size, Mcrypt_rand);
/* Initialize Encryption Handle */if (Mcrypt_generic_init ($TD, $key, $iv) = = 1) {return FALSE;
}/* Reinitialize buffers for decryption */$p _t = Mdecrypt_generic ($TD, $STR);
/* Clean up */mcrypt_generic_deinit ($TD);
Mcrypt_module_close ($TD);
Return trim ($p _t);
} $str = ' Hello world! '; Echo ' key: '. $key.
' <br> '; echo ' str: '. $str.
' <br> ';
$jiami = Jiami ($key, $STR); Echo ' Encrypt: '. $jiami.
' <br> ';
File_put_contents (' Jiamiqian.txt ', $str);
File_put_contents (' Jiamihou.txt ', $jiami);
$jiemi = Jiemi ($key, $jiami); Echo ' Decrypt: '. $jiemi.
' <br> ';
More interested in PHP related content readers can view the site topics: "PHP Encryption Method Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.