Javatpoint Logo
Javatpoint Logo

How to Encrypt or Decrypt a String in PHP

In the PHP programming language, Encryption, as well as Decryption of string data, is accomplished using the OpenSSL function. Here, we can encrypt and decrypt value through openssl_encrypt(data) and openssl_decrypt(data) respectively. We can use a single method or both methods of PHP language.

1. openssl_encrypt(data) Method

The encrypt function uses to encode the input value with many functionalities. We can get output values in difficult and safe coded formats.

Syntax:

The following syntax is used for the encrypt string data using php language.

Parameters:

Now, we can discuss syntax parameters for better understanding.

  • $data_input:It contains the input string or given data that need to be encrypted in the program.
  • $method_input:The cipher method contains inside of the encrypt function using the "openssl_get_cipher_methods()" function.
  • $key_input:It contains the encryption key for functionality.
  • $options:It contains the bitwise disjunction of the flag. The flag value represents the "OPENSSL_RAW_DATA" and "OPENSSL_ZERO_PADDING" data.
  • $iv_input:It contains the initialization vector data. This data should not be NULL.
  • $tag:It contains the authentication tag. It is used for passing the reference when using AEAD cipher mode. The cipher mode such as a GCM or CCM.
  • $adv_input:It contains the additional authentication input value for encrypt function.
  • $tag_length:It includes the length of the authentication input tag. The length of the authentication tag used from the 4 to 16 values for GCM mode.

Return Value:

  • The output value returns the encrypted string value on success.
  • It shows a FALSE output value on failure.

2. openssl_decrypt(data) method

The decrypt function uses to decode the input value with much functionality. We can get the output value in the original format from the coded format.

Syntax

The following syntax uses for the decrypt string data using php language.

parameters:

Now, we can discuss syntax parameters for better understanding.

  • $data_input:It contains the input string or given data that need to be decrypted in the program.
  • $method_input:The cipher method contains inside of the decrypt function using the "openssl_get_cipher_methods()" function.
  • $key_input:It contains the decryption key for functionality.
  • $options:It contains the bitwise disjunction of the flag. The flag value represents the "OPENSSL_RAW_DATA" and "OPENSSL_ZERO_PADDING" data.
  • $iv_input:It contains the initialization vector data. This data should not be NULL.
  • $tag:It contains the authentication tag. It is used for passing the reference when using AEAD cipher mode. The cipher mode such as a GCM or CCM.
  • $adv_input:It contains the additional authentication input value for the decrypt function.

Return Value:

  • The output value returns the decrypted string value on success.
  • It shows a FALSE output value on failure.

Examples

The following examples show we encrypt and decrypt a string data using a php language.

Example1

The following example shows encrypting a string of data using a php language.

We require the input string, cipher method, and encryption key value to encrypt a string value.

Output

The following output shows encrypting a string of data using a php language.

How to Encrypt or Decrypt a String in PHP

Example2

The following example shows the basic decrypting of string data using a php language. We require the input string, cipher method, and encryption key value to encrypt a string value. The encrypted value works as an input for decrypting data.

Output

The following output shows encrypting a string of data using a php language.

Original String: Welcome to JavaTpoint learners

Encrypted Input String: 47Qv+Tl92fM9MQyevgN3tyMA92KyNamCfx1ptqJ6R8Y=

Decrypted Input String: Welcome to JavaTpoint learners

Example3

The following example shows the basic decrypting of string data using a php language. We can use the multiple string functions in encrypt and decrypt functions.

Output

The following output shows encrypting a string of data using a php language.

How to Encrypt or Decrypt a String in PHP

Example4

The following example shows the basic decrypting of string data using a php language. We can use the multiple dynamic string functions in encrypt and decrypt functions.

Output

The following output shows encrypting a string of data using a php language.

How to Encrypt or Decrypt a String in PHP

Conclusion

The encrypt and decrypt string is an essential and safe function for the development process. It keeps and retrieves data safely without licking important values. The php functions create and display data without licking and destroying the original value.


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA