Encrypt and Decrypt a String in PHP

Encryption and decryption of data is a technique used for security purposes. This technique widely used to keep confidential messages secret and only readable by the receiver of the message.

In PHP, it is possible to encrypt and decrypt data. Well, PHP already has in-built functions to do this task. So let’s see how we can do it.

openssl_encrypt() and openssl_decrypt() PHP function:

The openssl_encrypt() PHP function can encrypt a data with a encryption key.

On the other hand, the openssl_decrypt() function can decrypt the encrypted data using a decrypted key.

Here in this article, I am going to show you how to encrypt and decrypt a string in PHP with examples.

Syntax for openssl_encrypt()

Below is the syntax for openssl_encrypt function

openssl_encrypt( string $data, string $method, string $key, $options = 0, string $iv, string $tag= NULL,
string $aad, int $tag_length = 16 )

Below is the description of each parameter of the function:

$data: This is the string or the data which to be encrypted.

$method: The cipher method. We have to use the openssl_get_cipher_methods() function to generate the cipher method to get the result and pass it to the openssl_encrypt() function.

$key: The encryption key to be used.

$options: Bitwise disjunction of the flags that are OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING.

$iv: The initialization vector that is not NULL.

$tag: Authentication tag passed by reference when using AEAD cipher mode that may be GCM or CCM.

$aad: It is the additional authentication data.

$tag_length: It holds the length of the authentication tag. For GCM mode, the length of the authentication tag can be between 4 to 6.

Syntax for openssl_decrypt()

Below is the syntax of the openssl_decrypt() PHP function:

openssl_decrypt( string $data, string $method, string $key, int $options = 0, string $iv, string $tag, string $aad)

The description of each parameters of openssl_decrypt() function is given below:

$data: The string or the data to be encrypted.
$method: The cipher method generated by the openssl_get_cipher_methods() PHP function.
$key: The encryption key. This is needed to decrypt the encrypted string..
$options: IThe bitwise disjunction of the flags for OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING.
$iv: The initialization vector which is non-NULL.
$tag: Holds the authentication tag using AEAD cipher mode that may be GCM or CCM. In case of authentication failed, openssl_decrypt() returns FALSE.
$aad: The additional authentication data.

Also, read:

Examples of PHP code to Encrypt and Decrypt a String

Example 1: This example will encrypt and decrypt a string keeping the encrypted string the same. Below is the PHP code is given:

<?php 
// Our original string store in the variable that need to be encrypted
$str = "Welcome to CodeSpeedy"; 
// Store the cipher method in variable
$cipher = "AES-128-CTR"; 
// Get the cipher iv length
$iv_length = openssl_cipher_iv_length($cipher); 
$options = 0;  
$iv = '8565825542115032'; 
// Take the encryption key in a variable
$enc_key = "CodeSpeedyKeybj54HH"; 
// Encrypt the data using openssl_encrypt function 
$encrypted_string = openssl_encrypt($str, $cipher, $enc_key, $options, $iv); 
$decryption_iv = '8565825542115032'; 
// Store the decryption key 
$dec_key = "CodeSpeedyKeybj54HH"; 
// Use openssl_decrypt() function to decrypt the data 
$decrypted_string=openssl_decrypt ($encrypted_string, $cipher, $dec_key, $options, $decryption_iv); 
// Display the decrypted string 
echo "Decrypted String: " . $decrypted_string; 
?>

 

Example 2: This example will randomly change the encrypted string. Every time you run the code, it will change the encrypted string accordingly. Although the string to be encrypted and the decrypted string will be the same.

Below is the code of this example:

<?php 
$str = "Welcome to CodeSpeedy"; 
$cipher = "BF-CBC"; 
$iv_length = openssl_cipher_iv_length($cipher); 
$options = 0; 
$iv = random_bytes($iv_length); 
$encryption_key = openssl_digest(php_uname(), 'MD5', TRUE); 
$encrypted_string = openssl_encrypt($str, $cipher, 
    $encryption_key, $options, $iv); 
$decryption_iv = random_bytes($iv_length); 
$decryption_key = openssl_digest(php_uname(), 'MD5', TRUE); 
$decrypted_string = openssl_decrypt ($encrypted_string, $cipher, 
      $decryption_key, $options, $iv); 
echo "Decrypted String: " . $decrypted_string; 
?> 

 

So I hope, you have understood and learned how we encrypt our data and or string and then decrypt it in PHP to get our data back.

 

Leave a Reply

Your email address will not be published. Required fields are marked *