PHPackages                             nmure/encryptor - 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. nmure/encryptor

ActiveLibrary[Security](/categories/security)

nmure/encryptor
===============

PHP data encryptor using open\_ssl

v1.0.1(8y ago)723.0k↓31.8%31MITPHPPHP &gt;=5.4

Since Aug 7Pushed 8y ago3 watchersCompare

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

READMEChangelog (2)DependenciesVersions (3)Used By (1)

NmureEncryptor
==============

[](#nmureencryptor)

[![Build Status](https://camo.githubusercontent.com/03f262bd1be831c06d56739360d37a55ed1a00fac809585220570b8394f19af6/68747470733a2f2f7472617669732d63692e6f72672f6e69636f6c61736d7572652f4e6d757265456e63727970746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/nicolasmure/NmureEncryptor)[![Coverage Status](https://camo.githubusercontent.com/e80cdaca42eb0981491f79c6d2191a7f90e35d5698819e11ceaf687847c5bcbc/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6e69636f6c61736d7572652f4e6d757265456e63727970746f722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/nicolasmure/NmureEncryptor?branch=master)

PHP data encryptor using open\_ssl

Table of contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
    - [Basic usage](#basic-usage)
        - [Encrypt](#encrypt)
        - [Decrypt](#decrypt)
    - [Advanced usage](#advanced-usage)
- [Formatters](#formatters)
    - [Built-in formatters](#built-in-formatters)
        - [Base64Formatter](#base64formatter)
        - [HexFormatter](#hexformatter)
    - [Make your own formatter](#make-your-own-formatter)
- [API](#api)
- [Troubleshooting](#troubleshooting)
    - [Using the HexFormatter with a C# app](#using-the-hexformatter-with-a-c-app)
- [Integration](#integration)
- [Development / contributing](#development--contributing)
    - [Installing ecosystem](#installing-ecosystem)
    - [Testing](#testing)
    - [PHP CS Fixer](#php-cs-fixer)
- [License](#license)

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

[](#installation)

Use composer to install the lib :

```
composer require nmure/encryptor "~1.0.0"
```

Usage
-----

[](#usage)

### Basic usage

[](#basic-usage)

#### Encrypt

[](#encrypt)

The simpliest way to use this library is to create an instance of the Encryptor by passing it a secret key and a cipher method to use during encryption. To see all the cipher methods supported by your php installation, use the [openssl\_get\_cipher\_methods](http://php.net/manual/en/function.openssl-get-cipher-methods.php "Gets available cipher methods") function.

Then you can encrypt your plain text data, for instance :

```
use Nmure\Encryptor\Encryptor;

$encryptor = new Encryptor('452F93C1A737722D8B4ED8DD58766D99', 'AES-256-CBC');
$encrypted = $encryptor->encrypt('plain text data');
```

The encryptor uses an Initialization Vector (IV) in addition to the secret key to encrypt data. This IV is randomly generated to be sure that 2 encryptions of the same data with the same key won't produce the same encrypted output.

Thereby, you should store the IV used to encrypt your data along side to the encrypted data to be able to decrypt it later.

For instance, you could store it in a database :

```
| id |  iv  | encrypted |
-----------------------
| 1  | 945a | oifd4867h |
| 2  | 894d | 62vbyibd6 |

```

##### Decrypt

[](#decrypt)

Then, to decrypt your data, initialize the encryptor and call the `decrypt` function :

```
use Nmure\Encryptor\Encryptor;

$encryptor = new Encryptor('452F93C1A737722D8B4ED8DD58766D99', 'AES-256-CBC');
// retrieve the IV ($iv) and the encryped data ($encrypted) from your DB
// ...
$encryptor->setIv($iv);
$plainText = $encryptor->decrypt($encrypted);
```

### Advanced usage

[](#advanced-usage)

If you don't want to deal with how to store the encrypted data and the IV, you can use the [formatters](#formatters "Formatters documentation"). The formatters combine the IV and the encrypted data into one string to make it easier to store and to share with an other app.

For instance, if you want to store an encrypted data to a file, you could use the [Base64Formatter](#base64formatter "Base64Formatter documentation") :

```
use Nmure\Encryptor\Encryptor;
use Nmure\Encryptor\Formatter\Base64Formatter;

$encryptor = new Encryptor('452F93C1A737722D8B4ED8DD58766D99', 'AES-256-CBC');
$encryptor->setFormatter(new Base64Formatter());

// will produce a string containg the IV and the encrypted data
$encrypted = $encryptor->encrypt('plain text data');
// store $encrypted to a file
```

Then, to decrypt your data, use the same encryptor / formatter couple and simply call the `decrypt` function with the combined string :

```
use Nmure\Encryptor\Encryptor;
use Nmure\Encryptor\Formatter\Base64Formatter;

$encryptor = new Encryptor('452F93C1A737722D8B4ED8DD58766D99', 'AES-256-CBC');
$encryptor->setFormatter(new Base64Formatter());

// get the encrypted data from a file, or whatever
// ... $encrypted

// the encryptor uses the formatter to get the IV used for the encryption from
// the given $encrypted string
$plainText = $encryptor->decrypt($encrypted);
```

If you don't want to use the formatter anymore, simply set it to `null` on the encryptor :

```
$encryptor->setFormatter(null);
```

Using formatters is more convenient as all the work to store the IV along side the encrypted data is done by the formatters, and not by you anymore.

Formatters
----------

[](#formatters)

The formatters are used to combine the IV and the encrypted data into one string (usually a non binary string), to make it easier to store and to share accross systems. They all implement the [`FormatterInterface`](/src/Formatter/FormatterInterface.php "Nmure\Encryptor\Formatter\FormatterInterface").

### Built-in formatters

[](#built-in-formatters)

#### Base64Formatter

[](#base64formatter)

The string returned by the [`Base64Formatter`](/src/Formatter/Base64Formatter.php "Nmure\Encryptor\Formatter\Base64Formatter")during the encryption process contains the base64 encoded IV, concatened to a colon (`:`), concatened to the base64 encoded encrypted data.

As the colon is not a char from the base64 chars, we can easily split this string in two parts from the colon, and get back the IV and the encrypted data during the decryption process.

#### HexFormatter

[](#hexformatter)

The string returned by the [`HexFormatter`](/src/Formatter/HexFormatter.php "Nmure\Encryptor\Formatter\HexFormatter")during the encryption process contains the hex representation of the concatened IV and encrypted data binary string.

During the decryption process, this string is splitted to get back the IV and the encrypted data. We use the cipher method's IV length to determine where to split this string.

### Make your own formatter

[](#make-your-own-formatter)

You can of course make your own formatter to suit your needs, it must just implement the [`FormatterInterface`](/src/Formatter/FormatterInterface.php "Nmure\Encryptor\Formatter\FormatterInterface").

API
---

[](#api)

- **Nmure\\Encryptor\\Encryptor** :
    - `public string encrypt($data)` : encrypts the given plain text string and returns it. When a formatter is set to the encyryptor, the returned value of this function is the formatted string composed of the IV and the ecrypted data.
    - `public string decrypt($data)` : decrypts the given encrypted data and returns it. When a formatter is set to the encryptor, the given data must be the string formatted by this formatter. The IV will be determined from the formatted string. When no formatter is set, the IV must be set to this encryptor to be able to decrypt the given data.
    - `public string generateIv()` : generate a new ramdom IV according to the cipher method, set it to the encryptor and returns it.
    - `public void enableAutoIvUpdate()` : enable the automatic IV update before each encryption process to be sure that two encryptions of the same data won't produce the same output. The automatic IV update is enabled by default.
    - `public void disableAutoIvUpdate()` : disable the automatic IV update before each encryption process. The encryption will use the last set IV or generate one if no IV was set.
    - `public void turnHexKeyToBin()` : turns the hex secret key into a binary key.
    - `public string getIv()` : returns the IV of the encryptor.
    - `public void setIv($iv)` : set the given IV to the encryptor.
    - `public void setFormatter(FormatterInterface $formatter = null)` : sets the given FormatterInterface to the encryptor. To unset the formatter, pass `null` to this function.
- **Nmure\\Encryptor\\Formatter\\FormatterInterface** :
    - `public string format($iv, $encrypted)` : formats the given IV and encrypted data to a string and returns it.
    - `public array parse($input, $ivLength)` : parse the given `$input` string and return an array containing the IV and the encrypted data. The `$ivLength` parameter can be used to parse the `$input` string.

Troubleshooting
---------------

[](#troubleshooting)

### Using the HexFormatter with a C# app

[](#using-the-hexformatter-with-a-c-app)

If you use this formatter with the encryptor to share crypted data with a C# app, you'll probably have to turn your secret key into a binary key :

```
use Nmure\Encryptor\Encryptor;
use Nmure\Encryptor\Formatter\HexFormatter;

$encryptor = new Encryptor('452F93C1A737722D8B4ED8DD58766D99', 'AES-256-CBC');
$encryptor->turnHexKeyToBin(); // turning the hex key to a binary key
$encryptor->setFormatter(new HexFormatter());

$encrypted = $encryptor->encrypt('plain text data');
```

Integration
-----------

[](#integration)

You can use this library as standalone, or if you're using Symfony, it is wrapped inside the [`NmureEncryptorBundle`](https://github.com/nicolasmure/NmureEncryptorBundle "A Symfony Bundle for the nmure/encryptor library").

Development / Contributing
--------------------------

[](#development--contributing)

### Installing ecosystem

[](#installing-ecosystem)

```
docker-compose run --rm composer install
```

### Testing

[](#testing)

```
docker-compose run --rm phpunit -c /app
```

The formatters test classes should extend the [`AbstractFormatterTest`](/tests/Nmure/Encryptor/Tests/Formatter/AbstractFormatterTest.php "Nmure\Encryptor\Tests\Formatter\AbstractFormatterTest") class to be sure that the formatters fulfill the minimum requirements.

### PHP CS Fixer

[](#php-cs-fixer)

```
docker-compose run --rm phpcs phpcbf --standard=PSR2 /scripts/
```

License
-------

[](#license)

This library is licensed under the MIT License. More informations in the [LICENSE](/LICENSE) file.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% 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 ~482 days

Total

2

Last Release

3089d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4362252?v=4)[Nicolas MURE](/maintainers/nicolasmure)[@nicolasmure](https://github.com/nicolasmure)

---

Top Contributors

[![nicolasmure](https://avatars.githubusercontent.com/u/4362252?v=4)](https://github.com/nicolasmure "nicolasmure (4 commits)")[![NicolasNSSM](https://avatars.githubusercontent.com/u/2535764?v=4)](https://github.com/NicolasNSSM "NicolasNSSM (2 commits)")

---

Tags

encryptoropensslphpsecurityencryptiondataencryptdecrypthashsslopen

### Embed Badge

![Health badge](/badges/nmure-encryptor/health.svg)

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

###  Alternatives

[defuse/php-encryption

Secure PHP Encryption Library

3.9k162.4M214](/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)[xxtea/xxtea

XXTEA is a fast and secure encryption algorithm. This is a XXTEA library for PHP.

11341.7k](/packages/xxtea-xxtea)[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)

PHPackages © 2026

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