Hi,
You can use the
mcrypt_encrypt
and
mcrypt_decrypt
functions (PHP 4 >= 4.0.2, PHP 5):
http://php.net/manual/en/function.mcrypt-encrypt.php[
^]
http://php.net/manual/en/function.mcrypt-decrypt.php[
^]
Or the
openssl_encrypt
and
openssl_decrypt
functions (PHP 5 >= 5.3.0):
http://php.net/manual/en/function.openssl-encrypt.php[
^]
http://php.net/manual/en/function.openssl-decrypt.php[
^]
http://stackoverflow.com/a/6639179[
^]
But if you want to encrypt/decrypt a
password, use hashing, because you can't decrypt a hashed string.
The
hash
method (PHP 5 >= 5.1.2, PECL hash >= 1.1):
http://php.net/manual/en/function.hash.php[
^]
Q:
But if you can't decrypt it, how do you check whether a given password is correct?
A:
Try this (for example if your password is hashed using SHA-256:
<?php
if (hash('sha256', $givenPsswd) == $hashedPsswd)
{
}
?>
Hope this helps.