PHP encryption and decryption instance analysis,
This article describes the PHP encryption and decryption method. Share to everyone for your reference, as follows:
Cryptographic 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 result is 11111111111111111echo '
'. String2secret ("11111111111111111");//Display garbled echo '
';//$string: String to encrypt $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); $dynKeyPart 1 = substr ($dynKey, 0, 20); $dynKeyPart 2 = substr ($dynKey, 20); $fixedKeyPart 1 = substr ($fixedKey, 0, 20); $fixedKeyPart 2 = substr ($fixedKey, 20); $key = hash (' SHA1 ', $dynKeyPart 1. $fixedKeyPart 1. $dynKeyPart 2. $fixedKeyPart 2); $string = $isEncrypt? $fixedKeyPart 1. $string. $dynKeyPart 2: (Isset ($string {339})? Gzuncompress (Base64_decode (substr ($string,)): 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%));} return $isEncrypt? $dynKey. Str_replace (' = ', ' ', Base64_encode ($n > 299? gzcompress ($result): $result)): substr ($result, 20,-20);} EchoStrlen (Dencrypt ("12345678912345"));
I hope this article is helpful to you in PHP programming.
Articles you may be interested in:
- SSL encryption decryption, authentication, signature method under PHP (very simple)
- Detailed PHP encryption decryption string function with source download
- PHP encapsulated string encryption and decryption function
- thinkphp Development (Message encryption and decryption)
- A detailed explanation of PHP cryptographic decryption functions
- PHP implementation of enhanced encryption decryption class instance
- How to use PHP RSA encryption and decryption
- PHP Encryption Decrypted String summary
- An example analysis of PHP encryption and decryption class
- PHP interface Data encryption, decryption, authentication signature
- PHP RSA encryption and decryption using the detailed
- PHP Custom Encryption and decryption program instances
http://www.bkjia.com/PHPjc/1084530.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084530.html techarticle PHP encryption and decryption instance analysis, this article describes the PHP encryption and decryption method. Share to everyone for your reference, as follows://Cryptographic function String2secret ($str) {$key = "123"; $td ...