PHP Classes

PHP Encryption Tools: Encrypt, decrypt, sign, verify data with OpenSSL

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 115 All time: 9,571 This week: 38Up
Version License PHP version Categories
encryption-tools-php 1.0MIT/X Consortium ...5PHP 5, Files and Folders, Cryptography
Description 

Author

This package can encrypt, decrypt, sign, and verify data with OpenSSL.

It provides several classes that implement asymmetric encryption.

Currently, it provides classes that can:

- Encrypt and decrypt data strings using a secret key

- Encrypt and decrypt data strings using public and private RSA keys

- Encrypt and decrypt large data from files using public and private RSA keys

Innovation Award
PHP Programming Innovation award nominee
June 2023
Number 2
Some applications must encrypt data before storing or transmitting it to other computers.

Existing PHP extensions like OpenSSL support great encryption algorithms.

Usually, these algorithms are used to encrypt data from small text strings.

This package implements classes that can encrypt and decrypt large data by reading the data from files.

This way, these classes can handle encrypting and decrypting data that may take more memory than the limit that PHP is configured to use.

Manuel Lemos
Picture of Smoren  Freelight
  Performance   Level  
Name: Smoren Freelight <contact>
Classes: 38 packages by
Country: Russian Federation Russian Federation
Age: 35
All time rank: 280778 in Russian Federation Russian Federation
Week rank: 10 Up1 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 16x

Documentation

encryption-tools

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: MIT

Tools for encryption/decryption and signing/verifying (wraps openssl lib).

  • Symmetric
  • Asymmetric (RSA-based)

Install to your project

composer require smoren/encryption-tools

Unit testing

composer install
composer test-init
composer test

Usage

Symmetric encryption/decryption

use Smoren\EncryptionTools\Helpers\SymmetricEncryptionHelper;

$data = ["some", "data" => "to", "encrypt"];
$secretKey = uniqid();

$dataEncrypted = SymmetricEncryptionHelper::encrypt($data, $secretKey);
$dataDecrypted = SymmetricEncryptionHelper::decrypt($dataEncrypted, $secretKey);
print_r($dataDecrypted);

$dataEncrypted = SymmetricEncryptionHelper::encrypt($data, $secretKey, 'camellia-256-ofb');
$dataDecrypted = SymmetricEncryptionHelper::decrypt($dataEncrypted, $secretKey, 'camellia-256-ofb');
print_r($dataDecrypted);

Asymmetric encryption/decryption (RSA-based)

use Smoren\EncryptionTools\Helpers\AsymmetricEncryptionHelper;

$data = ["some", "data" => "to", "encrypt"];
[$privateKey, $publicKey] = AsymmetricEncryptionHelper::generateKeyPair();

$dataEncrypted = AsymmetricEncryptionHelper::encryptByPrivateKey($data, $privateKey);
$dataDecrypted = AsymmetricEncryptionHelper::decryptByPublicKey($dataEncrypted, $publicKey);
print_r($dataDecrypted);

$dataEncrypted = AsymmetricEncryptionHelper::encryptByPublicKey($data, $publicKey);
$dataDecrypted = AsymmetricEncryptionHelper::decryptByPrivateKey($dataEncrypted, $privateKey);
print_r($dataDecrypted);

Asymmetric signing/verifying (RSA-based)

use Smoren\EncryptionTools\Helpers\AsymmetricEncryptionHelper;
use Smoren\EncryptionTools\Exceptions\AsymmetricEncryptionException;

$data = ["some", "data" => "to", "encrypt"];
[$privateKey, $publicKey] = AsymmetricEncryptionHelper::generateKeyPair();

$signature = AsymmetricEncryptionHelper::sign($data, $privateKey);

try {
    AsymmetricEncryptionHelper::verify($data, $signature, $publicKey);
} catch(AsymmetricEncryptionException $e) {
    // ... handling exception if cannot verify signature
}

Asymmetric encryption/decryption (RSA-based) for lage data

use Smoren\EncryptionTools\Helpers\AsymmetricLargeDataEncryptionHelper;

$data = file_get_contents('file_with_large_data.txt');
[$privateKey, $publicKey] = AsymmetricLargeDataEncryptionHelper::generateKeyPair();

$dataEncrypted = AsymmetricLargeDataEncryptionHelper::encryptByPrivateKey($data, $privateKey);
$dataDecrypted = AsymmetricLargeDataEncryptionHelper::decryptByPublicKey($dataEncrypted, $publicKey);
print_r($dataDecrypted);

$dataEncrypted = AsymmetricLargeDataEncryptionHelper::encryptByPublicKey($data, $publicKey);
$dataDecrypted = AsymmetricLargeDataEncryptionHelper::decryptByPrivateKey($dataEncrypted, $privateKey);
print_r($dataDecrypted);

  Files folder image Files (18)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (2 directories)
Files folder imagetests (3 files, 2 directories)
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (18)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (18)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file test_master.yml Data Auxiliary data

  Files folder image Files (18)  /  src  
File Role Description
Files folder imageExceptions (4 files)
Files folder imageHelpers (4 files)

  Files folder image Files (18)  /  src  /  Exceptions  
File Role Description
  Plain text file AsymmetricEncryptionException.php Class Class source
  Plain text file EncryptionException.php Class Class source
  Plain text file JsonException.php Class Class source
  Plain text file SymmetricEncryptionException.php Class Class source

  Files folder image Files (18)  /  src  /  Helpers  
File Role Description
  Plain text file AsymmetricEncryptionHelper.php Class Class source
  Plain text file AsymmetricLargeDataEncryptionHelper.php Class Class source
  Plain text file JsonHelper.php Class Class source
  Plain text file SymmetricEncryptionHelper.php Class Class source

  Files folder image Files (18)  /  tests  
File Role Description
Files folder imageunit (1 file)
Files folder image_support (1 file)
  Accessible without login Plain text file coding_standard.xml Data Auxiliary data
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files (18)  /  tests  /  unit  
File Role Description
  Plain text file HelpersTest.php Class Class source

  Files folder image Files (18)  /  tests  /  _support  
File Role Description
  Plain text file UnitTester.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:115
This week:0
All time:9,571
This week:38Up