How to Implement Simple PHP Encryption Methods with Key - PHP Encryption Library package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP Encryption Library PHP Encryption Library   Blog PHP Encryption Library package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement Simp...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Updated on: 2021-02-10

Posted on: 2021-02-10

Viewers: 424 (February 2021)

Package: PHP Encryption Library

Encrypt and decryption are common processes that we need to use in our applications to protect data that needs to be transmitted or stored in a way that only the intended people or programs can access the original data.

Read this article to learn how to implement simple encryption methods that can be used with just a few lines of code to protect data using a secret key.




Introduction

Encryption is the process of translating plain text data stored in PHP string variables into something that appears to be random and meaningless (cipher-text). Decryption is the process of converting cipher-text back to original plain-text.

This article covers how to use the PHP Encryption Library to implement simple PHP encryption and decryption by using OpenSSL or Sodium, you have choice of which adapter you want to use.

This library will bundled in next release of Zest Framework (3.0.0). But this package can be used with any PHP application or frameworks.

Requirements

To use this package here follows the requirements:
  • PHP 7 (7.3 Recommended).
  • Composer to install the package
  • OpenSSL PHP extension
  • Sodium php extension If you want to use the Sodium adapter.

Installation

Installing this package is very simple. First, make sure you have the right PHP version and the  composer installed. Then in your command line shell prompt enter:

composer require lablnet/encryption

Encrypt Data

You can encrypt a string by calling the package class encrypt method.

<?php

  use LablnetEncryption;

  require '../vendor/autoload.php';

  $encryption = new Encryption('your-key');

  // Encrypt the message
  $encrypt = $encryption->encrypt("This is a text");
  echo $encrypt;

Decrypt Data

You can decrypt data by calling package class decrypt method.

<?php

  use LablnetEncryption;

  require '../vendor/autoload.php';

  $encryption = new Encryption('your-key');

  // Decrypt the message
  $decrypt = $encryption->decrypt($encrypt);
  echo $decrypt;

Using Different Encryption Adapters

This package supports two encryption adapters. This means that it can use different PHP extensions to implement. The default adapter uses the PHP OpenSSL.

To change the adapter, you can pass another supported adapter parameter to the class like below:

Use the PHP Sodium extension

<?php

  use LablnetEncryption;

  require '../vendor/autoload.php';

  $encryption = new Encryption('your-key','sodium');

Use the PHP OpenSSL extension

<?php

  use LablnetEncryption;

  require '../vendor/autoload.php';

  $encryption = new Encryption('your-key','openssl');

Conclusion

Using encryption in PHP can be hard using plain PHP code, as it requires that you understand a lot about encryption details that many developers are not away. This package tries to implement a simpler approach that everybody can understand.

If you liked this package, you can download it from the download page or install it using the composer tool following instructions that are also available in the download page.




You need to be a registered user or login to post a comment

1,573,517 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

No comments were submitted yet.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   PHP Encryption Library PHP Encryption Library   Blog PHP Encryption Library package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement Simp...  

For more information send a message to info at phpclasses dot org.