PHP Encrypt/Decrypt With Salt
Last Updated : Jan 1, 2023
IN - PHP
In this article we will show you the solution of PHP encrypt/decrypt with salt, here we needs to use ‘encryption method, hash algorithm, openssl_encrypt(), base64_encode() methods’ for do encryption with salt.
Generally encryption with salt is made up of random bits added to each password instance before its hashing.
Salts create unique passwords even in the instance of two users choosing the same passwords.
Step By Step Guide On PHP Encrypt/Decrypt With Salt :-
Here we used AES encryption method with CBC mode then first we have to hash the defined key ‘$privateKey’ so we used sha256 algorithm for hashing and return value stored on variable $key.
Then likewise we needs to hash ‘$secretKey’ result stored on ‘$ival’ variable.
The openssl_encrypt() function is used to encrypt data so we passing defined and hashed result of values to this method as per rules.
Encrypted binary data needs to passed on base64_encode method for convert binary data to an ASCII format then at last we printed encoded string on webpage.
<?php $privateKey = 'AA74CDCC2BBRT935136HH7B63C27'; $secretKey = '5fgf5HJ5g27'; $encryptMethod = "AES-256-CBC"; $string = 'IB12345'; $key = hash('sha256', $privateKey); $ival = substr(hash('sha256', $secretKey), 0, 16); $result = openssl_encrypt($string, $encryptMethod, $key, 0, $ival); $output = base64_encode($result); echo "Ecrypt Result : ".$output; ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
- Here we defined string for make encrypted i.e ‘AA74CDCC2BBRT935136HH7B63C27’ stored on variable ‘$privateKey’ then variable ‘$secretKey’ defined for holds secret key string and we choosing encryption method as ‘AES-256-CBC’.
- Variable ‘$string’ defined for holds ‘IB12345’ value those variables are needs for access openssl_encrypt() method. Now we have to convert secret, private keys into hash type string with the help of ‘sha256’ algorithm then results stored on respective variables ‘$key, $ival’.
- We have all arguments to use openssl_encrypt() method for make as encryption data then we passing ‘$string, $encryptMethod, $key, 0, $ival’ as parameters and returned result of binary data stored on variable ‘$result’.
- At last we passing ‘$result’ variable on base64_encode() method for gets encrypted data and result stored on ‘$output’ variable then we printed encrypted data on webpage using echo statement.
- In this concept we can do changes on hash algorithm or encryption method because they had another types also.
In conclusion now we are able to know how to do encryption with salt using php.
When work with php we need to create and process php files at server location and then we need to start the server before execute the program. When we executing this program on browser it will prints result of ‘Encrypt Result: OVEzdnpzV3JCc1ZaN3dwalc0VXlLZz09’.
We can also modify above user defined keys ‘$privateKey , $secretKey’ and string ‘$string’ to any other value of string and try to explore decryption with salt also. Later we will see about that in detail.
I hope this article on PHP encrypt/decrypt with salt helps you and the steps and method mentioned above are easy to follow and implement.