Masked password in php
-
12-12-2019 - |
Question
Hi There is one xml in which i am assigning the value of password that i get by third party . I want to masked in it. I want to hide that password. Code is in php. Is it possible to mask password in php ?
Solution
You can encrypt the password using the following:
define('SALT', 'atopsecretphrase');
function encrypt($text)
{
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
function decrypt($text)
{
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SALT, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
$encryptedmessage = encrypt("mypassword");
echo decrypt($encryptedmessage);
OTHER TIPS
You can hash your password with md5()
or sha1()
If you need to pass the password on the best you can do is encrypt the password.
If you only need to check the password you should look at hashing. See: http://phpsec.org/articles/2005/password-hashing.html
You could save it hashed. For example, sha1($password)
will already return the same hash for the same password, but it cannot be decrypted.
That way the password is safe, and you could always take the input of the user, hash it the same way and compare his password entered with the one in the XML file.
Otherwise, another solution is the write your own encryption/decryption algorithm instead of hashing.
Try the following:
echo md5("password");
Will return:
5f4dcc3b5aa765d61d8327deb882cf99