PHP - String Encryption

Recommend this page to Google

If you want to know how to encrypt a string in PHP, then you have come to the right place.

The easiest way to encrypt a string is to use the crypt() function. There are only 2 things you need to know:


How to encrypt a string

How to match an encrypted string

Encrypting a String

To encrypt a string, you have to do the following.

$password = "password";
$encryptedPassword = crypt($password);

If you want to see the encrypted string, print $encryptedPassword.

Matching an Encrypted String

There is no such function called decrypt(). This is because crypt uses a one-way encryption algorithm. This basically means that you can encrypt a string but can not decrypt it to get back the original string. If you can't see the original password that was encrypted, then how can you compare a password to make sure that it is a valid password. The answer is that you encrypt the string you want to compare it to and then compare the encryptions. Here is how you match the strings.

$verifyPassword = crypt($password,$encryptedPassword);

now you compare the 2 passwords (their encryptions)

if ($verifyPassword == $encryptedPassword)
echo "Password is valid";
echo "Invalid Password";

I know that I skipped over a lot of things, but the purpose of this article to simply get you started easily and quickly.

About the Author:

Copyright Nazim Rahman (c)


No votes yet