PHP Simple Encryption and Decryption: Encrypt and decrypt data

Recommend this page to a friend!
  Info   View files Documentation   View files View files (187)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2020-09-30 (25 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 219 This week: 3All time: 8,123 This week: 47Up
Version License PHP version Categories
phpsimple-encryption 1.0.3The 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: 5 packages by
Country: United States United States
Age: 47
All time rank: 3226405 in United States United States
Week rank: 52 Up3 in United States United States Up
Innovation award
Innovation award
Nominee: 1x

Details

Latest Stable Version Total Downloads PHP Composer 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) {
    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 (\RuntimeException).

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 = $encryptuion->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: 132 Default cipher: AES-256-CBC (version 1)

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

Notes

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

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 (4 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 (32 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 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
  Accessible without login Plain text file Aes128ocb.php.txt Doc. Documentation
  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
  Accessible without login Plain text file Aes192ocb.php.txt Doc. Documentation
  Plain text file Aes192ofb.php Class Class source
  Plain text file Aes256cbc.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
  Accessible without login Plain text file Aes256ocb.php.txt Doc. Documentation
  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 EncryptionException.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:219
This week:3
All time:8,123
This week:47Up

For more information send a message to info at phpclasses dot org.