Click here to Skip to main content
16,021,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HY!
can any one give me an idea that which is the best and latest way to encrypt user data in php and also decrypt it whenever we want?
Posted
Updated 27-Apr-13 5:50am
v2
Comments
[no name] 27-Apr-13 11:51am    
"give me an idea".... www.google.com

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
<?php
if (hash('sha256', $givenPsswd) == $hashedPsswd) // change $givenPsswd and $hashedPsswd into the names of your variables
{
 // given password is correct
}
?>

Hope this helps.
 
Share this answer
 
Comments
msz900 27-Apr-13 13:15pm    
actually iam using the PDO interaction with mysql is the above example
work with PDO?
Thomas Daniels 27-Apr-13 13:18pm    
What do you mean exactly?
msz900 27-Apr-13 13:37pm    
i mean that for now aday's Msql are going down and mysqli is not much powerful so, i decide to use PDO for the conection of Mysql..
and i just want the idea of enrypting/decrypting the data which work's with Pdo.
Thomas Daniels 27-Apr-13 13:39pm    
You can encrypt the data before adding it into your database.
msz900 27-Apr-13 13:44pm    
yes
The best way is to use PHP 5.5 hashing algorithm function...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900