The openssl_encrypt() and openssl_decrypt() functions can be used to encrypt and decrypt text in PHP.
They are useful to encrypt and decrypt the user password in a register /login script.

- Here is a simple example with the openssl_encrypt() and openssl_decrypt() functions.
$password = 'user_pass'; //password added by user $_POST['password']
$key_enc = '1234'; //key for encrypt
$met_enc = 'aes256'; //method to encrypt: aes128, aes192, aes256, blowfish, cast-cbc
$iv = '16_characters_ok'; //a random string with 16 characters

//encrypts the password
$pass_enc = openssl_encrypt($password, $met_enc, $key_enc, 0, $iv);
echo $pass_enc; // "uw4QKRUsG+l17w5epb7nKw=="

//decrypts the encrypted password, it uses the same arguments: $met_enc, $key_enc, $opt=0, $iv
$pass_enc ='uw4QKRUsG+l17w5epb7nKw=='; //the encrypted password
$pass = openssl_decrypt($pass_enc, $met_enc, $key_enc, 0, $iv);
echo '<br>'.$pass; // "user_pass"

Notice, the openssl_decrypt() must use the same arguments (here: $met_enc, $key_enc, $opt=0, $iv) to decrypt the string encrypted with openssl_encrypt().

Daily Test with Code Example

Which HTML5 tag can be used to embed an external application (SWF, PDF) in web page?
<mark> <embed> <canvas>
<embed src="flash_game.swf" width="450" height="350" />
Which CSS pseudo-element adds a special style to the first line of a text?
:first-letter :before :first-line
#id:first-line {
  font-weight: bold;
  color: blue;
Click on the window object property which gets or sets the URL of current page.
window.location window.self window.status
var url = window.location;
Indicate the PHP function used to get the contents of a file or page and store it into a string.
fopen() file_put_contents() file_get_contents()
$homepage = file_get_contents("");
echo $homepage;
Using openssl_encrypt and openssl_decrypt in PHP

Last accessed pages

  1. Read Excel file data in PHP - PhpExcelReader (77165)
  2. Countdown Timer with starting time added into a form (8310)
  3. SHA512 Encrypt hash in JavaScript (16291)
  4. JavaScript code and PHP (29324)
  5. PHP MySQL - INSERT INTO (12993)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (317)
  2. Register and show online users and visitors (211)
  3. Node.js Move and Copy file (171)
  4. Read Excel file data in PHP - PhpExcelReader (155)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (154)