Change language

How do I encrypt and decrypt a PHP string?

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

Shop

Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$
Gifts for programmers

Best laptop for Zoom

$499
Gifts for programmers

Best laptop for Minecraft

$590

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically