This package allows you to easily generate a private/public key pairs, and encrypt/decrypt messages using those keys.
use Justmd5\Crypto\Rsa\KeyPair;
use Justmd5\Crypto\Rsa\PrivateKey;
use Justmd5\Crypto\Rsa\PublicKey;
// generating an RSA key pair
list($privateKey,$publicKey) = (new KeyPair())->generate();
// when passing paths, the generated 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 openssl_*
functions to improve DX.
You can install the package via composer:
composer require justmd5/crypto
You can generate a key pair using the generate
function on the KeyPair
class.
use Justmd5\Crypto\Rsa\KeyPair;
list($privateKey,$publicKey) = (new 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 KeyPair())->generate($pathToPrivateKey, $pathToPublicKey);
You can protect the private key with a password by using the password
method:
$generate = (new KeyPair())->password('my-password')->generate();
list(passwordProtectedPrivateKey,$publicKey) = (new KeyPair())->generate();
When using a password to generating a private key, you will need that password when instantiating the PrivateKey
class.
To load a key from a file use the fromFile
static method.
Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey);
Justmd5\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);
Alternatively, you can also create a key object using a string.
Justmd5\Crypto\Rsa\PrivateKey::fromString($privateKeyString);
Justmd5\Crypto\Rsa\PublicKey::fromString($publicKeyString);
If the private key is password protected, you need to pass the password as the second argument.
Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey, $password);
Justmd5\Crypto\Rsa\PrivateKey::fromString($privateKeyString, $password);
If you do not specify the right password, a Justmd5\Crypto\Exceptions\InvalidPrivateKey
exception will be thrown.
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 = Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey);
$encryptedData = $privateKey->encrypt($data); // encrypted data contains something unreadable
$publicKey = Justmd5\Crypto\Rsa\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 Justmd5\Crypto\Exceptions\CouldNotDecryptData
will be thrown.
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 = Justmd5\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);
$encryptedData = $publicKey->encrypt($data); // encrypted data contains something unreadable
$privateKey = Justmd5\Crypto\Rsa\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 Justmd5\Crypto\Exceptions\CouldNotDecryptData
will be thrown.
Both the PublicKey
and PrivateKey
class have a canDecrypt
method to determine if given data can be decrypted.
Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey)->canDecrypt($data); // returns a boolean;
Justmd5\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey)->canDecrypt($data); // returns a boolean;
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.
If verify
returns true
, you know for certain that the holder of the private key signed the message, and that it was not tampered with.
$signature = Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey)->sign('my message'); // returns a string
$publicKey = Justmd5\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);
$publicKey->verify('my message', $signature) // returns true;
$publicKey->verify('my modified message', $signature) // returns false;
This package aims to be very lightweight and easy to use. If you need more features, consider using of one these alternatives:
At the time of writing, RSA is secure enough for the use case we've built this package for.
To know more about why RSA might not be good enough for you, read this post on public-key encryption at Paragonie.com
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.