Our commonly used MD5 algorithm is not reversible theoretically, but there are powerful methods can be MD5 to calculate out, just according to the complexity of the time required to be different, but sometimes we want to encrypt the transmission of their data after the acceptance can be decrypted, I would like to provide you with a function.
For most password encryption, we can use MD5, SHA1 and other methods. Can effectively prevent data breaches, but these methods apply only to data encryption that does not need to be restored.
For information that needs to be restored, a reversible cryptographic decryption algorithm is required.
The following set of PHP functions is a way to implement this encryption decryption:
The encryption algorithm is as follows:
| The code is as follows |
Copy Code |
function Encrypt ($data, $key) { $key = MD5 ($key); $x = 0; $len = strlen ($data); $l = strlen ($key); for ($i = 0; $i < $len; $i + +) { if ($x = = $l) { $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 as follows |
copy code |
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) { $x = 0; } $char. = substr ($key, $x, 1); $x + +; } for ($i = 0; $i < $len; $i + +) { if (Ord (substr ($data, $i, 1)) < Ord (substr ($char, $i, 1))) { br> $str. = Chr ((Ord ($data, $i, 1) + substr)-Ord (substr ($char, $i, 1))); } Else { $str. = Chr (Ord (substr ($data, $i, 1))-Ord (substr ($char, $i, 1))); } } return $str; } |
The process of encrypting and decrypting above requires the use of an encryption key (i.e., parameter $key).
| The code is as follows |
Copy Code |
$data = ' PHP encryption and decryption algorithm '; Information that is encrypted $key = ' 123 '; Secret key $encrypt = Encrypt ($data, $key); $decrypt = Decrypt ($encrypt, $key); Echo $encrypt, "n", $decrypt; |
The above output will look similar to the following results:
Gnicsozzg+hns9zcfea7sefnghxf
PHP Encryption and decryption algorithm
As can be seen from the above results, this is a set of reversible encryption and decryption algorithms that can be used to encrypt some data that needs to be restored.
http://www.bkjia.com/PHPjc/629603.html www.bkjia.com true http://www.bkjia.com/PHPjc/629603.html techarticle The MD5 algorithm we used is theoretically irreversible, but there are powerful ways to calculate the MD5, just according to the complexity of the time required to be different, but sometimes I ...