smoren / encryption-tools
Tools for encryption/decryption and signing/verifying (wraps openssl lib)
1.0.0
2021-08-11 21:05 UTC
Requires
- php: >=7.2.0
- ext-json: *
- ext-openssl: *
- smoren/extended-exceptions: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-12-25 11:46:42 UTC
README
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 large 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);