How to Encrypt And Decrypt String in PHP7

This tutorial help to understand encrypt and decrypt string in php7.3. I have also shared How To Encrypt and Decrypt string in PHP 5.As you aware sodium is replaced mcrypt method (which is officially being deprecated) in PHP7.3.

The mycrpt function is used to encrypt and dycrypt string in php 5, but this function is deprecated in php 7.3.

We will use Sodium PHP encryption library.This is a open-source library and convert string data into encrypted text and vise versa.The Sodium PHP encryption library help to protect your data and secure every field in your entities with this tiny encryption library.

Two Way Encryption In PHP7

Let’s Integrate Sodium PHP encryption library with php 7 application, We will encrypt a string using salt/key and decrypt string by same salt/key.

How To Install Sodium PHP encryption library

You can install encryption/decryption library by using composer. Please use the following command to install into application:

composer require internetpixels/sodium-encryption

How To Configure Sodium Encryption

We need to pass secret key and public key into Sodium libs. This is one time setup keys and are not allowed to change overtime.

How To Create nonce in Sodium Encryption

The nounce is help to encrypt and decrypt data, You can create unique nonce per entity wise.The EncryptionManager::generateNonce() method help to create nonce.

How To Encrypt String in PHP

The PHP Sodium Encryption library is provide encrypt($param1, param2) method to encrypt data.This method takes the first params as a source string and second is nonce.

How To Decrypt String in PHP

The PHP Sodium Encryption library is provide decrypt($param1, param2) method to decrypt data.The first parameters is encrypted string and second one is nonce.

3 thoughts on “How to Encrypt And Decrypt String in PHP7

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.