This article mainly introduces the PHP encryption and decryption class, the example analyzes the PHP implementation encryption and the decryption principle and the related technique, very has the practical value, the need friend may refer to under
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:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 The |
* *-Tool Library-Cryptographic solution 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 (' Secu Rity/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, the time of encryption valid, unit seconds, 0 for permanent valid * @return string */static Public function en ($string = ', $skey = ', $expiry =0) {if (Is_array ($string)) {$string = Json_encode ($st Ring); 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_repla CE (self:: $original, Self:: $later, Join (", $STRARR)); /** * @use ption::d E ($string, $key); * @param string $string @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 = STA TIC::MD5 ($skey); foreach (Str_split ($skey) as $key => $value) {$key < $strCount && $STRARR [$key][1] = = $value && $s trarr[$key] = $STRARR [$key][0]; $result = Base64_decode (Join (', $STRARR)); if (substr ($result, 0,) = 0 | | substr ($result, 0)-time > 0) {return substr ($result);} else {return fals E } } } |
2. The usage is as follows:
?
1 2 3 4 5 6 |
$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.