PHPackages                             jamielsharief/encryption - 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. jamielsharief/encryption

ActiveLibrary[Security](/categories/security)

jamielsharief/encryption
========================

PHP Encryption Library

1.3.1(5y ago)11341MITPHPPHP &gt;=7.3.0

Since Feb 25Pushed 5y ago1 watchersCompare

[ Source](https://github.com/jamielsharief/encryption)[ Packagist](https://packagist.org/packages/jamielsharief/encryption)[ RSS](/packages/jamielsharief-encryption/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (9)Used By (1)

Encryption
==========

[](#encryption)

[![license](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)[![build](https://github.com/jamielsharief/encryption/workflows/CI/badge.svg)](https://github.com/jamielsharief/encryption/actions)[![Coverage Status](https://camo.githubusercontent.com/cf891dc23623a090c13412b41afa39fcda8fdd317c9065472470ae798488356d/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6a616d69656c736861726965662f656e6372797074696f6e2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/jamielsharief/encryption?branch=master)

This library supports both asymmetric (using key pairs) and symmetric (single key) encryption. There is also a Hybrid encryption which uses both asymmetric and symmetric. Both encrypted data and signatures are returned as a `Base64` encoded string.

Asymmetric Encryption
---------------------

[](#asymmetric-encryption)

### Generating Keys

[](#generating-keys)

To generate a key pair

```
use Encryption\KeyPair;
$keyPair = KeyPair::generate();

$publicKey = $keyPair->publicKey(); // this is used to encrypt data
$privateKey = $keyPair->privateKey(); // this is to decrypt data
$string = $keyPair->toString(); // combines both key into a single string
```

Generate accepts the following options:

- size: default:4096 the size of the key
- passphrase: a password to encrypt the key with

Working with Private Keys
-------------------------

[](#working-with-private-keys)

To create a `PrivateKey` object using a private key string, pass this to the constructor

```
use Encryption\PrivateKey;
$privateKey = new PrivateKey($string);
$privateKey = new PrivateKey($string, ['passphrase' => 'secret']));
```

To create a `PrivateKey` object by loading from a file

```
use Encryption\PrivateKey;
$privateKey = PrivateKey::load($path);
$privateKey = PrivateKey::load($path, ['passphrase' => 'secret']);
```

Things you can do with the `PrivateKey` object

```
$encrypted = $privateKey->encrypt($data);
$decrypted = $privateKey->decrypt($encrypted); // decrypts data encrypted by public key
$signature = $privateKey->sign($data);
$publicKey = $privateKey->extractPublicKey();
$bits = $privateKey->bits(); // 4096
echo $privateKey->toString();
```

You can also generate a private key using the static method `generate`, this will return a new `PrivateKey` object.

```
$privateKey = PrivateKey::generate();
```

Working with Public Keys
------------------------

[](#working-with-public-keys)

To create a `PublicKey` object using a public key string, pass this to the constructor

```
use Encryption\PublicKey;
$publicKey = new PublicKey($string);
```

To create a `PublicKey` object by loading from a file

```
use Encryption\PublicKey;
$publicKey = PublicKey::load($path);
```

Things you can do with the `PublicKey` object

```
$encrypted = $publicKey->encrypt($data);
$decrypted = $publicKey->decrypt($encrypted); // decrypts data encrypted by private key
$signature = $publicKey->verify($data, $signature);
$fingerprint = $publicKey->fingerprint(); // D52A E482 CBE7 BB75 0148 3851 93A3 910A 0719 994D
$bits = $publicKey->bits(); // 4096
echo $publicKey->toString();
```

### Keychain

[](#keychain)

You can also manage keys with `Keychain`

```
$keychain = new Keychain(__DIR__ . '/keys');
```

#### Creating keys and adding to the Key Chain

[](#creating-keys-and-adding-to-the-key-chain)

To create a private and public key pair and add this to the `Keychain`, you can pass an email address, username, UUID or any other unique id.

```
$keychain->create('jon@example.com');
```

You can also set an expiry date for the key

```
$keychain->create('jon@example.com',[
    'expires' => '+ 1 year'
]);
```

#### Adding

[](#adding)

> When you add a private key, the public key will be extracted and added to the same document.

To add a private or public key from a string.

```
$keychain->add('user@example.com',(string) $privateKey);
```

#### Importing

[](#importing)

> When you add a private key, the public key will be extracted and added to the same document.

To import an existing public key or private/public key pair

```
$keychain->import('user-1979', __DIR__ .'/privateKey');
```

You can also set an expiry date for the key

```
$keychain->import('user-1979', __DIR__ .'/publicKey',[
    'expires' => '+ 1 year'
]);
```

### Get

[](#get)

To get a key and data

```
$key = $keychain->get('jon@example.com');
/*
DocumentStore\Document Object
(
    [id] => 784e148db03ac07ff34ae57c29b01549
    [name] => user@example.com
    [privateKey] => -----BEGIN PRIVATE KEY-----
MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA0BaIweRiLW1Uunxw
NrPr9GaNWtnr+FbzsY8DNf894yI4n6q47s7yTPCFmHuIDzKaYx0xdS3L2XcY3HYg
ctPUNQIDAQABAkAMQ/fFrgeXc+VVpLYck1hqLI1SeJvvJHjy02I2EZh9RdDcBKi9
+MOuP+TzkVL0w1QAFgB8nPGblPjUB6FMhkwVAiEA9VmWwKxlTevev7XcOUYSOabv
qHeqab6aY8H1+o9+e3MCIQDZHuDTTizUW4frKhvtKiBkwAV4YdErVM9LNFC+TFTX
twIhAL8o/FJGf+/EVRtdoKZnOA//Rz8lbXtSbIxJNVPxtYSNAiBhI5CA2WPzKnRY
AUH3TLarfMG1x0W29j28Ls7FJQ98ZwIgH5Esr246hK1bSGO4R2Z6yFCcBfo1Sgib
bjupP+8HbUs=
-----END PRIVATE KEY-----
    [publicKey] => -----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANAWiMHkYi1tVLp8cDaz6/RmjVrZ6/hW
87GPAzX/PeMiOJ+quO7O8kzwhZh7iA8ymmMdMXUty9l3GNx2IHLT1DUCAwEAAQ==
-----END PUBLIC KEY-----

    [fingerprint] => E010 6888 BE78 1571 D35A D3CC 22C7 62D3 6025 E288
    [expires] => 2050-01-01 12:00:00
    [type] => key-pair
    [comment] => foo
    [created] => 2020-11-20 17:07:41
)
*/
```

### Delete

[](#delete)

To delete a key and data

```
$keychain->delete('jon@example.com');
```

### List

[](#list)

To get a list of keys

```
$keychain->list();
```

Symmetric Encryption
--------------------

[](#symmetric-encryption)

First you will need to generate a key that is 256 bit/32 bytes

```
$crypto = new SymmetricEncryption();
$key = $crypto->generateKey(); // 3LSpUJL4s0HNLun4T1KcheGjrVtCjaQ7
```

To encrypt a string

```
$crypto = new SymmetricEncryption();
$encrypted = $crypto->encrypt($text, $key);
```

To decrypt a string

```
$crypto = new SymmetricEncryption();
$decrypted = $crypto->decrypt($text, $key);
```

Hybrid Encryption
-----------------

[](#hybrid-encryption)

> This can only decrypt data encrypted with the Hybrid Encryption class

Hybrid encryption uses both asymmetric and symmetric encryption. With hybrid encryption there is no limit on message size.

```
$publicKey = PublicKey::load($pathToPublicKey);
$privateKey = PrivateKey::load($pathToPrivateKey);

$crypto = new HybridEncryption();

$encrypted = $crypto->encrypt($data, $publicKey);
echo $crypto->decrypt($encrypted, $privateKey);
```

By default encrypted/signed data is wrapped in a ENCRYPTED DATA or SIGNATURE boundary, however this can be disabled when encrypting or signing data. For example

```
-----BEGIN ENCRYPTED DATA-----
eGrjYfLFQI/gVWfpZeEA05q7Swb9gaKRalZnBZ788mGXiOhj1+f+a2RLJxDu24FE1HnFd70YcPAAdWme1Lu0yQ==
-----END ENCRYPTED DATA-----

```

Decryption and signature verification will remove boundaries automatically if they are found present in the data.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity56

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

Every ~45 days

Recently: every ~14 days

Total

8

Last Release

1949d ago

Major Versions

0.2.1 → 1.0.02020-11-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/7718a059aa8ef3d6e9f6e5bd7f4d5b09b7321a059f2669e61546d1a665fbb372?d=identicon)[jamielsharief](/maintainers/jamielsharief)

---

Top Contributors

[![jamielsharief](https://avatars.githubusercontent.com/u/20553479?v=4)](https://github.com/jamielsharief "jamielsharief (56 commits)")

---

Tags

asymmetricencryptionfingerprintkey-pairskeychainpassphrasesignaturesymmetricverifyencryptionaesencryptdecryptopenssldecryptionasymmetric

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jamielsharief-encryption/health.svg)

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

###  Alternatives

[defuse/php-encryption

Secure PHP Encryption Library

3.9k162.4M212](/packages/defuse-php-encryption)[nzo/url-encryptor-bundle

The NzoUrlEncryptorBundle is a Symfony Bundle used to Encrypt and Decrypt data and variables in the Web application or passed through URL

961.0M2](/packages/nzo-url-encryptor-bundle)[miladrahimi/phpcrypt

Encryption, decryption, and hashing tools for PHP projects

3171.5k2](/packages/miladrahimi-phpcrypt)[poly-crypto/poly-crypto

High-level cryptographic functions that are interoperable between NodeJS and PHP 7.1+

127.8k1](/packages/poly-crypto-poly-crypto)[hemiframe/php-aes

PHP class for encrypt and decrypt data with AES algorithm

1030.3k](/packages/hemiframe-php-aes)

PHPackages © 2026

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