Simple Encryption in PHP.

Overview

php-encryption

Build Status codecov Latest Stable Version License Downloads

composer require defuse/php-encryption

This is a library for encrypting data with a key or password in PHP. It requires PHP 5.6 or newer and OpenSSL 1.0.1 or newer. We recommend using a version of PHP that still has security support, which at the time of writing means PHP 7.3 or later. Using this library with an unsupported version of PHP could lead to security vulnerabilities.

The current version of php-encryption is v2.3.1. This library is expected to remain stable and supported by its authors with security and bugfixes until at least January 1st, 2022.

The library is a joint effort between Taylor Hornby and Scott Arciszewski as well as numerous open-source contributors.

What separates this library from other PHP encryption libraries is, firstly, that it is secure. The authors used to encounter insecure PHP encryption code on a daily basis, so they created this library to bring more security to the ecosystem. Secondly, this library is "difficult to misuse." Like libsodium, its API is designed to be easy to use in a secure way and hard to use in an insecure way.

Dependencies

This library requires no special dependencies except for PHP 5.6 or newer with the OpenSSL extensions (version 1.0.1 or later) enabled (this is the default). It uses random_compat, which is bundled in with this library so that your users will not need to follow any special installation steps.

Getting Started

Start with the Tutorial. You can find instructions for obtaining this library's code securely in the Installing and Verifying documentation.

After you've read the tutorial and got the code, refer to the formal documentation for each of the classes this library provides:

If you encounter difficulties, see the FAQ answers. The fixes to the most commonly-reported problems are explained there.

If you're a cryptographer and want to understand the nitty-gritty details of how this library works, look at the Cryptography Details documentation.

If you're interested in contributing to this library, see the Internal Developer Documentation.

Other Language Support

This library is intended for server-side PHP software that needs to encrypt data at rest. If you are building software that needs to encrypt client-side, or building a system that requires cross-platform encryption/decryption support, we strongly recommend using libsodium instead.

Examples

If the documentation is not enough for you to understand how to use this library, then you can look at an example project that uses this library:

Security Audit Status

This code has not been subjected to a formal, paid, security audit. However, it has received lots of review from members of the PHP security community, and the authors are experienced with cryptography. In all likelihood, you are safer using this library than almost any other encryption library for PHP.

If you use this library as a part of your business and would like to help fund a formal audit, please contact Taylor Hornby.

Public Keys

The GnuPG public key used to sign current and older releases is available in dist/signingkey.asc. Its fingerprint is:

2FA6 1D8D 99B9 2658 6BAC  3D53 385E E055 A129 1538

You can verify it against Taylor Hornby's contact page and twitter.

Due to the old key expiring, new releases will be signed with a new public key available in dist/signingkey-new.asc. Its fingerprint is:

6DD6 E677 0281 5846 FC85  25A3 DD2E 507F 7BDB 1669

A signature of this new key by the old key is available in dist/signingkey-new.asc.sig.

Comments
  • Making it easier for people to use...

    Making it easier for people to use...

    @defuse

    I came across your library in some of those slim threads and thought it might be great to use, as I've currently been investigating encryption libraries to finish my security module.

    That said, two things I noticed immediately:

    1. It lacks composer support
    2. It takes over the exception handler (I understand this is for security)

    I'd like to fix these two problems and make use of your library, but based on the previous composer.json pull request that is open, I'd like to verify that you're interested first.

    With respect to the exception handler, it would make more sense, if the exception handler was changed temporarily during the processes which could throw exceptions. Your exception handler could be split to a separate class, and http://php.net/manual/en/function.restore-exception-handler.php can be used to restore the previous one once decrypting is done (for example).

    High Priority 
    opened by mattsah 58
  • Derive Key From Password

    Derive Key From Password

    Add a function for deriving a key from a password with PBKDF2.

    We have to think really hard about how to make this usable, since we want to make sure they're doing the salt right (by making it hard to do it wrong).

    enhancement v2.0 
    opened by defuse 34
  • Unsupported cipher method aes-256-ctr

    Unsupported cipher method aes-256-ctr

    At the moment i try to fix an issue on my server:

    Cipher method not supported. This is normally caused by an outdated version of OpenSSL (and/or OpenSSL compiled for FIPS compliance). Please upgrade to a newer version of OpenSSL that supports aes-256-ctr to use this library.

    After some searching i found two constants in the Core class: CIPHER_METHOD and LEGACY_CIPHER_METHOD. Is it possible to set the cipher method from outside without manipulating the original code? Did not find any possibility to do so.

    I'm using php7.2 on different OS (Ubuntu, Alpine Linux and Windows 10) with v2.2.1 of the defuse/php-encryption package, and LibreSSL 2.7.4

    opened by markus-hsk 32
  • Perform the v2.0.0 release ceremony

    Perform the v2.0.0 release ceremony

    I sign the git tags, but we need GPG signatures that people actually know how to verify. For now, I'll use my email address key, and then we'll switch to my air-gapped key in the future once I get it set up correctly.

    v2.0 
    opened by defuse 30
  • Help with saveToAsciiSafeString

    Help with saveToAsciiSafeString

    If there is a better place to ask questions regarding this library please let me know. When I attempt to save the randomly generated key using Key::saveToAsciiSafeString Intellij alerts me that "Non-static method 'saveToAsciiSafeString should not be called statically"

    Can someone please provide proper syntax or ideally an example of how to create a random key one time then store in a format that can be saved as a variable so that I can call that to decrypt stored data in the future. I understand that this may not be the safest of all possible scenarios but aside from storing a key generated with this library I am left with mcrypt. Thanks!

    support 
    opened by SkynetHome 29
  • Use of statics prohibits testing/injection/mocking capabilities

    Use of statics prohibits testing/injection/mocking capabilities

    This library uses all static methods, and while some of them make sense, its really too bad that the library's Crypto class can't be used as an injectable instance. Currently, testing services that make use of this library means running expensive crypto hashing algorithms that slow down a unit testing feedback loop.

    Switching to non-statics would also allow for certain configurations to be used, if certain aspects needed to be overrided (I know, likely not suggested), for example providing a different hash function but defaulting to the current constant sha256.

    Thank you for this great library and for considering this issue.

    v2.1 
    opened by Rican7 22
  • PBKDF2 polyfill vulnerable to

    PBKDF2 polyfill vulnerable to "long password DoS"

    The PBKDF2 polyfill in Core.php (which should run only for PHP 5.4 users) doesn't pre-hash the password before PBKDF2 inner loop if the password's length is greater than the underlying hash algorithm's output length.

    This could lead to DoS situation if a malicious user is able to input long passwords to the PBKDF2 function. I.e. on my machine, 100000 byte password and 25000 iterations (with SHA256 and 32 byte output) took ~16 seconds.

    I suggest we add a conditional pre-hash step before the outer PBKDF2 loop to mitigate this issue: check the password lenght, and if it is greater than $hash_length, do a pre-hash.

    Roughly something like:

    if (Core::ourStrlen($password) > $hash_length) {
        $password = \hash($algorithm, $password, true);
    }
    
    SECURITY v2.0 review needed 
    opened by timoh6 19
  • I think something is wrong with the v2.0 branch

    I think something is wrong with the v2.0 branch

    In all the pull requests I'm seeing comments I left to review some of scott's commits... so the thing people are basing their work off of must be newer than our current v2.0 branch and I'm very confused.

    opened by defuse 17
  • Write a blog post about our API and the challenges we faced

    Write a blog post about our API and the challenges we faced

    Maybe we should write a paper, similar to the NaCl paper, about our API. We found a way to make file encryption usable so I think it's worth it. It would document our design choices and perhaps make a good argument for libsodium to support the same API for encrypting/decrypting files.

    promotion / community 
    opened by defuse 16
  • Code cleanup pass

    Code cleanup pass

    Look over all the code, check for:

    • Unreachable code (e.g. functions that are never called).
    • Files without trailing new lines.
    • Tabs that should be spaces.
    • Consistent capitalization (e.g. some static methods are CamelCase some are whateverThisIsCalled)
    • Comments are correct (and all the methods that need documentation have doc comments).
    v2.0 review needed 
    opened by defuse 16
  • Cannot safely perform encryption

    Cannot safely perform encryption

    I am not using SSL is this required? I am using windows 10, PHPv7,fast cgi. I am runing the example.php and this is the report.

    HTML Error: Cannot safely perform encryption

    Exception: Defuse\Crypto\Exception\CannotPerformOperationException Object ( [message:protected] => [string:Exception:private] => [code:protected] => 0 [file:protected] => php-encryption-master\src\Crypto.php [line:protected] => 618 [trace:Exception:private] => Array ( [0] => Array ( [file] => php-encryption-master\src\Crypto.php [line] => 221 [function] => ensureFunctionExists [class] => Defuse\Crypto\Crypto [type] => :: [args] => Array ( [0] => openssl_get_cipher_methods ) ) [1] => Array ( [file] => php-encryption-master\src\Crypto.php [line] => 65 [function] => runtimeTest [class] => Defuse\Crypto\Crypto [type] => :: [args] => Array ( ) ) [2] => Array ( [file] => php-encryption-master\example.php [line] => 9 [function] => createNewRandomKey [class] => Defuse\Crypto\Crypto [type] => :: [args] => Array ( ) ) ) [previous:Exception:private] => ) Notice: Undefined variable: key in php-encryption-master\example.php on line 20

    support 
    opened by page3131 16
  • New release with the fix for PHP 8 support

    New release with the fix for PHP 8 support

    Now that the fix is in place for the PHP8 - is it possible to get a new release published so that we can grab it from composer? Right now the latest release is from April 2021. Thanks

    opened by slawn 0
  • Decrypt data when user change password

    Decrypt data when user change password

    Hi, I'm trying to use the userPassword scenario and I found confusion when the user change the password after the initial key was saved. is there any suggestion for unlocking the saved key when the user changes the password? is there any method to update the key and decrypt saved encrypted data?

    opened by suwidadi 1
  • Recommendations in choosing a password

    Recommendations in choosing a password

    What are the recommendations in choosing a password for Crypto::encryptWithPassword(), which will eliminate the expediency of brute force when compromising an encrypted file? Suppose the password consists of a set of printed characters of ASCII. What will be the safe minimum length?

    opened by PhantomArt 0
  • ChangePassword method fail?

    ChangePassword method fail?

    The change password method breaks the process, even with a try catch block, but creating random password and unlocking are fine.

    $a = KeyProtectedByPassword::loadFromAsciiSafeString($userdata['protectedKey']); //ok $a->changePassword( $password, $_REQUEST['newPassword'] ); echo $a->saveToAsciiSafeString(); //fail

    However unlocking is fine ... $a = KeyProtectedByPassword::loadFromAsciiSafeString($userdata['protectedKey']); //ok $a->unlockKey($_REQUEST['oldPassword']); //ok echo $a->saveToAsciiSafeString(); //ok

    opened by RustieNewton 1
Releases(v2.3.1)
Owner
Taylor Hornby
Taylor Hornby
Simple Encryption in PHP.

php-encryption composer require defuse/php-encryption This is a library for encrypting data with a key or password in PHP. It requires PHP 5.6 or new

Taylor Hornby 3.6k Jan 3, 2023
A cryptography API wrapping the Sodium library, providing a simple object interface for symmetrical and asymmetrical encryption, decryption, digital signing and message authentication.

PHP Encryption A cryptography API wrapping the Sodium library, providing a simple object interface for symmetrical and asymmetrical encryption, decryp

null 19 Dec 31, 2022
A petite library of encryption functions for PHP

?? dcrypt A petite library of essential encryption functions for PHP 7.1+. For legacy PHP version support, look here. If you need a dcrypt inspired en

null 96 Oct 6, 2022
JSON Object Signing and Encryption library for PHP.

NAMSHI | JOSE Deprecation notice Hi there, as much as we'd like to be able to work on all of the OSS in the world, we don't actively use this library

Namshi 1.7k Dec 22, 2022
AES 128 bit Encryption and Decryption algorithm excuted purely on PHP with no external libraries.

AES128 Executed with PHP Advanced Encryption Standard (AES) is a specification for the encryption of electronic data established by the U.S National I

Ahmed Mohamed Mostafa 2 Aug 8, 2022
Encryption-free Private Messaging For Flarum

Whisper - Private Messaging for Flarum A Flarum extension. Add private messaging functionality to your Flarum Community! Simple to install, no setting

Charlie 4 Dec 7, 2021
Pretty Good Privacy (PGP) is an encryption program that provides cryptographic privacy and authentication for data communication.

Pretty Good Privacy (PGP) is an encryption program that provides cryptographic privacy and authentication for data communication. PGP is used for signing, encrypting, and decrypting texts, e-mails, files, directories, and whole disk partitions and to increase the security of e-mail communications. Phil Zimmermann developed PGP in 1991.

[sCRiPTz-TEAM] 3 Dec 31, 2021
Password manager featuring client-side encryption, vaults, folders and more.

vaults is a password manager featuring client side AES-256 encryption, PBKDF2 hashing, vaults, password generation & more. Features Technical overview

null 27 Nov 18, 2022
Simplest implementation of RSA algorithm encryption and decryption

Simplest RSA (Rivest–Shamir–Adleman) Simplest implementation of RSA algorithm encryption and decryption. Richard Feynman: What I cannot create, I do n

Max Base 8 Aug 30, 2022
PHPIDS (PHP-Intrusion Detection System) is a simple to use, well structured, fast and state-of-the-art security layer for your PHP based web application

PHPIDS PHPIDS (PHP-Intrusion Detection System) is a simple to use, well structured, fast and state-of-the-art security layer for your PHP based web ap

null 752 Jan 3, 2023
TCrypto is a simple and flexible PHP 5.3+ in-memory key-value storage library

About TCrypto is a simple and flexible PHP 5.3+ in-memory key-value storage library. By default, a cookie will be used as a storage backend. TCrypto h

timoh 57 Dec 2, 2022
A simple PHP web backdoor allows you to retrieve directory/file contents and upload file(s) from the local machine or remote URL.

Simple PHP Web Backdoor A simple PHP web backdoor allows you to retrieve directory/file contents and upload file(s) from the local machine or remote U

Aqhmal Hafizi 15 Oct 7, 2022
encrypt HMAC SHA1 with simple PHP

HMAC Tugas UTS SKJ 1 MALAM Nama : MOCHAMMAD SIDIQ C.A. NIM : 1814321019 Create Hmac Buka terminal powershell pada visual studioCode (dahal hal ini

Shidiq Mochsi 2 Jan 10, 2022
A simple, yet feature rich password manager for Nextcloud

Easy to use yet feature-rich and secure password manager for Nextcloud

M. Wieschollek 169 Jan 3, 2023
A Simple Cross Origin Resource Sharing for Lumen Framework (5.*).

Lumen Cors Package A Simple Cross Origin Resource Sharing for Lumen Framework. Note: That should works fine on Laravel Framework too, but the tests ar

Vagner Luz do Carmo 46 Jul 27, 2022
Security advisories as a simple composer exclusion list, updated daily

Roave Security Advisories This package ensures that your application doesn't have installed dependencies with known security vulnerabilities. Installa

Roave, LLC 2.5k Jan 5, 2023
php-chmod is a PHP library for easily changing permissions recursively.

PHP chmod php-chmod is a PHP library for easily changing the permissions recursively. Versions & Dependencies Version PHP Documentation ^1.1 ^7.4 curr

Mathias Reker ⚡️ 5 Oct 7, 2022
PHP 5.x support for random_bytes() and random_int()

random_compat PHP 5.x polyfill for random_bytes() and random_int() created and maintained by Paragon Initiative Enterprises. Although this library sho

Paragon Initiative Enterprises 8k Jan 5, 2023
PHP Secure Communications Library

phpseclib - PHP Secure Communications Library Supporting phpseclib Become a backer or sponsor on Patreon One-time donation via PayPal or crypto-curren

null 4.9k Jan 7, 2023