Adding a random key makes the ciphertext irregular. even if the original text and the key are exactly the same, the encryption results will be different each time, increasing the difficulty of cracking.
Well, a very good php string encryption function,
Recently, a powerful encryption and decryption function was found in discuz.
This function can encrypt and restore strings within a specified period of time.
In this way, we can use this function for many purposes, such as the token encrypted transmission and temporary password for single sign-on.
/**
* @ Param string $ string original text or ciphertext
* @ Param string $ operation (ENCODE | DECODE). The default value is DECODE.
* @ Param string $ key
* @ Param int $ expiry ciphertext validity period. Valid during encryption. unit: Seconds. 0 indicates permanent validity.
* @ Return string the original text after processing or the ciphertext after base64_encode processing
*
* @ Example
*
* $ A = authcode ('ABC', 'encode', 'key ');
* $ B = authcode ($ a, 'Decode', 'key'); // $ B (abc)
*
* $ A = authcode ('ABC', 'encode', 'key', 3600 );
* $ B = authcode ('ABC', 'Decode', 'key'); // within an hour, $ B (abc); otherwise, $ B is empty.
*/
Function authcode ($ string, $ operation = 'Decode', $ key = '', $ expiry = 3600 ){
$ Ckey_length = 4;
// The random key length ranges from 0 to 32;
// Adding a random key can make the ciphertext irregular. even if the original text and the key are identical, the encryption results will be different each time, increasing the difficulty of cracking.
// The larger the value, the larger the ciphertext change law. The ciphertext change is equal to the power of $ ckey_length of 16.
// When this value is 0, no random key is generated
$ Key = md5 ($ key? $ Key: 'default _ key'); // you can enter the default key value here.
$ Keya = md5 (substr ($ key, 0, 16 ));
$ Keyb = md5 (substr ($ key, 16, 16 ));
$ Keyc = $ ckey_length? ($ Operation = 'decode '? Substr ($ string, 0, $ ckey_length): substr (md5 (microtime (),-$ ckey_length )):";
$ Cryptkey = $ keya. md5 ($ keya. $ keyc );
$ Key_length = strlen ($ cryptkey );
$ String = $ operation = 'decode '? Base64_decode (substr ($ string, $ ckey_length): sprintf ('% 010d', $ expiry? $ Expiry + time (): 0). substr (md5 ($ string. $ keyb), 0, 16). $ string;
$ String_length = strlen ($ string );
$ Result = ";
$ Box = range (1, 0,255 );
$ Rndkey = array ();
For ($ I = 0; $ I $ rndkey [$ I] = ord ($ cryptkey [$ I % $ key_length]);
}
For ($ j = $ I = 0; I I <256; $ I ++ ){
$ J = ($ j + $ box [$ I] + $ rndkey [$ I]) % 256;
$ Tmp = $ box [$ I];
$ Box [$ I] = $ box [$ j];
$ Box [$ j] = $ tmp;
}
For ($ a = $ j = $ I = 0; $ I <$ string_length; $ 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 ($ operation = 'decode '){
If (substr ($ result, 0, 10) = 0 | substr ($ result, 0, 10)-time ()> 0) & substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ keyb), 0, 16 )){
Return substr ($ result, 26 );
} Else {
Return ";
}
} Else {
Return $ keyc. str_replace ('=', ", base64_encode ($ result ));
}
}
/******** An example **********/
Echo authcode ("pianyu blog ",");
Echo'
';
Echo authcode ("abc ",");
?>
Browser output:
2cd2jqYh0repuAg4ozEgiVfd1Ex5TSOUmQsznQjpWndjsPmIIknDHNw
83d8xTnzqXo3pQ9nrkLJj49ArkobS3mBNR28NhUnitg