PHP encryption algorithm for the realization of reversible encryption algorithm and decryption sharing

Source: Internet
Author: User
Tags chr decrypt md5 ord strlen

  For most password encryption, we can use MD5, SHA1 and other methods. Can effectively prevent data disclosure, but these methods apply only to data encryption that does not need to be restored. For information that needs to be restored, a reversible encryption decryption algorithm is required, and the following set of PHP functions is the method that implements this cryptographic decryption

The encryption algorithm is as follows: The   code is as follows: function Encrypt ($data, $key) {  $key = MD5 ($key);     $x  = 0;     $l En = strlen ($data);     $l  = strlen ($key);     for ($i = 0 $i < $len $i + +)     {        if ($x = = $l)     &NBS P     {          $x = 0        }         $char . = $key {$x};         $x + +;    }     for ($i = 0 $i < $len; $i + +)     {        $str. = Chr (ord ($data {$i}) + (Ord ($char {$i}))% 256);    }     return Base64_encode ($STR); The     decryption algorithm is as follows:       Code is as follows: function decrypt ($data, $key) {  $key = MD5 ($key);     $x = 0;     $data = Base64_decode ($data);     $len = strlen ($data);     $l = strlen ($key);     for ($i = 0; $i < $len; $i + +)     {&NBsp       if ($x = $l)           {          $x = 0;   &N Bsp    }         $char. = substr ($key, $x, 1);         $x + +;    }     for ($i = 0 $i < $len; $i + +)     {        ORD substr ( $data, $i, 1) < Ord (substr ($char, $i, 1))         {            $ST R. = Chr ((Ord (substr ($data, $i, 1)) + 256)-Ord (substr ($char, $i, 1));        }         else         {        & nbsp   $STR. = Chr (Ord (substr ($data, $i, 1))-Ord (substr ($char, $i, 1));        }    }     return $str; An encryption key (that is, parameter $key) needs to be used in the process of     the above cryptographic decryption.   Code as follows: $data = ' PHP encryption and decryption algorithm ';  //Encrypted information $key = ' 123 ';    //Key $encrypt = Encrypt ($data, $key); $decRypt = Decrypt ($encrypt, $key); Echo $encrypt, "n", $decrypt;     The output similar to the following results:     Code is as follows: GNICSOZZG+HNS9ZCFEA7SEFNGHXF PHP encryption and decryption algorithm     from the above results can be seen, This is a set of reversible encryption and decryption algorithms that can be used for some data encryption that needs to be restored.  

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.