PHP Classes

PHP Simple Encryption and Decryption: Encrypt and decrypt data

Recommend this page to a friend!
  Info   View files Documentation   View files View files (194)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 354 This week: 1All time: 6,962 This week: 560Up
Version License PHP version Categories
phpsimple-encryption 1.0.7The PHP License7.2Cryptography, PHP 7
Description 

Author

This package can be used to encrypt and decrypt data.

It can take a given string of data and encrypt it using a given key string.

The package can also do the opposite, i.e. decrypt previously encrypted data using the same key.

It supports many types of encryption cyphers. By default it uses AES-256-CBC.

Picture of John Conde
  Performance   Level  
Name: John Conde <contact>
Classes: 6 packages by
Country: United States United States
Age: 50
All time rank: 2410328 in United States United States
Week rank: 106 Up12 in United States United States Up
Innovation award
Innovation award
Nominee: 1x

Recommendations

Read encrypted data
I need to read encrypted passwords stored in MySQL

Documentation

Latest Stable Version Total Downloads Build Scrutinizer Code Quality Maintainability License

PHP Simple Encryption (php-simple-encryption)

A PHP library that makes encryption and decryption of text easy.

Requirements

  • PHP 7.2+
  • Openssl PHP extension

Installation

Simply add a dependency on stymiee/php-simple-encryption to your project's composer.json file if you use Composer to manage the dependencies of your project.

Here is a minimal example of a composer.json file that just defines a dependency on PHP Simple Encryption:

{
    "require": {
        "stymiee/php-simple-encryption": "^1"
    }
}

Basic Usage

To get a more detailed introduction to this library, visit the PHP Simple Encryption tutorial on my blog.

require('./vendor/autoload.php');

use Encryption\Encryption;
use Encryption\Exception\EncryptionException;

$text = 'The quick brown fox jumps over the lazy dog';
$key  = 'secretkey';
try {
    $encryption = Encryption::getEncryptionObject();
    $iv = $encryption->generateIv();
    $encryptedText = $encryption->encrypt($text, $key, $iv);
    $decryptedText = $encryption->decrypt($encryptedText, $key, $iv);

    printf('Cipher   : %s%s', $encryption->getName(), PHP_EOL);
    printf('Encrypted: %s%s', $encryptedText, PHP_EOL);
    printf('Decrypted: %s%s', $decryptedText, PHP_EOL);
    printf('Version  : %s%s', Encryption::VERSION, PHP_EOL);
}
catch (EncryptionException $e) {
    echo $e;
}

Outputs

Cipher   : AES-256-CBC
Encrypted: lierDqV4Qo3Cm87YU01K+YnQsDGrFsYypjHJVZaagqfLFg7xb2T7b9qfqb4NcoIGcTzqvQbOx72AVgbuRFxqgg==
Decrypted: The quick brown fox jumps over the lazy dog
Version  : 1

An exception may be thrown if: - An invalid/unsupported cipher is attempted to be used (Encryption\Exception\InvalidCipherException) - A cipher available in openssl but yet implemented is attempted to be used (Encryption\Exception\CipherNotImplementedException) - generateIv() is unable to generate a initialization vector (Encryption\Exception\GenerateIvException). - Encryption::encrypt() is unable to encrypt the data (Encryption\Exception\EncryptException). - Encryption::decrypt() is unable to decrypt the data (Encryption\Exception\DecryptException).

Supported Ciphers

The PHP Simple Encryption library currently defaults to AES-256-CBC. This may change in future versions and will result in a major version bump when this occurs. You can check the version of your library by calling Encryption::VERSION. This library is currently on version "1".

To determine what cipher you are using you can call the getName() method on your encryption object.

$encryption = Encryption::getEncryptionObject();
$cipherName = $encryption->getName(); // AES-256-CBC

To get a list of ciphers supported by your system and this library you can call Encryption::listAvailableCiphers() to receive an array of available ciphers. This list is an intersection of available ciphers from your system's installed version of Openssl and ciphers supported by this library.

Total ciphers supported: 139 Default cipher: AES-256-CBC (version 1)

| AES | Aria/Blowfish | Camellia/Cast5/SM4 | DES/Idea/RC2/RC4/Seed | |-------------------------|---------------|--------------------|-----------------------| | aes-128-cbc | aria-128-cbc | camellia-128-cbc | des-cbc | | aes-128-cbc-hmac-sha1 | aria-128-ccm | camellia-128-cfb | des-cfb | | aes-128-cbc-hmac-sha256 | aria-128-cfb | camellia-128-cfb | des-cfb1 | | aes-128-ccm | aria-128-cfb1 | camellia-128-cfb | des-cfb8 | | aes-128-cfb | aria-128-cfb8 | camellia-128-ctr | des-ecb | | aes-128-cfb1 | aria-128-ctr | camellia-128-ecb | des-ede | | aes-128-cfb8 | aria-128-ecb | camellia-128-ofb | des-ede3 | | aes-128-ctr | aria-128-gcm | camellia-192-cbc | des-ede-cbc | | aes-128-ecb | aria-128-ofb | camellia-192-cfb | des-ede-cfb | | aes-128-gcm | aria-192-cbc | camellia-192-cfb | des-ede-ofb | | aes-128-ofb | aria-192-ccm | camellia-192-cfb | des-ede3-cbc | | aes-128-xts | aria-192-cfb | camellia-192-ctr | des-ede3-cfb | | aes-192-cbc | aria-192-cfb1 | camellia-192-ecb | des-ede3-cfb1 | | aes-192-ccm | aria-192-cfb8 | camellia-192-ofb | des-ede3-cfb8 | | aes-192-cfb | aria-192-ctr | camellia-256-cbc | des-ede3-ofb | | aes-192-cfb1 | aria-192-ecb | camellia-256-cfb | des-ofb | | aes-192-cfb8 | aria-192-gcm | camellia-256-cfb | desx-cbc | | aes-192-ctr | aria-192-ofb | camellia-256-cfb | id-aes128-ccm | | aes-192-ecb | aria-256-cbc | camellia-256-ctr | id-aes128-gcm | | aes-192-gcm | aria-256-ccm | camellia-256-ecb | id-aes192-ccm | | aes-192-ofb | aria-256-cfb | camellia-256-ofb | id-aes192-gcm | | aes-256-cbc | aria-256-cfb1 | cast5-cbc | id-aes256-ccm | | aes-256-cbc-hmac-sha1 | aria-256-cfb8 | cast5-cfb | id-aes256-gcm | | aes-256-cbc-hmac-sha256 | aria-256-ctr | cast5-ecb | idea-cbc | | aes-256-ccm | aria-256-ecb | cast5-ofb | idea-cfb | | aes-256-cfb | aria-256-gcm | chacha20 | idea-ecb | | aes-256-cfb1 | aria-256-ofb | chacha20-poly1305 | idea-ofb | | aes-256-cfb8 | bf-cbc | seed-cbc | rc2-40-cbc | | aes-256-ctr | bf-cfb | seed-cfb | rc2-64-cbc | | aes-256-ecb | bf-ecb | seed-ecb | rc2-cbc | | aes-256-gcm | bf-ofb | seed-ofb | rc2-cfb | | aes-256-ofb | sm4-cbc | | rc2-ecb | | aes-256-xts | sm4-cfb | | rc2-ofb | | | sm4-ctr | | rc4 | | | sm4-ecb | | rc4-40 | | | | | rc4-hmac-md5 |

Notes

If the text to be encrypted contains trailing null characters they will be removed when decrypting those values.

Some ciphers may not work on your version of PHP. The newer your version of PHP (and its openssl extension), the more ciphers that will be available to you.

Support

If you require assistance using this library start by viewing the HELP.md file included in this package. It includes common problems and their solutions.

If you need additional assistance, I can be found at Stack Overflow. Be sure when you ask a question pertaining to the usage of this library be sure to tag your question with the PHP and encryption tags. Make sure you follow their guide for asking a good question as poorly asked questions will be closed, and I will not be able to assist you.

A good question will include all the following: - A description of the problem (what are you trying to do? what results are you expecting? what results are you actually getting?) - The code you are using (only post the relevant code) - Your unencrypted text - The key you are using - The IV you are using - The output of your method call(s) - Any error message(s) you are getting

Do not use Stack Overflow to report bugs. Bugs may be reported here.


  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imageartifacts (1 file)
Files folder imagesrc (1 directory)
Files folder imagetests (2 directories)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file CHANGELOG Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file HELP.md Data Auxiliary data
Accessible without login Plain text file license.txt Doc. Documentation
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file SECURITY.md Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file php.yml Data Auxiliary data

  Files folder image Files  /  artifacts  
File Role Description
  Accessible without login Plain text file cipherExamples.txt Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageEncryption (1 file, 4 directories)

  Files folder image Files  /  src  /  Encryption  
File Role Description
Files folder imageCipher (4 files, 14 directories)
Files folder imageExceptions (7 files)
Files folder imageInterfaces (5 files)
Files folder imageTraits (8 files)
  Plain text file Encryption.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  
File Role Description
Files folder imageAES (36 files)
Files folder imageARIA (27 files)
Files folder imageBF (4 files)
Files folder imageCAMELLIA (21 files)
Files folder imageCAST5 (4 files)
Files folder imageCHACHA20 (2 files)
Files folder imageDES (16 files)
Files folder imageDESX (1 file)
Files folder imageID (13 files)
Files folder imageIDEA (4 files)
Files folder imageRC2 (6 files)
Files folder imageRC4 (3 files)
Files folder imageSEED (4 files)
Files folder imageSM4 (5 files)
  Plain text file ACipher.php Class Class source
  Plain text file ACipherAeadMode.php Class Class source
  Plain text file ACipherNoInitializationVector.php Class Class source
  Plain text file ACipherWithInitializationVector.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  AES  
File Role Description
  Plain text file Aes128cbc.php Class Class source
  Plain text file Aes128cbchmacsha1.php Class Class source
  Plain text file Aes128cbchmacsha256.php Class Class source
  Plain text file Aes128ccm.php Class Class source
  Plain text file Aes128cfb.php Class Class source
  Plain text file Aes128cfb1.php Class Class source
  Plain text file Aes128cfb8.php Class Class source
  Plain text file Aes128ctr.php Class Class source
  Plain text file Aes128ecb.php Class Class source
  Plain text file Aes128gcm.php Class Class source
  Plain text file Aes128ocb.php Class Class source
  Plain text file Aes128ofb.php Class Class source
  Plain text file Aes128xts.php Class Class source
  Plain text file Aes192cbc.php Class Class source
  Plain text file Aes192ccm.php Class Class source
  Plain text file Aes192cfb.php Class Class source
  Plain text file Aes192cfb1.php Class Class source
  Plain text file Aes192cfb8.php Class Class source
  Plain text file Aes192ctr.php Class Class source
  Plain text file Aes192ecb.php Class Class source
  Plain text file Aes192gcm.php Class Class source
  Plain text file Aes192ocb.php Class Class source
  Plain text file Aes192ofb.php Class Class source
  Plain text file Aes256cbc.php Class Class source
  Plain text file Aes256cbchmacsha1.php Class Class source
  Plain text file Aes256cbchmacsha256.php Class Class source
  Plain text file Aes256ccm.php Class Class source
  Plain text file Aes256cfb.php Class Class source
  Plain text file Aes256cfb1.php Class Class source
  Plain text file Aes256cfb8.php Class Class source
  Plain text file Aes256ctr.php Class Class source
  Plain text file Aes256ecb.php Class Class source
  Plain text file Aes256gcm.php Class Class source
  Plain text file Aes256ocb.php Class Class source
  Plain text file Aes256ofb.php Class Class source
  Plain text file Aes256xts.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  ARIA  
File Role Description
  Plain text file Aria128cbc.php Class Class source
  Plain text file Aria128ccm.php Class Class source
  Plain text file Aria128cfb.php Class Class source
  Plain text file Aria128cfb1.php Class Class source
  Plain text file Aria128cfb8.php Class Class source
  Plain text file Aria128ctr.php Class Class source
  Plain text file Aria128ecb.php Class Class source
  Plain text file Aria128gcm.php Class Class source
  Plain text file Aria128ofb.php Class Class source
  Plain text file Aria192cbc.php Class Class source
  Plain text file Aria192ccm.php Class Class source
  Plain text file Aria192cfb.php Class Class source
  Plain text file Aria192cfb1.php Class Class source
  Plain text file Aria192cfb8.php Class Class source
  Plain text file Aria192ctr.php Class Class source
  Plain text file Aria192ecb.php Class Class source
  Plain text file Aria192gcm.php Class Class source
  Plain text file Aria192ofb.php Class Class source
  Plain text file Aria256cbc.php Class Class source
  Plain text file Aria256ccm.php Class Class source
  Plain text file Aria256cfb.php Class Class source
  Plain text file Aria256cfb1.php Class Class source
  Plain text file Aria256cfb8.php Class Class source
  Plain text file Aria256ctr.php Class Class source
  Plain text file Aria256ecb.php Class Class source
  Plain text file Aria256gcm.php Class Class source
  Plain text file Aria256ofb.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  BF  
File Role Description
  Plain text file Bfcbc.php Class Class source
  Plain text file Bfcfb.php Class Class source
  Plain text file Bfecb.php Class Class source
  Plain text file Bfofb.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  CAMELLIA  
File Role Description
  Plain text file Camellia128cbc.php Class Class source
  Plain text file Camellia128cfb.php Class Class source
  Plain text file Camellia128cfb1.php Class Class source
  Plain text file Camellia128cfb8.php Class Class source
  Plain text file Camellia128ctr.php Class Class source
  Plain text file Camellia128ecb.php Class Class source
  Plain text file Camellia128ofb.php Class Class source
  Plain text file Camellia192cbc.php Class Class source
  Plain text file Camellia192cfb.php Class Class source
  Plain text file Camellia192cfb1.php Class Class source
  Plain text file Camellia192cfb8.php Class Class source
  Plain text file Camellia192ctr.php Class Class source
  Plain text file Camellia192ecb.php Class Class source
  Plain text file Camellia192ofb.php Class Class source
  Plain text file Camellia256cbc.php Class Class source
  Plain text file Camellia256cfb.php Class Class source
  Plain text file Camellia256cfb1.php Class Class source
  Plain text file Camellia256cfb8.php Class Class source
  Plain text file Camellia256ctr.php Class Class source
  Plain text file Camellia256ecb.php Class Class source
  Plain text file Camellia256ofb.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  CAST5  
File Role Description
  Plain text file Cast5cbc.php Class Class source
  Plain text file Cast5cfb.php Class Class source
  Plain text file Cast5ecb.php Class Class source
  Plain text file Cast5ofb.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  CHACHA20  
File Role Description
  Plain text file Chacha20.php Class Class source
  Plain text file Chacha20poly1305.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  DES  
File Role Description
  Plain text file Descbc.php Class Class source
  Plain text file Descfb.php Class Class source
  Plain text file Descfb1.php Class Class source
  Plain text file Descfb8.php Class Class source
  Plain text file Desecb.php Class Class source
  Plain text file Desede.php Class Class source
  Plain text file Desede3.php Class Class source
  Plain text file Desede3cbc.php Class Class source
  Plain text file Desede3cfb.php Class Class source
  Plain text file Desede3cfb1.php Class Class source
  Plain text file Desede3cfb8.php Class Class source
  Plain text file Desede3ofb.php Class Class source
  Plain text file Desedecbc.php Class Class source
  Plain text file Desedecfb.php Class Class source
  Plain text file Desedeofb.php Class Class source
  Plain text file Desofb.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  DESX  
File Role Description
  Plain text file Desxcbc.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  ID  
File Role Description
  Plain text file Idaes128ccm.php Class Class source
  Plain text file Idaes128gcm.php Class Class source
  Accessible without login Plain text file Idaes128wrap.php.txt Doc. Documentation
  Accessible without login Plain text file Idaes128wrappad.php.txt Doc. Documentation
  Plain text file Idaes192ccm.php Class Class source
  Plain text file Idaes192gcm.php Class Class source
  Accessible without login Plain text file Idaes192wrap.php.txt Doc. Documentation
  Accessible without login Plain text file Idaes192wrappad.php.txt Doc. Documentation
  Plain text file Idaes256ccm.php Class Class source
  Plain text file Idaes256gcm.php Class Class source
  Accessible without login Plain text file Idaes256wrap.php.txt Doc. Documentation
  Accessible without login Plain text file Idaes256wrappad.php.txt Doc. Documentation
  Accessible without login Plain text file Idsmimealgcms3deswrap.php.txt Doc. Documentation

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  IDEA  
File Role Description
  Plain text file Ideacbc.php Class Class source
  Plain text file Ideacfb.php Class Class source
  Plain text file Ideaecb.php Class Class source
  Plain text file Ideaofb.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  RC2  
File Role Description
  Plain text file Rc240cbc.php Class Class source
  Plain text file Rc264cbc.php Class Class source
  Plain text file Rc2cbc.php Class Class source
  Plain text file Rc2cfb.php Class Class source
  Plain text file Rc2ecb.php Class Class source
  Plain text file Rc2ofb.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  RC4  
File Role Description
  Plain text file Rc4.php Class Class source
  Plain text file Rc440.php Class Class source
  Plain text file Rc4hmacmd5.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  SEED  
File Role Description
  Plain text file Seedcbc.php Class Class source
  Plain text file Seedcfb.php Class Class source
  Plain text file Seedecb.php Class Class source
  Plain text file Seedofb.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Cipher  /  SM4  
File Role Description
  Plain text file Sm4cbc.php Class Class source
  Plain text file Sm4cfb.php Class Class source
  Plain text file Sm4ctr.php Class Class source
  Plain text file Sm4ecb.php Class Class source
  Plain text file Sm4ofb.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Exceptions  
File Role Description
  Plain text file CipherNotImplementedException.php Class Class source
  Plain text file DecryptException.php Class Class source
  Plain text file EncryptException.php Class Class source
  Plain text file EncryptionException.php Class Class source
  Plain text file GenerateIvException.php Class Class source
  Plain text file InvalidCipherException.php Class Class source
  Plain text file InvalidVersionException.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Interfaces  
File Role Description
  Plain text file ICipher.php Class Class source
  Plain text file IEncryptAeadMode.php Class Class source
  Plain text file IEncryptWithInitializationVector.php Class Class source
  Plain text file IEncryptWithoutInitializationVector.php Class Class source
  Plain text file IGenerateInitializationVector.php Class Class source

  Files folder image Files  /  src  /  Encryption  /  Traits  
File Role Description
  Plain text file Decrypt.php Class Class source
  Plain text file DecryptAeadMode.php Class Class source
  Plain text file DecryptNoIV.php Class Class source
  Accessible without login Plain text file encryptWithoutPadding.php.txt Doc. Documentation
  Plain text file EncryptWithPadding.php Class Class source
  Plain text file EncryptWithPaddingAeadMode.php Class Class source
  Plain text file EncryptWithPaddingNoIV.php Class Class source
  Plain text file GenerateIv.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagefunctional (3 files)
Files folder imageunit (1 file, 2 directories)

  Files folder image Files  /  tests  /  functional  
File Role Description
  Plain text file PaddingWithIvAeadTest.php Class Class source
  Plain text file PaddingWithIvTest.php Class Class source
  Plain text file PaddingWithoutIvTest.php Class Class source

  Files folder image Files  /  tests  /  unit  
File Role Description
Files folder imageciphers (1 file)
Files folder imagetraits (7 files)
  Plain text file EncryptionTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  ciphers  
File Role Description
  Plain text file AbstractCipherTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  traits  
File Role Description
  Plain text file DecryptAeadModeTest.php Class Class source
  Plain text file DecryptNoIvTest.php Class Class source
  Plain text file DecryptTest.php Class Class source
  Plain text file EncryptWithPaddingAeadModeTest.php Class Class source
  Plain text file EncryptWithPaddingNoIVTest.php Class Class source
  Plain text file EncryptWithPaddingTest.php Class Class source
  Plain text file GenerateIvTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:354
This week:1
All time:6,962
This week:560Up