openssl_encrypt() function:openssl_encrypt() function is used to encrypt data.
Syntax:string openssl_encrypt (string $data, string $method, string $key, $options = 0, string $iv, string $tag = NULL, string $aad, int $tag_length = 16)
Parameters: - $data:contains the string or data to be encrypted.
- $method : the encryption methodis accepted using the openssl_get_cipher_methods() function.
- $key:contains the encryption key.
- $options: contains a bitwise disjunction of the OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING flags.
- $iv:contains an initialization vector that is not NULL.
- $tag:contains an authentication tag that is passed by the link when using re AEAD encryption press (GCM or CCM).
- $aad:contains additional authentication data.
- $tag_length:contains length authentication tag. Authentication tag length ranges from 4 to 16 for GCM mode.
Return value:Returns an encrypted string on success, or FALSE on error.
openssl_decrypt() Function Theopenssl_decrypt() function is used to decrypt data.
Syntax:string openssl_decrypt (string $data, string $method, string $key, int $options = 0 , string $iv, string $tag, string $aad)
Parameters: - $data:contains a string or data that needs to be encrypted.
- $method:encryption method is accepted from using the openssl_get_cipher_methods() function.
- $key:contains the encryption key.
- $options:contains a bitwise disjunction of the OPENSSL_RAW_DATA flags and OPENSSL_ZERO_PADDING.
- $iv: contains an initialization vector that is not NULL.
- $tag:contains an authentication tag in AEAD encryption mode (GCM or CCM). If authentication fails, openssl_decrypt() returns FALSE.
- $aad:contains additional authentication credentials.
Return value: Returns the decrypted string on success or FALSE on error.
Approach:first declare the string and store it in a variable, use the openssl_encrypt() function to encrypt the given string and use openssl_decrypt() to decrypt the specified string.
Example 1:This example illustrates encrypting and decrypting a string.
// Save the string to a variable that
// need to be encrypted
$simple_string
=
"Welcome to GeeksforGeeks"
;
// Show original string
echo
"Original String:"
.
$simple_string
;
// Save the encryption method
$ciphering
=
"AES-128-CTR"
;
// Using the OpenSSl encryption method
$iv_length
= openssl_cipher_iv_length (
$ciphering
);
$options
= 0;
// Nonzero initialization vector for encryption
$encryption_iv
=
’1234567891011121’
;
// Save the encryption key
$encryption_key
=
"GeeksforGeeks"
;
// Use the openssl_encrypt() function to encrypt data
$encryption
= openssl_encrypt (
$simple_string
,
$ciphering
,
$encryption_key
,
$options
,
$encryption_iv
);
// Show encrypted string
echo
"Encrypted String:"
.
$encryption
.
""
;
// Nonzero initialization vector for decryption
$decryption_iv
=
’1234567891011121’
;
// Save the decryption key
$decryption_key
=
"GeeksforGeeks"
;
// Use the openssl_decrypt() function to decrypt the data
$decryption
= openssl_decrypt (
$encryption
,
$ciphering
,
$decryption_key
,
$options
,
$decryption_iv
);
// Show the decrypted string
echo
"Decrypted String:"
.
$decryption
;
?>
Output:Original String: Welcome to GeeksforGeeks Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA == Decrypted String: Welcome to GeeksforGeeks
Example 2:Below is an example of encryption and decryption. Here, the encrypted string and the decrypted string will be the same, but the encrypted string is changed at random accordingly.
// Save the string to a variable that
// need to encrypt
$simple_string
=
"Welcome to GeeksforGeeks"
;
// Show original string
echo
"Original String:"
.
$simple_string
.
""
;
// Save the encryption method
$ciphering
=
"BF-CBC"
;
// Using the OpenSSl encryption method
$iv_length
= openssl_cipher_iv_length (
$ciphering
);
$options
= 0;
// Use the random_bytes() function, which gives
// random 16-digit values
$encryption_iv
= random_bytes (
$iv_length
);
// Alternatively, we can use any 16 digits
// characters or numbers for iv
$encryption_key
= openssl_digest (php_uname(),
’ MD5’
, TRUE);
// encryption of the string process begins
$encryption
= openssl_encrypt (
$simple_string
,
$ciphering
,
$encryption_key
,
$options
,
$encryption_iv
);
// Show encrypted string
echo
"Encrypted String:"
.
$encryption
.
""
;
// Deciphering the string process begins
// Used by random_bytes(), which produces randomly
// 16-digit values
$decryption_iv
= random_bytes (
$iv_length
);
// Save the decryption key
$decryption_key
= openssl_digest (php_uname(),
’MD5’
, TRUE);
// Decode the string
$decryption
= openssl_decrypt (
$encryption
,
$ciphering
,
$decryption_key
,
$options
,
$encryption_iv
);
// Show the decrypted string
echo
"Decrypted String:"
.
$decryption
;
?>
Output:Original String: Welcome to GeeksforGeeks Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA == Decrypted String: Welcome to GeeksforGeeks
References: href = https://www.php.net/manual/en/function.openssl-encrypt.php> https://www.php.net/manual/en/function.openssl-encrypt.php - https://www.php.net/manual/en/function.openssl- decrypt.php