A simple encryption and decryption code for PHP

Source: Internet
Author: User
Tags crypt decrypt md5 strlen

  Share a PHP encryption and decryption class, when the user is registered to send mail validation when the estimated use of the code as follows

    Code as follows: Class syscrypt{ private $crypt _key= ' http://www.jb51.net ';//key  public function __ Construct ($crypt _key) {  $this->crypt_key= $crypt _key;  }  public function Encrypt ($txt) {  Srand (Double) microtime () *1000000);   $encrypt _key=md5 (rand (0,32000));   $CTR = 0;   $tmp = ';   for ($i =0 $i <strlen ($txt); $i + +) {    $ctr = $ctr ==strlen ($encrypt _key) 0: $ctr;     $tmp. =$ encrypt_key[$ctr]. ($txt [$i]^ $encrypt _key[$ctr + +]);  }   return Base64_encode (Self::__key ($tmp, $this->crypt_key)); &NBSP}  public function Decrypt ($txt) {  $txt =self::__key (Base64_decode ($txt), $this->crypt_key);   $tmp = ';   for ($i =0 $i <strlen ($txt); $i + +) {    $MD 5= $txt [$i];     $tmp. = $txt [+ + $i]^ $MD 5;  }   return $tmp; &NBSP}  private function __key ($txt, $encrypt _key) {  $encrypt _key=md5 ($encrypt _key);   $CTR =0;   $ Tmp= ';   for ($i =0; $i <strlen ($txt);$i + +) {    $ctr = $ctr ==strlen ($encrypt _key) 0: $ctr     $tmp. = $txt [$i]^ $encrypt _key[$ctr + +];  }   return $tmp; &NBSP}  public function __destruct () {  $this->crypt_key=null;  }}     How this class is used:   code is as follows: $SC =new syscrypt (' http://www.jb51.net '); $text = ' yhm.1234@163.com '; $test 1= $sc->encrypt ($text); Echo ' original: ', $text; echo ' <br/> '; Echo ' ciphertext: ', $test 1; echo "<br/>"; Echo ' Decrypt: ', $sc->decrypt ($test 1);     Output similar:   Original: yhm.1234@163.com cipher: bs8dbfu6aiocnfflvgzqmgrgbduoylezbxoazgo0bjk= decryption: yhm.1234@163.com  
Related Article

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.