PHP7 Sodium Encryption Decryption

Вся информация и фото, взяты с https://www.etutorialspoint.com/index.php/179-php7-sodium-encryption-decryption

In this post, you will learn about the Sodium encryption decryption of the PHP programming language.

PHP7.2 introduces modern cryptography extensions to make encryption, decryption techniques more protected. That provides high security and protects our system from advanced hackers. Cryptography is hard to design and implement. The Sodium Crypto Library is one of them. It is a fork of NaCL (Network and Cryptography Library).

Sodium Crypto Library is a modern, installable, easy-to-use, cross-compatible library that is generally used for network and data communication, high speed encryption decryption, and digital signatures.

https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9418213259189791&output=html&h=175&slotname=6486222076&adk=2437954989&adf=2194502042&pi=t.ma~as.6486222076&w=700&fwrn=4&lmt=1681149907&rafmt=11&format=700×175&url=https%3A%2F%2Fwww.etutorialspoint.com%2Findex.php%2F179-php7-sodium-encryption-decryption&wgl=1&uach=WyJXaW5kb3dzIiwiOC4wLjAiLCJ4ODYiLCIiLCIxMTEuMC41NTYzLjE0OCIsW10sZmFsc2UsbnVsbCwiNjQiLFtbIkdvb2dsZSBDaHJvbWUiLCIxMTEuMC41NTYzLjE0OCJdLFsiTm90KEE6QnJhbmQiLCI4LjAuMC4wIl0sWyJDaHJvbWl1bSIsIjExMS4wLjU1NjMuMTQ4Il1dLGZhbHNlXQ..&dt=1681149901655&bpp=5&bdt=764&idt=1797&shv=r20230405&mjsv=m202304050101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D0abdcbcd472d36fc-226c9e5a8cdd00e7%3AT%3D1680974452%3ART%3D1681149873%3AS%3DALNI_MaKzOT0I1tswBq7swxSjfBjELWdKg&gpic=UID%3D00000bd35ff9b071%3AT%3D1680974452%3ART%3D1681149874%3AS%3DALNI_MYSwG3_GIeMeceToRyZJlXr5z0tTw&prev_fmts=0x0%2C1200x200&nras=1&correlator=5463313654462&frm=20&pv=1&ga_vid=485842470.1680974446&ga_sid=1681149903&ga_hid=102446232&ga_fc=1&rplot=4&u_tz=180&u_his=1&u_h=768&u_w=1366&u_ah=728&u_aw=1366&u_cd=24&u_sd=1&dmc=4&adx=171&ady=973&biw=1349&bih=625&scr_x=0&scr_y=624&eid=44759876%2C44759927%2C44759842%2C31073585%2C31073709%2C44785295&oid=2&pvsid=4260762491301990&tmod=1819441088&uas=0&nvt=1&ref=https%3A%2F%2Fyandex.ru%2F&fc=1920&brdim=0%2C0%2C0%2C0%2C1366%2C0%2C1366%2C728%2C1366%2C625&vis=1&rsz=%7C%7CpEe%7C&abl=CS&pfx=0&fu=128&bc=31&ifi=3&uci=a!3&fsb=1&xpc=OVxw7M6HiB&p=https%3A//www.etutorialspoint.com&dtd=1855

Sodium Features

These are the following features of the Sodium Crypto Library

  • Public and shared key signatures
  • Hashing
  • Keyed hashing
  • Authenticated public key encryption
  • Authenticated shared key encryption
  • Secured pseudorandom number generation

https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9418213259189791&output=html&h=175&slotname=6486222076&adk=2437954989&adf=2354055739&pi=t.ma~as.6486222076&w=700&fwrn=4&lmt=1681149907&rafmt=11&format=700×175&url=https%3A%2F%2Fwww.etutorialspoint.com%2Findex.php%2F179-php7-sodium-encryption-decryption&wgl=1&uach=WyJXaW5kb3dzIiwiOC4wLjAiLCJ4ODYiLCIiLCIxMTEuMC41NTYzLjE0OCIsW10sZmFsc2UsbnVsbCwiNjQiLFtbIkdvb2dsZSBDaHJvbWUiLCIxMTEuMC41NTYzLjE0OCJdLFsiTm90KEE6QnJhbmQiLCI4LjAuMC4wIl0sWyJDaHJvbWl1bSIsIjExMS4wLjU1NjMuMTQ4Il1dLGZhbHNlXQ..&dt=1681149901660&bpp=4&bdt=768&idt=1959&shv=r20230405&mjsv=m202304050101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D0abdcbcd472d36fc-226c9e5a8cdd00e7%3AT%3D1680974452%3ART%3D1681149873%3AS%3DALNI_MaKzOT0I1tswBq7swxSjfBjELWdKg&gpic=UID%3D00000bd35ff9b071%3AT%3D1680974452%3ART%3D1681149874%3AS%3DALNI_MYSwG3_GIeMeceToRyZJlXr5z0tTw&prev_fmts=0x0%2C1200x200%2C700x175&nras=1&correlator=5463313654462&frm=20&pv=1&ga_vid=485842470.1680974446&ga_sid=1681149903&ga_hid=102446232&ga_fc=1&rplot=4&u_tz=180&u_his=1&u_h=768&u_w=1366&u_ah=728&u_aw=1366&u_cd=24&u_sd=1&dmc=4&adx=171&ady=1539&biw=1349&bih=625&scr_x=0&scr_y=624&eid=44759876%2C44759927%2C44759842%2C31073585%2C31073709%2C44785295&oid=2&pvsid=4260762491301990&tmod=1819441088&uas=0&nvt=1&ref=https%3A%2F%2Fyandex.ru%2F&fc=1920&brdim=0%2C0%2C0%2C0%2C1366%2C0%2C1366%2C728%2C1366%2C625&vis=1&rsz=%7C%7CpEebr%7C&abl=CS&pfx=0&fu=128&bc=31&ifi=4&uci=a!4&btvi=1&fsb=1&xpc=YYSXQrO3r7&p=https%3A//www.etutorialspoint.com&dtd=1983

Install PHP Libsodium in Wampserver

To use Sodium crypto, we must first install Libsodium using PECL. These are the steps-

Libsodium Version

To make sure that libsodium is enabled on the server or not, let’s check its version using the following code —

<?php
var_dump([
    SODIUM_LIBRARY_MAJOR_VERSION,
    SODIUM_LIBRARY_MINOR_VERSION,
    SODIUM_LIBRARY_VERSION
]);
?>

It returns this output —

array (size=3)
  0 => int 10
  1 => int 2
  2 => string '1.0.17' (length=6)

https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9418213259189791&output=html&h=175&slotname=6486222076&adk=2437954989&adf=1322987391&pi=t.ma~as.6486222076&w=700&fwrn=4&lmt=1681149907&rafmt=11&format=700×175&url=https%3A%2F%2Fwww.etutorialspoint.com%2Findex.php%2F179-php7-sodium-encryption-decryption&wgl=1&uach=WyJXaW5kb3dzIiwiOC4wLjAiLCJ4ODYiLCIiLCIxMTEuMC41NTYzLjE0OCIsW10sZmFsc2UsbnVsbCwiNjQiLFtbIkdvb2dsZSBDaHJvbWUiLCIxMTEuMC41NTYzLjE0OCJdLFsiTm90KEE6QnJhbmQiLCI4LjAuMC4wIl0sWyJDaHJvbWl1bSIsIjExMS4wLjU1NjMuMTQ4Il1dLGZhbHNlXQ..&dt=1681149901664&bpp=7&bdt=773&idt=2091&shv=r20230405&mjsv=m202304050101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D0abdcbcd472d36fc-226c9e5a8cdd00e7%3AT%3D1680974452%3ART%3D1681149873%3AS%3DALNI_MaKzOT0I1tswBq7swxSjfBjELWdKg&gpic=UID%3D00000bd35ff9b071%3AT%3D1680974452%3ART%3D1681149874%3AS%3DALNI_MYSwG3_GIeMeceToRyZJlXr5z0tTw&prev_fmts=0x0%2C1200x200%2C700x175%2C700x175&nras=1&correlator=5463313654462&frm=20&pv=1&ga_vid=485842470.1680974446&ga_sid=1681149903&ga_hid=102446232&ga_fc=1&rplot=4&u_tz=180&u_his=1&u_h=768&u_w=1366&u_ah=728&u_aw=1366&u_cd=24&u_sd=1&dmc=4&adx=171&ady=2667&biw=1349&bih=625&scr_x=0&scr_y=624&eid=44759876%2C44759927%2C44759842%2C31073585%2C31073709%2C44785295&oid=2&pvsid=4260762491301990&tmod=1819441088&uas=0&nvt=1&ref=https%3A%2F%2Fyandex.ru%2F&fc=1920&brdim=0%2C0%2C0%2C0%2C1366%2C0%2C1366%2C728%2C1366%2C625&vis=1&rsz=%7C%7CpEebr%7C&abl=CS&pfx=0&fu=128&bc=31&ifi=5&uci=a!5&btvi=2&fsb=1&xpc=MJCUviPl44&p=https%3A//www.etutorialspoint.com&dtd=2151

Algorithms in Sodium

These are some algorithms that we can use with Sodium.

  • Salsa20, ChaCha20
  • Poly1305
  • Argon2, Scrypt
  • AES-GCM

Sodium Encryption and Decryption Example

In this example, we have used random_bytes() to generate cryptographically secure pseudo-random bytes and have generated both secure key using sodium SODIUM_CRYPTO_SECRETBOX_KEYBYTES and a nonce using Sodium SODIUM_CRYPTO_SECRETBOX_NONCEBYTES. After that, we encrypted the text message using the sodium_crypto_secretbox() method and decrypted the text using the sodium_crypto_secretbox_open() method.

<?php
$text = 'Encrypt Decrypt this text using Sodium';

// Create an encryption key and a nonce
$secret_key   = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES); 
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);

// Encrypting Text
$encryptedtext = sodium_crypto_secretbox($text, $nonce, $secret_key);
// Decrypting Text
$plaintext = sodium_crypto_secretbox_open($encryptedtext , $nonce, $secret_key);

echo $plaintext === $text ? 'Encryption Success' : 'Error';
?>

https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9418213259189791&output=html&h=175&slotname=6486222076&adk=2437954989&adf=3690903962&pi=t.ma~as.6486222076&w=700&fwrn=4&lmt=1681149907&rafmt=11&format=700×175&url=https%3A%2F%2Fwww.etutorialspoint.com%2Findex.php%2F179-php7-sodium-encryption-decryption&wgl=1&adsid=ChEI8OfOoQYQhuiUuae0057QARI9ADPB8h4FfxtsVmtlG5ChW_ZW36INo6nNOQWHBnLbVlfJPf-dBkhcdjt5PWjbXxkSPTPj-3Q51KybnhK_bw&uach=WyJXaW5kb3dzIiwiOC4wLjAiLCJ4ODYiLCIiLCIxMTEuMC41NTYzLjE0OCIsW10sZmFsc2UsbnVsbCwiNjQiLFtbIkdvb2dsZSBDaHJvbWUiLCIxMTEuMC41NTYzLjE0OCJdLFsiTm90KEE6QnJhbmQiLCI4LjAuMC4wIl0sWyJDaHJvbWl1bSIsIjExMS4wLjU1NjMuMTQ4Il1dLGZhbHNlXQ..&dt=1681149901671&bpp=8&bdt=779&idt=2250&shv=r20230405&mjsv=m202304050101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D0abdcbcd472d36fc-226c9e5a8cdd00e7%3AT%3D1680974452%3AS%3DALNI_MaKzOT0I1tswBq7swxSjfBjELWdKg&gpic=UID%3D00000bd35ff9b071%3AT%3D1680974452%3ART%3D1681149874%3AS%3DALNI_MYSwG3_GIeMeceToRyZJlXr5z0tTw&prev_fmts=0x0%2C1200x200%2C700x175%2C700x175%2C700x175%2C288x600%2C288x600%2C288x600&nras=1&correlator=5463313654462&frm=20&pv=1&ga_vid=485842470.1680974446&ga_sid=1681149903&ga_hid=102446232&ga_fc=1&rplot=4&u_tz=180&u_his=1&u_h=768&u_w=1366&u_ah=728&u_aw=1366&u_cd=24&u_sd=1&dmc=4&adx=171&ady=3384&biw=1349&bih=625&scr_x=0&scr_y=1224&eid=44759876%2C44759927%2C44759842%2C31073585%2C31073709%2C44785295&oid=2&pvsid=4260762491301990&tmod=1819441088&uas=0&nvt=1&ref=https%3A%2F%2Fyandex.ru%2F&fc=1920&brdim=0%2C0%2C0%2C0%2C1366%2C0%2C1366%2C728%2C1366%2C625&vis=1&rsz=%7C%7CpEebr%7C&abl=CS&pfx=0&fu=128&bc=31&jar=2023-04-10-13&ifi=6&uci=a!6&btvi=5&fsb=1&xpc=kfdDsn0jk0&p=https%3A//www.etutorialspoint.com&dtd=2593

Related Articles

PHP User Authentication by IP Address
How to encrypt password in PHP
Different datatype comparison in PHP
PHP loop through an associative array
PHP CURL Cookie Jar
PHP remove last character from string
PHP calculate percentage of total
Insert image in database using PHP
PHP set a cookie to store login detail
Preventing Cross Site Request Forgeries(CSRF) in PHP
Recover forgot password using PHP and MySQL
How to add google reCAPTCHA v2 in registration form using PHP
Complete HTML Form Validation in PHP
Submit a form data without page refresh using PHP, Ajax and Javascript
PHP Server Side Form Validation
How to send emojis in email subject and body using PHP
PHP7.3 New Features, Functions and Deprecated Functions