2017 © Pedro Peláez
 

library phpcrypt

Free PHP encryption, decryption and hashing tools for neat and powerful projects!

image

miladrahimi/phpcrypt

Free PHP encryption, decryption and hashing tools for neat and powerful projects!

  • Saturday, June 24, 2017
  • by miladrahimi
  • Repository
  • 2 Watchers
  • 8 Stars
  • 341 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 9 Versions
  • 8 % Grown

The README.md

Latest Stable Version Total Downloads Build Status Coverage Status License, (*1)

PhpCrypt

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

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., (*3)

Versions

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

Installation

Install Composer and run the following command in your project's root directory:, (*4)

composer require miladrahimi/phpcrypt "5.*"

Symmetric Encryption

This example shows how to encrypt and decrypt data using symmetric algorithms like AES., (*5)

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., (*6)

Custom Key

If you have already a key, you can use your own key like this:, (*7)

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., (*8)

use MiladRahimi\PhpCrypt\Symmetric;

$key = Symmetric::generateKey();

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., (*9)

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

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

use MiladRahimi\PhpCrypt\Symmetric;

print_r(Symmetric::supportedMethods());

RSA Encryption

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

Encryption with private key

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

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

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

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

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., (*14)

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

This example shows how to hash data and verify it., (*15)

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

The Symmetric, PrivateRsa, PublicRsa, and Hash classes may throw these exceptions:, (*16)

  • 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

PhpCrypt is initially created by Milad Rahimi and released under the MIT License., (*17)

The Versions

24/06 2017

dev-master

9999999-dev http://miladrahimi.github.io

Free PHP encryption, decryption and hashing tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-openssl *

 

The Development Requires

password encrypt decrypt cryptography openssl hash verify mcrypt decipher encipher

24/06 2017

3.0

3.0.0.0 http://miladrahimi.github.io

Free PHP encryption, decryption and hashing tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-openssl *

 

The Development Requires

password encrypt decrypt cryptography openssl hash verify decipher encipher

24/09 2015

2.0

2.0.0.0 http://miladrahimi.github.io

Free PHP cryptography and hashing tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

password encrypt decrypt cryptography hash verify mcrypt decipher encipher

26/07 2015

1.6

1.6.0.0 http://miladrahimi.github.io/phpcrypt

Free PHP cryptography tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

password encrypt decrypt cryptography hash verify mcrypt decipher encipher

26/07 2015

1.5

1.5.0.0 http://miladrahimi.github.io/phpcrypt

Free PHP cryptography tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

password encrypt decrypt cryptography hash verify mcrypt decipher encipher

10/07 2015

v1.4

1.4.0.0 http://miladrahimi.github.io/phpcrypt

Free PHP cryptography tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

password encrypt decrypt cryptography hash verify mcrypt decipher encipher

10/07 2015

v1.2

1.2.0.0 http://miladrahimi.github.io/phpcrypt

Free PHP cryptography tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

password encrypt decrypt cryptography hash verify mcrypt decipher encipher

28/06 2015

v1.1

1.1.0.0 http://phpcrypt.neatplex.com

Free PHP cryptography tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

password encrypt decrypt cryptography hash verify mcrypt decipher encipher

18/06 2015

v1.0

1.0.0.0 http://phpcrypt.neatplex.com

Free PHP cryptography tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

encrypt decrypt cryptography mcrypt decipher encipher