This tutorial help to understand encrypt and decrypt a string in php7.3. I have also shared How To Encrypt and Decrypt strings 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 an open-source library and converts 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.
Let’s Integrate Sodium PHP encryption library with php 7 application, We will encrypt a string using salt/key and decrypt the string with the same salt/key.
You can install the encryption/decryption library by using composer. Please use the following command to install into the application:
composer require internetpixels/sodium-encryption
We need to pass the secret key and public key into Sodium libs. This is one-time setup keys and is not allowed to change over time.
<?php // Update the keys (create a new unique keypair)! \InternetPixels\SodiumEncryption\EncryptionManager::setKeys( 'secret key..', 'public key..' ); ?>
The nounce
is help to encrypt and decrypt data, You can create unique nonce per entity wise. The EncryptionManager::generateNonce()
method helps to create nonce.
The PHP Sodium Encryption library provides encrypt($param1, param2)
method to encrypt data. This method takes the first params as a source string and the second is nounce.
<?php $string = 'Hi, This is PHPFlow.'; $nonce = EncryptionManager::generateNonce(); $encrypted = EncryptionManager::encrypt($string, $nonce); ?>
The PHP Sodium Encryption library provides decrypt($param1, param2)
method to decrypt data. The first parameter is an encrypted string and the second one is nonce.
<?php $string = EncryptionManager::decrypt($encrypted, $nonce); ?>
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
This code is giving error .
what type error?
error The keys must be 64 chars (a-z, 0-9) long!
Please define key without any special chars