PHPackages                             justmd5/crypto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. justmd5/crypto

ActiveLibrary[Security](/categories/security)

justmd5/crypto
==============

Encrypting and signing data using private/public keys

v1.0.0(5y ago)26.2kMITPHPPHP ^7.0CI failing

Since Dec 31Pushed 4y ago1 watchersCompare

[ Source](https://github.com/justmd5/crypto)[ Packagist](https://packagist.org/packages/justmd5/crypto)[ Docs](https://github.com/spatie/crypto)[ GitHub Sponsors](https://github.com/justmd5/crypto)[ RSS](/packages/justmd5-crypto/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

Encrypting and signing data using private/public keys For All version of php7 only
==================================================================================

[](#encrypting-and-signing-data-using-privatepublic-keys-for-all-version-of-php7-only)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e188aa244966cdd2e63b9b901e2fa57626dfae4b4ff209730c5546fd1e4c0b47/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f4a7573746d64352f63727970746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/Justmd5/crypto)[![Tests](https://github.com/Justmd5/crypto/workflows/Tests/badge.svg)](https://github.com/Justmd5/crypto/workflows/Tests/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/f1fbf9f6d10276af870a2e82301c330e0d5af5fc484425c8a160a787a1805da0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f4a7573746d64352f63727970746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/Justmd5/crypto)

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.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require justmd5/crypto
```

Usage
-----

[](#usage)

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.

### Loading keys

[](#loading-keys)

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.

### Encrypting a message with a private key, decrypting with the public key

[](#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 = 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.

### Encrypting a message with a public key, decrypting with the private key

[](#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 = 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.

### Determining if the data can be decrypted

[](#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.

```
Justmd5\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey)->canDecrypt($data); // returns a boolean;
Justmd5\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey)->canDecrypt($data); // returns a boolean;
```

### Signing and verifying data

[](#signing-and-verifying-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.

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;
```

Alternatives
------------

[](#alternatives)

This package aims to be very lightweight and easy to use. If you need more features, consider using of one these alternatives:

- [paragonie/halite](https://github.com/paragonie/halite)
- [vlucas/pikirasa](https://github.com/vlucas/pikirasa)
- [laminas/crypt](https://docs.laminas.dev/laminas-crypt/)
- [phpseclib/phpseclib](https://github.com/phpseclib/phpseclib)

A word on the usage of RSA
--------------------------

[](#a-word-on-the-usage-of-rsa)

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](https://paragonie.com/blog/2016/12/everything-you-know-about-public-key-encryption-in-php-is-wrong#php-openssl-rsa-bad-default)

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Thanks To
---------

[](#thanks-to)

[spatie/crypto](https://github.com/spatie/crypto)

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2010d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f5b85bbec67d8b4154f54a8e0fc5bdc005124d90b331ddfa23d7c12dfcc83984?d=identicon)[justmd5](/maintainers/justmd5)

---

Top Contributors

[![justmd5](https://avatars.githubusercontent.com/u/4120960?v=4)](https://github.com/justmd5 "justmd5 (2 commits)")

---

Tags

cryptoencrypt-datakey-pairsrsarsa-encryptionrsa-key-pairrsa-signaturesignaturesigningcryptoopenssl

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/justmd5-crypto/health.svg)

```
[![Health](https://phpackages.com/badges/justmd5-crypto/health.svg)](https://phpackages.com/packages/justmd5-crypto)
```

###  Alternatives

[defuse/php-encryption

Secure PHP Encryption Library

3.9k175.2M254](/packages/defuse-php-encryption)[phpseclib/phpseclib

PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.

5.6k465.6M1.5k](/packages/phpseclib-phpseclib)[spatie/crypto

Encrypting and signing data using private/public keys

487825.3k14](/packages/spatie-crypto)[mmeyer2k/dcrypt

A petite library of encryption functionality for PHP

98794.6k1](/packages/mmeyer2k-dcrypt)[acmephp/core

Raw implementation of the ACME protocol in PHP

411.0M8](/packages/acmephp-core)[acmephp/ssl

PHP wrapper around OpenSSL extension providing SSL encoding, decoding, parsing and signing features

151.4M4](/packages/acmephp-ssl)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
