This article introduces the content is about the PHP string encryption enhanced version, has a certain reference value, now share to everyone, the need for friends can refer to
To increase the security of your data
Avoid multiple encryption of the same character to get a consistent result
You can set the ciphertext validity period
A custom encryption method is listed below.
How to use
Put Mcrypt.class.php into the class file in your project and then introduce it in a require way where you need it, if more references are required than the parent controller can reference:
Encryption method: Mcrypt::encode ($STR, $n);
Decryption method: Mcrypt::d ecode ($STR, $n);
$Str must need to decrypt the string
$n non-essential ciphertext validity period, unit s,0 for permanent effect,
Require ("Mcrypt.class.php"); $code = Mcrypt::encode (' Sajkfcasjcla ', ' 7580 '); echo "code-". $code; $code _ans = Mcrypt:: Decode ("$code", ' 7580 '); echo "answer-". $code _ans;
Mcrypt.class.php
/** string and decryption class; * Once a secret, and time decryption valid * can be used for encryption & dynamic key generation */class mcrypt{public $default _key = ' a!taka:dlmcldev,e ';/** * character encryption, one secret at a time, can be Timed decryption Valid * @param string $string original * @param string $key key * @param int $expiry ciphertext validity, unit s,0 is permanently valid * @return String encrypted content * * public static function Encode ($string, $key = ", $expiry = 0) {$ckeyLength = 4; $key = MD5 ($key? $key: $this->default_key); Decryption Key $keya = MD5 (substr ($key, 0, 16)); Do data integrity Verification $KEYB = MD5 (substr ($key, 16, 16)); Ciphertext for change generation (initialization vector iv) $KEYC = SUBSTR (MD5 (Microtime ()),-$ckeyLength); $cryptkey = $keya. MD5 ($keya. $KEYC); $keyLength = strlen ($cryptkey); $string = sprintf ('%010d ', $expiry? $expiry + Time (): 0). substr (MD5 ($string. $keyb), 0, 16). $string; $stringLength = strlen ($string); $rndkey = Array (); for ($i = 0; $i <= 255; $i + +) {$rndkey [$i] = Ord ($cryptkey [$i% $keyLength]); } $box = Range (0, 255); Scramble the key book to add randomness for ($j = $i = 0; $i <; $i ++) {$j = ($j + $box [$i] + $rndkey [$i])% 256; $tmp = $box [$i]; $box [$i] = $box [$j]; $box [$j] = $tmp; }//Add decryption, from the key book to obtain the key to the XOR, and then into a character $result = '; for ($a = $j = $i = 0; $i < $stringLength; $i + +) {$a = ($a + 1)% 256; $j = ($j + $box [$a])% 256; $tmp = $box [$a]; $box [$a] = $box [$j]; $box [$j] = $tmp; $result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j])% 256])); } $result = $keyc. Str_replace (' = ', ' ', Base64_encode ($result)); $result = str_replace (Array (' + ', '/', ' = '), array ('-', ' _ ', '. '), $result); return $result; }/** * Character decryption, one secret at a time, can be timed decryption valid * @param string $string ciphertext * @param string $key decryption key * @return string decrypted content */public static F Unction decode ($string, $key = ') {$string = Str_replace (Array ('-', ' _ ', '. '), Array (' + ', '/', ' = '), $string); $ckeyLength = 4; $key = MD5 ($key? $key: $this->default_key); Decryption Key $keya = MD5 (substr ($key, 0, 16)); Do data integrity Verification $KEYB = MD5 (substr ($key, 16, 16)); Ciphertext for change generation (initialization vector iv) $KEYC = substr ($string, 0, $ckeyLength); $cryptkey = $keya. MD5 ($keya. $KEYC); $keyLength = strlen ($cryptkey); $string = Base64_decode (substr ($string, $ckeyLength)); $stringLength = strlen ($string); $rndkey = Array (); for ($i = 0; $i <= 255; $i + +) {$rndkey [$i] = Ord ($cryptkey [$i% $keyLength]); } $box = Range (0, 255); Scrambled key book to increase randomness for ($j = $i = 0; $i < $i + +) {$j = ($j + $box [$i] + $rndkey [$i])% 256; $tmp = $box [$i]; $box [$i] = $box [$j]; $box [$j] = $tmp; }//Add decryption, from the key book to obtain the key to the XOR, and then into a character $result = '; for ($a = $j = $i = 0; $i < $stringLength; $i + +) {$a = ($a + 1)% 256; $j = ($j + $box [$a])% 256; $tmp = $box [$a]; $box [$a] = $box [$j]; $box [$j] = $tmp; $result . = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j]) (% 256])); if (substr ($result, 0, ten) = = 0 | | substr ($result, 0,)-time () > 0) && substr ($result, 10, 16 ) = = substr (MD5 ($result, 0)) {return substr ($result, 26); } else {return '; } }}