Encryption and Decryption in php

Encryption is the process of translating plain text data (plaintext) into something that appears to be random and meaningless (ciphertext). Decryption is the process of converting ciphertext back to plaintext.

To encrypt more than a small amount of data, symmetric encryption is used. A symmetric key is used during both the encryption and decryption processes. To decrypt a particular piece of ciphertext, the key that was used to encrypt the data must be used.

In this post we will show you how to encrypt and decrypt any string in php. We will use two function one for encryption and second for decryption. There will be need to set a key for both functionality. The code which needs to be decrypted should must be encrypted by the same key used in encryption.

Find the code below for both Encryption and Decryption.

Code for Encryption

Code for Decryption

That’s it, Send the fresh keyword for encryption as key value in encrypt function which will provide the ciphertext value then when you need to decrypt the same encryted string Pass the encrypted string to decrypt function to get the original string.