Source: Internet
Author: User

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.
Related Article