PHPackages                             miladrahimi/phpcrypt - 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. miladrahimi/phpcrypt

ActiveLibrary[Security](/categories/security)

miladrahimi/phpcrypt
====================

Encryption, decryption, and hashing tools for PHP projects

v5.0.0(5y ago)3171.5k↓25%62MITPHPPHP &gt;=7.1CI failing

Since Jun 18Pushed 5y ago3 watchersCompare

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

READMEChangelog (10)Dependencies (1)Versions (12)Used By (2)

[![Latest Stable Version](https://camo.githubusercontent.com/d12f823d8729693a5f8087a2009d71d685ecd9b1c358c2bab10ab3b8eb543370/68747470733a2f2f706f7365722e707567782e6f72672f6d696c6164726168696d692f70687063727970742f762f737461626c65)](https://packagist.org/packages/miladrahimi/phpcrypt)[![Total Downloads](https://camo.githubusercontent.com/a573c4bfdc15ac6006bd3a33c3a8e961e6256d0c051e5963854ad66d391eaacd/68747470733a2f2f706f7365722e707567782e6f72672f6d696c6164726168696d692f70687063727970742f646f776e6c6f616473)](https://packagist.org/packages/miladrahimi/phpcrypt)[![Build Status](https://camo.githubusercontent.com/f8f76ef28c3aa1d1b2859cf51cfb764022b1d6ccb016cd342ccb4aadfc2f50eb/68747470733a2f2f7472617669732d63692e6f72672f6d696c6164726168696d692f70687063727970742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/miladrahimi/phpcrypt)[![Coverage Status](https://camo.githubusercontent.com/1d6af1ab0d3a5ab8bf116812a73d741a1206358fd83abe0a57d8679e4443eb20/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d696c6164726168696d692f70687063727970742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/miladrahimi/phpcrypt?branch=master)[![License](https://camo.githubusercontent.com/91f87bda0fa0bebaecc1d14f6fa0e93ebc2a349316431cd827aa8c3f2d9ba517/68747470733a2f2f706f7365722e707567782e6f72672f6d696c6164726168696d692f70687063727970742f6c6963656e7365)](https://packagist.org/packages/miladrahimi/phpcrypt)

PhpCrypt
========

[](#phpcrypt)

PhpCrypt is a package for encryption, decryption, and hashing data in PHP projects. It provides an easy-to-use and fluent interface.

Features:

- Symmetric encryption/decryption using AES and other symmetric methods.
- Asymmetric encryption/decryption using the RSA method.
- Hashing and verifying data (e.g. passwords) using the BCrypt method.

Versions
--------

[](#versions)

- v5.x.x
- v4.x.x
- v3.x.x (Unsupported)
- v2.x.x (Unsupported)
- v1.x.x (Unsupported)

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

[](#installation)

Install [Composer](https://getcomposer.org) and run the following command in your project's root directory:

```
composer require miladrahimi/phpcrypt "5.*"
```

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

[](#symmetric-encryption)

This example shows how to encrypt and decrypt data using symmetric algorithms like AES.

```
use MiladRahimi\PhpCrypt\Symmetric;

$symmetric = new Symmetric();
$encryptedData = $symmetric->encrypt('secret');
echo $symmetric->decrypt($encryptedData); // secret
```

It generates a random key and uses `aes-256-cbc` method for encrypting/decrypting data.

### Custom Key

[](#custom-key)

If you have already a key, you can use your own key like this:

```
use MiladRahimi\PhpCrypt\Symmetric;

$key = '1234567890123456';

// Set the key using the constructor
$symmetric = new Symmetric($key);

// Or set the key using the setter
$symmetric = new Symmetric();
$symmetric->setKey($key);

// And get the key using the getter
$myKey = $symmetric->getKey();
```

The method `generateKey` can help you to generate a new random key. See the snippet below.

```
use MiladRahimi\PhpCrypt\Symmetric;

$key = Symmetric::generateKey();
```

### Custom Methods

[](#custom-methods)

In default, The `Symmetric` class uses `aes-256-cbc` method to encrypt/decrypt data. You can use your preferred method as well. See the following example.

```
use MiladRahimi\PhpCrypt\Exceptions\MethodNotSupportedException;
use MiladRahimi\PhpCrypt\Symmetric;

try {
    $symmetric = new Symmetric();
    $symmetric->setMethod('aria-256-ctr');
    // ...
} catch (MethodNotSupportedException $e) {
    // The method is not supported.
}
```

### Supported Methods

[](#supported-methods)

If you want to know which methods do your installed OpenSSL extension support, see the snippet below:

```
use MiladRahimi\PhpCrypt\Symmetric;

print_r(Symmetric::supportedMethods());
```

RSA Encryption
--------------

[](#rsa-encryption)

RSA is a popular asymmetric encryption/decryption algorithm. The examples below illustrate how to encrypt/decrypt data using the RSA algorithm.

### Encryption with private key

[](#encryption-with-private-key)

In this example, we encrypt data with a private key and decrypt it with the related public key.

```
use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;

$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');

$result = $privateRsa->encrypt('secret');
echo $publicRsa->decrypt($result); // secret
```

### Encryption with public key

[](#encryption-with-public-key)

In this example, we encrypt data with a public key and decrypt it with the related private key.

```
use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;

$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');

$result = $publicRsa->encrypt('secret');
echo $privateRsa->decrypt($result); // secret
```

### Base64 Encoding

[](#base64-encoding)

In default, the encrypted data returned by `PrivateRsa::encrypt()` and `PublicRsa::encrypt()` methods will be Base64 encoded. You can disable this encoding like the example below.

```
use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;

$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');

// Disable Base64 encoding for public encryption
$result = $publicRsa->encrypt('secret', false);

// Disable Base64 encoding for private encryption
$result = $privateRsa->encrypt('secret', false);
```

Hashing
-------

[](#hashing)

This example shows how to hash data and verify it.

```
use MiladRahimi\PhpCrypt\Hash;

$hash = new Hash();

$hashedPassword = $hash->make('MyPassword');
echo $hash->verify('MyPassword', $hashedPassword); // true
echo $hash->verify('AnotherPassword', $hashedPassword); // false
```

Error Handling
--------------

[](#error-handling)

The `Symmetric`, `PrivateRsa`, `PublicRsa`, and `Hash` classes may throw these exceptions:

- `EncryptionException`: When it cannot encrypt data.
- `DecryptionException`: When it cannot decrypt data.
- `HashingException`: When it cannot hash data.
- `MethodNotSupportedException`: When the passed encryption method to the `Symmetric` class is not supported.
- `InvalidKeyException`: When the passed key to `PrivateRsa` or `PublicRsa` classes is not valid.

License
-------

[](#license)

PhpCrypt is initially created by [Milad Rahimi](https://miladrahimi.com) and released under the [MIT License](http://opensource.org/licenses/mit-license.php).

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity42

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity65

Established project with proven stability

 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 ~194 days

Recently: every ~462 days

Total

11

Last Release

2040d ago

Major Versions

1.6 → 2.02015-09-24

2.0 → 3.02017-06-24

3.0 → v4.02020-01-14

v4.0 → v5.x-dev2020-10-17

PHP version history (3 changes)v1.0PHP &gt;=5.3.0

3.0PHP &gt;=5.4.0

v4.0PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/921274b8bb29236d8f94f6c83100a5f751f6394c4c49741e1b92bfbccac0911b?d=identicon)[miladrahimi](/maintainers/miladrahimi)

---

Top Contributors

[![miladrahimi](https://avatars.githubusercontent.com/u/6689295?v=4)](https://github.com/miladrahimi "miladrahimi (47 commits)")

---

Tags

aesasymmetriccryptographydecryptencrypthashopensslrsasymmetriccryptographyrsaaespasswordencryptdecrypthashopensslcryptasymmetricsymmetric

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/miladrahimi-phpcrypt/health.svg)

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

###  Alternatives

[defuse/php-encryption

Secure PHP Encryption Library

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

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

127.8k1](/packages/poly-crypto-poly-crypto)[vlucas/pikirasa

PKI public/private RSA key encryption using the OpenSSL extension

104101.1k1](/packages/vlucas-pikirasa)[xxtea/xxtea

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

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