A set of PHP reversible encryption decryption algorithm instance code

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

  This article mainly introduces a set of PHP reversible encryption and decryption algorithm instance code, a friend in need can refer to the

For most cryptographic cryptography, 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 and decryption algorithm is needed.   The following set of PHP functions is the way to implement this cryptographic decryption: The   encryption algorithm is as follows: The code is as follows: function Encrypt ($data, $key) {  $key = MD5 ($key);     $x  = 0;     $len = 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: The   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 + +)     {        if ($x = = $l)     &NBS P     {          $x = 0        }         $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) is required for the process of encrypting and decrypting the   above. Copy code code as follows: $data = ' php Cryptographic solutionSecret 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:   GNICSOZZG+HNS9ZCFEA7SEFNGHXF PHP encryption and decryption algorithm   from the above results can be seen, this is a set of reversible encryption and decryption algorithm, can be used for partial restoration of data encryption.  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.