PHP Classes

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

Recommend this page to a friend!
  Info   View files Documentation   View files View files (18)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2023-06-08 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 96 This week: 3All time: 9,774 This week: 9Up
Version License PHP version Categories
encryption-tools-php 1.0MIT/X Consortium ...5PHP 5, Files and Folders, Cryptography
Description 

Author

Smoren


Contributor

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: 26 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 12x

Details

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  
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  /  .github  
File Role Description
Files folder imageworkflows (1 file)

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

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

  Files folder image Files  /  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  /  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  /  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  /  tests  /  unit  
File Role Description
  Plain text file HelpersTest.php Class Class source

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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:96
This week:3
All time:9,774
This week:9Up