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.