PHPackages                             eplayment/laravel-crypto-pkg - 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. eplayment/laravel-crypto-pkg

ActiveLibrary[Security](/categories/security)

eplayment/laravel-crypto-pkg
============================

Encrypting and signing data using private/public keys

2.0.1(4y ago)05MITPHPPHP ^7.4|^8.0

Since Nov 6Pushed 4y agoCompare

[ Source](https://github.com/eplayment/laravel-crypto-pkg)[ Packagist](https://packagist.org/packages/eplayment/laravel-crypto-pkg)[ Docs](https://github.com/spatie/crypto)[ GitHub Sponsors](https://github.com/sponsors/spatie)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/eplayment-laravel-crypto-pkg/feed)WikiDiscussions master Synced 1mo ago

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

Encrypting and signing data using private/public keys
=====================================================

[](#encrypting-and-signing-data-using-privatepublic-keys)

[![Latest Version on Packagist](https://camo.githubusercontent.com/26c04c3b8243a10d225f61eb542c6a8ac8285262a6d7cc4d7024f0d10b5806c6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f63727970746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/crypto)[![Tests](https://github.com/spatie/crypto/workflows/Tests/badge.svg)](https://github.com/spatie/crypto/workflows/Tests/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/f2a749910deb8db8673e498ba289ceff055df0f4ce7083e34a8e582fa36ba699/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f63727970746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/crypto)

This package allows you to easily generate a private/public key pairs, and encrypt/decrypt messages using those keys.

```
use Spatie\Crypto\Rsa\KeyPair;
use Spatie\Crypto\Rsa\PrivateKey;
use Spatie\Crypto\Rsa\PublicKey;

// generating an RSA key pair
[$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.

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/715bddcb846771a5a1eec255c83f0fcf35279b9f6c2a09a291f377b1b0894a6c/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f7061636b6167652d736b656c65746f6e2d7068702e6a70673f743d31)](https://spatie.be/github-ad-click/crypto)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

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](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

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

[](#installation)

You can install the package via composer:

```
composer require spatie/crypto
```

Usage
-----

[](#usage)

You can generate a key pair using the `generate` function on the `KeyPair` class.

```
use Spatie\Crypto\Rsa\KeyPair;

[$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:

```
[$passwordProtectedPrivateKey, $publicKey] = (new KeyPair())->password('my-password')->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.

```
Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey);
Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);
```

Alternatively, you can also create a key object using a string.

```
Spatie\Crypto\Rsa\PrivateKey::fromString($privateKeyString);
Spatie\Crypto\Rsa\PublicKey::fromString($publicKeyString);
```

If the private key is password protected, you need to pass the password as the second argument.

```
Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey, $password);
Spatie\Crypto\Rsa\PrivateKey::fromString($privateKeyString, $password);
```

If you do not specify the right password, a `Spatie\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 = Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey);
$encryptedData = $privateKey->encrypt($data); // encrypted data contains something unreadable

$publicKey = Spatie\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 `Spatie\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 = Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);
$encryptedData = $publicKey->encrypt($data); // encrypted data contains something unreadable

$privateKey = Spatie\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 `Spatie\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.

```
Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey)->canDecrypt($data); // returns a boolean;
Spatie\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 = Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey)->sign('my message'); // returns a string

$publicKey = Spatie\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.

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

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 88.9% 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

Every ~39 days

Recently: every ~49 days

Total

6

Last Release

1812d ago

Major Versions

0.0.3 → 1.0.02020-11-09

1.0.0 → 2.0.02020-12-03

PHP version history (2 changes)0.0.1PHP ^8.0

0.0.2PHP ^7.4|^8.0

### Community

Maintainers

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

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (56 commits)")[![woenel](https://avatars.githubusercontent.com/u/8844488?v=4)](https://github.com/woenel "woenel (2 commits)")[![francislavoie](https://avatars.githubusercontent.com/u/2111701?v=4)](https://github.com/francislavoie "francislavoie (1 commits)")[![riasvdv](https://avatars.githubusercontent.com/u/3626559?v=4)](https://github.com/riasvdv "riasvdv (1 commits)")[![simivar](https://avatars.githubusercontent.com/u/828020?v=4)](https://github.com/simivar "simivar (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")[![noplanman](https://avatars.githubusercontent.com/u/9423417?v=4)](https://github.com/noplanman "noplanman (1 commits)")

---

Tags

spatiecrypto

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/eplayment-laravel-crypto-pkg/health.svg)

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

###  Alternatives

[phpseclib/phpseclib

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

5.6k434.8M1.3k](/packages/phpseclib-phpseclib)[defuse/php-encryption

Secure PHP Encryption Library

3.9k162.4M214](/packages/defuse-php-encryption)[spatie/crypto

Encrypting and signing data using private/public keys

486763.5k10](/packages/spatie-crypto)[spatie/laravel-csp

Add CSP headers to the responses of a Laravel app

8519.6M19](/packages/spatie-laravel-csp)[spatie/laravel-ciphersweet

Use ciphersweet in your Laravel project

416718.4k1](/packages/spatie-laravel-ciphersweet)[spatie/security-advisories-health-check

A Laravel Health check to security advisories for PHP packages

481.6M10](/packages/spatie-security-advisories-health-check)

PHPackages © 2026

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