PHP encryption and Decryption class instance analysis _php skill

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.