Example introduction of PHP encryption and decryption method
This article mainly explains the "PHP encryption and decryption method example introduction", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "PHP encryption and decryption method example introduction" bar!
This paper gives an example of PHP encryption and decryption method. Share with you for your reference, the details are as follows:
/ / encryption function string2secret ($str) {$key = "123"; $td = mcrypt_module_open (MCRYPT_DES,'','ecb',''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); $ks = mcrypt_enc_get_key_size ($td); $key = substr (md5 ($key), 0, $ks); mcrypt_generic_init ($td, $key, $iv); $secret = mcrypt_generic ($td, $str); mcrypt_generic_deinit ($td) Mcrypt_module_close ($td); return $secret;} / / decrypt function secret2string ($sec) {$key = "123"; $td = mcrypt_module_open (MCRYPT_DES,'','ecb',''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); $ks = mcrypt_enc_get_key_size ($td); $key = substr (md5 ($key), 0, $ks); mcrypt_generic_init ($td, $key, $iv); $string = mdecrypt_generic ($td, $sec) Mcrypt_generic_deinit ($td); mcrypt_module_close ($td); return trim ($string);} echo secret2string (string2secret ("11111111111111111")); / / display the result as 11111111111111111echo'
'.string2secret ("11111111111111111"); / / shows garbled echo'
'; / / $string: string to be encrypted $isEncrypt=true: encrypt $isEncrypt=false: decrypt function dencrypt ($string, $isEncrypt=true, $key = "KEY_SPACE") {if (! isset ($string {0}) | |! isset ($key {0})) {return false;} $dynKey = $isEncrypt? Hash ('sha1', microtime (true)): substr ($string, 0,40); $fixedKey = hash (' sha1', $key); $dynKeyPart1 = substr ($dynKey, 0,20); $dynKeyPart2 = substr ($dynKey, 20); $fixedKeyPart1 = substr ($fixedKey, 0,20); $fixedKeyPart2 = substr ($fixedKey, 20); $key = hash ('sha1', $dynKeyPart1. $fixedKeyPart1. $dynKeyPart2. $fixedKeyPart2); $string = $isEncrypt? $fixedKeyPart1. $string. $dynKeyPart2: (isset ($string {339})? Gzuncompress (base64_decode (substr ($string, 40)): base64_decode (substr ($string, 40)); $n = 0; $result =''; $len = strlen ($string); for ($n = 0; $n
< $len; $n++) { $result .= chr(ord($string{$n}) ^ ord($key{$n % 40})); } return $isEncrypt ? $dynKey . str_replace('=', '', base64_encode($n >299? Gzcompress ($result): $result): substr ($result, 20,-20);} echo strlen (dencrypt ("12345678912345")); Thank you for reading, the above is the "PHP encryption and decryption method example introduction" content, after the study of this article, I believe you have a deeper understanding of the PHP encryption and decryption method example introduction of this problem, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!