Encrypt and decrypt data using private/public keys
This package allows you to easily generate a private/public key pairs, and encrypt/decrypt messages using those keys.
use Spatie\Crypto\KeyPair;
use Spatie\Crypto\PrivateKey;
use Spatie\Crypto\PublicKey;
// generating a key pair
[$privateKey, $publicKey] = (new KeyPair())->generate();
// when passing paths, the generate keys will be written those paths
(new KeyPair())->generate($pathToPrivateKey, $pathToPublicKey);
$data = 'my secret data';
$privateKey = PrivateKey::fromFile($pathToPrivateKey);
$encryptedData = $privateKey->encrypt($data); // returns something unreadable
$publicKey = PublicKey::fromFile($pathToPublicKey);
$decryptedData = $publicKey->decrypt($encryptedData); // returns 'my secret data'
Most functions in this package are wrappers around open_ssl_*
functions to improve DX.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/crypto
Usage
You can generate a key pair using the generate
function on the KeyPair
class.
[$privateKey, $publicKey] = (new Spatie\Crypto\KeyPair())->generate();
You can write the keys to disk, by passing paths to the generate
function.
// when passing paths, the generate keys will to those paths
(new Spatie\Crypto\KeyPair())->generate($pathToPrivateKey, $pathToPublicKey)
Loading keys
To load a key from a file use the fromFile
static method.
Spatie\Crypto\PrivateKey::fromFile($pathToPrivateKey);
Spatie\Crypto\PublicKey::fromFile($pathToPublicKey);
Alternatively, you can also create a key object using a string.
Spatie\Crypto\PrivateKey::fromString($privateKeyString);
Spatie\Crypto\PublicKey::fromString($publicKeyString);
Encrypting a message with a private key, decrypting with the public key
Here's how you can encrypt data using the private key, and how to decrypt it using the public key.
$data = 'my secret data';
$privateKey = Spatie\Crypto\PrivateKey::fromFile($pathToPrivateKey);
$encryptedData = $privateKey->encrypt($data); // encrypted data contains something unreadable
$publicKey = Spatie\Crypto\PublicKey::fromFile($pathToPublicKey);
$decryptedData = $publicKey->decrypt($encryptedData); // decrypted data contains 'my secret data'
If decrypt
cannot decrypt the given data (maybe a non-matching private key was used to encrypt the data, or maybe tampered with the data), an exception of class Spatie\Crypto\Exceptions\CouldNotDecryptData
will be thrown.
Encrypting a message with a public key, decrypting with the private key
Here's how you can encrypt data using the public key, and how to decrypt it using the private key.
$data = 'my secret data';
$publicKey = Spatie\Crypto\PublicKey::fromFile($pathToPublicKey);
$encryptedData = $publicKey->encrypt($data); // encrypted data contains something unreadable
$privateKey = Spatie\Crypto\PrivateKey::fromFile($pathToPrivateKey);
$decryptedData = $privateKey->decrypt($encryptedData); // decrypted data contains 'my secret data'
If decrypt
cannot decrypt the given data (maybe a non-matching public key was used to encrypt the data, or maybe tampered with the data), an exception of class Spatie\Crypto\Exceptions\CouldNotDecryptData
will be thrown.
Determining if the data can be decrypted
Both the PublicKey
and PrivateKey
class have a canDecrypt
method to determine if given data can be decrypted.
Spatie\Crypto\PrivateKey::fromFile($pathToPrivateKey)->canDecrypt($data) // returns a boolean;
Spatie\Crypto\PublicKey::fromFile($pathToPublicKey)->canDecrypt($data) // returns a boolean;
Sign and verify data
The PrivateKey
class has a method sign
to generate a signature for the given data. The verify
method on the PublicKey
class can be used to verify if a signature is valid for the given data.
$signature = Spatie\Crypto\PrivateKey::fromFile($pathToPrivateKey)->sign('my message') // returns a boolean;
$publicKey = Spatie\Crypto\PublicKey::fromFile($pathToPublicKey);
$publicKey->verify('my message', $signature) // returns true;
$publicKey->verify('my modified message', $signature) // returns false;
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.