This article describes the PHP encryption and decryption class. Share to everyone for your reference. The specific analysis is as follows:
This code supports array encryption, ciphertext expiration, various symmetric encryption
Where the parameters are as follows:
* @use ption::en ($string, $key);
* @param string $string strings that need to be encrypted
* @param String $skey key
* @param int $expiry ciphertext validity, valid when encrypted, unit seconds, 0 for permanent validity
* @return String
1. PHP code is as follows:
* *-Tool library-encryption password/class Ption {private static $original = Array (' = ', ' + ', '/');
private static $later = Array (' o0o0o ', ' o0o0o ', ' oo00o '); function __construct () {} private static function md5 ($skey = ') {$skey = $skey? $skey: ' UI ';//uicms::_config
(' Security/authkey ');
return MD5 (substr ($skey, 0, 16));
/** * @use ption::en ($string, $key); * @param string $string strings that need to be encrypted * @param string $skey key * @param int $expiry ciphertext validity period, time valid for encryption, unit seconds, 0 for permanent valid * @return STR ing/static public function en ($string = ', $skey = ', $expiry =0) {if (Is_array ($string)) {$string = Json_encode ($string);
Uicms::json ($string, true, ' en ');
$string = Str_pad ($expiry $expiry + time:0, 0). $string;
$STRARR = Str_split (Base64_encode ($string));
$strCount = count ($STRARR);
$skey = STATIC::MD5 ($skey);
foreach (Str_split ($skey) as $key => $value) {$key < $strCount && $strArr [$key].= $value; Return Str_replace (self:: $original, Self:: $later, Join (", $STRARR));
/** * @use ption::d E ($string, $key); * @param string $string strings to be decrypted * @param string $skey key * @return string/static Public function de ($string = ', $
Skey = ') {$STRARR = Str_split (Str_replace (self:: $later, Self:: $original, $string), 2);
$strCount = count ($STRARR);
$skey = STATIC::MD5 ($skey); foreach (Str_split ($skey) as $key => $value) {$key < $strCount && $STRARR [$key][1] = = $value &&am P
$STRARR [$key] = $STRARR [$key][0];
$result = Base64_decode (Join (', $STRARR));
if (substr ($result, 0,) = = 0 | | substr ($result, 0)-time > 0) {return substr ($result, 10);
else {return false; }
}
}
2. The usage is as follows:
$str [' username '] = ' oschina ';
$str [' pw '] = ' 123456 ';
$str [' huoxin '] = '!@#$%^& ';
echo "string:". $str. "<br/>";
echo "Encode:". ($enstring = Ption::en ($STR)). ' <br/> ';
echo "Decode:". Ption::d E ($enstring);
I hope this article will help you with your PHP program design.