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: 116 All time: 9,561 This week: 54Up
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  
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

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:116
This week:0
All time:9,561
This week:54Up