Giter Club home page Giter Club logo

php-encryption's Introduction

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 8.0 or later. Using this library with an unsupported version of PHP could lead to security vulnerabilities.

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

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 the current and new releases is available in dist/signingkey-new.asc. Its fingerprint is:

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

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

Older releases were signed with a (now-expired) available in dist/signingkey-old.asc. The old key's fingerprint is:

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

The old key's fingerprint can be verified against Taylor Hornby's contact page and twitter.

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

php-encryption's People

Contributors

aleeeftw avatar artfulrobot avatar chriseskow avatar codermarcel avatar darkheir avatar defuse avatar exussum12 avatar glensc avatar hansott avatar larowlan avatar laurentj avatar lewiscowles1986 avatar malaynayak avatar mbardelmeijer avatar mesour avatar narfbg avatar nyholm avatar paragonie-scott avatar paragonie-security avatar qasim-at-tci avatar reedy avatar robstoll avatar samnela avatar sarciszewski avatar sethbattin avatar spaze avatar sstok avatar theodorejb avatar tsusanka avatar twistor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-encryption's Issues

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.

Runtime Self-Test

We should develop a quick self-test that will have to run once (and pass) before it will encrypt or decrypt anything.

Is it hard to misuse?

We want this code to be as hard to misuse as possible. We should consider giving it a property like:

  • Encrypt() and Decrypt() will only accept a key generated with CreateNewRandomKey()

Safe to change static encryption keys?

Hello,

TLDR: is it safe to change the two constants

const ENCRYPTION_INFO = 'DefusePHP|KeyForEncryption';
const AUTHENTICATION_INFO = 'DefusePHP|KeyForAuthentication';

I am concerned about a situation where an attacker has compromised the database, and therefore has access to the encrypted data and ivs. In that situation, if they know the secret used for encryption they can decrypt the data. I would rather not use a hard coded secret that is constant across all copies of this library.

I see a giant comment saying not to change any constants, however these seem different then the others in that they don't change the workings of the encryption at all.

If it is unsafe to change these, how do you recommend addressing my issue above? Am I just applying the library wrong?

Thanks

Encrypt in PHP, decrypt in nodejs and vice versa

Hi,

I would like to ask a stupid question: I would like to send a crypted message between nodejs and php applications and I would love to use this lib. However, I'm unsure as how I should decrypt the encrypted message in nodejs and vice versa. As I'm a total beginner in this, any pointer would be extremely appreciated.

Security Audit

Get someone else to look at this code and find all the problems with it.

Real Life Example

Make a real life example, showing, most importantly, how error handling should be done.

Class Constants

There is no need for the constants to be global. They should be class constants. Figure out what versions of PHP class constants are supported on.

Searching on Encrypted Database

I've googled and found theories and CryptDB.

The most sensible approach I found is by indexing keywords and connecting them to the encrypted records, so the searching is actually getting data from those index db, without decrypting the records.

I'd like to hear some thought of the practical way of doing this, or not.
Especially, because this repo is about (en|de)cryption and followed by implementers.

This can be a challenging topic for Documentation.

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).

Publicity / Advocacy

We need to start some sort of campaign to get people using this library instead of either rolling their own or using an existing broken one.

Before we do this, though, we need to:

  1. Get Crypto.php on Composer - #44. [DONE]
  2. Get Crypto.php on PEAR - #45.
  3. Release versioned downloadable tarballs (possibly through github) - #46.
  4. Cryptographically sign releases - #47.
  5. Version tagging - #34.

Constitution

Answer these questions:

  • Why does this library exist?
  • What's special about this library?
  • What are the "core values" of this library?

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).

Key Leaked in Stack Trace

If the HMAC is invalid, PHP will dump out a stack trace with the key in it. This is obviously not good, because it means an attacker who can corrupt the ciphertext can learn the key.

#0 /[redacted]/php-encryption/Crypto.php(275): Crypto::Decrypt('??+6?G"?????tk?...', '?9M???????l?.j;...')

Basic usage example ?

I've dowloaded the .zip and installed it (one week ago).
My problem is I don't find the way to use it and I don't find any example on internet.

Could you please add a file to the .zip with an example ?

User Documentation

At the top of the code should be some really good documentation for the user. We shouldn't just tell them how to use the code, we should tell them what this code is good for, and what it isn't (e.g. network communication protocol).

Encoding

We need to provide encoding and decoding functions (hex, base64, etc) that are safe against side channel attacks.

Error check returns exception instead of throws

The error check on line 375 returns the exception as a function call, instead of throwing it. This would fatal error (non-existent function) if the branch was ever reached.

Instead, change

return CannotPerformOperationException();

To

throw new CannotPerformOperationException();

Best way to store the cypher key

What is the best way to store the key used to encrypt data?

I used base64_encode/decode my cypher key and then save it to a file, but this is not advised according to the crypto class comments.

Thus, I am asking what are or is the best way to store a cypher key to avoid leaks and attacks.

thanks.

Namespacing

Include the crypto class and exception classes inside a namespace so we're less likely to conflict with other code.

Best Mysql field type to store encrypted data

Hello!
I am wondering about what is the best type i should choose for the field i use to store the encrypted data, i chose varchar but encrypted data seems to be corrupted after it is stored in mysql. i Planing to use a blob type, is this a good choice and what size should choose also. I planning to encrypt integers only which should not exceed 8 or 9 digits,
my Mysql database uses innoDB engine with utf8_general_ci

thanks

Function error

Hi there,
Firstly, I would like to thank you for the hard work. I'm using this library to do encryption and decryption for my education purposes but unfortunately I'm having trouble with the decrypt part which it keeps showing me this error message: 'DANGER! DANGER! The ciphertext has been tampered with!' . I did not edit anything from the how to use this code example code except the $message which I wish to encode and the ciphertext and key has been saved to the database for future use. It seems like the how to use this code example couldn't use separately; I have verified the output of the ciphertext and key after retrieving from database and finds that the output is indeed match. Below are some of my sample code of using your library:
//...encrypting part...
$content = trim($_POST['content']);

try {
$key = Crypto::CreateNewRandomKey();
// WARNING: Do NOT encode $key with bin2hex() or base64_encode(),
// they may leak the key to the attacker through side channels.
} catch (CryptoTestFailedException $ex) {
die('Cannot safely create a key');
} catch (CannotPerformOperationException $ex) {
die('Cannot safely create a key');
}

$message = $content; //text which grab from end user input
try {
$ciphertext = Crypto::Encrypt($message, $key);
//print $message;
//print $ciphertext;
} catch (CryptoTestFailedException $ex) {
die('Cannot safely perform encryption');
} catch (CannotPerformOperationException $ex) {
die('Cannot safely perform decryption');
}

//...decrypting part...
$query = "SELECT content,seckey FROM contents WHERE site = :site";
$query_params = array(':site' => $site);

try{
global $db;
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}catch(PDOException $ex){
die("Failed to run query: " . $ex->getMessage());
}

$row = $stmt->fetch();

if($row){
$ciphertext = $row['content']; //select the ciphertext which saved earlier
$key = $row['seckey']; //select the secretkey which saved earlier
//print $ciphertext;
//print $key;

try {
$decrypted = Crypto::Decrypt($ciphertext, $key);
} catch (InvalidCiphertextException $ex) { // VERY IMPORTANT
// Either:
// 1. The ciphertext was modified by the attacker,
// 2. The key is wrong, or
// 3. $ciphertext is not a valid ciphertext or was corrupted.
// Assume the worst.
die('DANGER! DANGER! The ciphertext has been tampered with!');
} catch (CryptoTestFailedException $ex) {
die('Cannot safely perform encryption');
} catch (CannotPerformOperationException $ex) {
die('Cannot safely perform decryption');
}

$text = preg_replace("/[\r\n]+/","

",$decrypted);
}

More HKDF Test Vectors

Add the rest of the HKDF test vectors. We have 1, 2, 3, and 7. We're missing 4, 5, and 6.

The test vectors are in The RFC

Version Tagging

This is a nice-to-have in case anyone wants to encrypt with version 1.x of this library in 2015 then decrypt it with version 3.x in 2025.

Multibyte strings

Do they affect us? There are $string[$index] stuff going on in this code, so maybe it could?

BSD License

The 2-clause BSD license would be better for this code than public domain, because of the issues of "public domain" not being defined in some countries, etc. Unfortunately I released the original code as "public domain." Is it too late to change it?

Safe Failure Tests on 5.2 and 5.3.

Switching to OpenSSL broke 5.2 and 5.3. We don't want to support those versions any more, but we should at least be sure we're not dangerous on those versions. Add travis tests for those versions that make sure they fail somehow safe (e.g. by throwing an exception).

#36

Documentation

The documentation shouldn't be a massive comment at the top of the file (that nobody will read anyway).

Decryption fails with a lot of data.

I am trying to Encrypt/Decrypt large amounts of data. I am encrypting about 3.56MB of text, and when I try to decrypt it, it fails on line 232 - throw new InvalidCiphertextException();

My end goal is to be able to encrypt very large files perhaps up to 2gb, but at this point I'm stuck at decrypting 3.56MB.

Portable Ciphertexts

Should we encode the cipher and hash function into the ciphertext, so that it can be decrypted even after the algorithms have changed?

Storing & Retrieving Encrypted Data

Hello this is my first time posting on Github so excuse me if i format the issue wrong!

Using the "example.php" provided with the current version i added the following code/statements;

<Generate keys & encrypt the text>

$encode = base64_encode($ciphertext);

$stmt = "INSERT INTO test (test) VALUES (:test)";

$y = $pdo->prepare($stmt);
$y->execute(array(
':test'=> $encode
));

$sth = $pdo->prepare("SELECT test from test where id = '1' ");
$sth->execute();
$result = $sth->fetch();
$data = $result['test'];

$decode = base64_decode($data);

This returns;

"DANGER! DANGER! The ciphertext has been tampered with!"

This could be just me being new to using encryption with PHP or it could be an issue with the code itself but i was told i should bring it up here;

"http://stackoverflow.com/questions/30226517/encrypting-data-using-the-defuse-php-encryption?noredirect=1#comment48595894_30226517"

Thanks again.

Find Copies of HKDF Bug

#19 reports a flaw in the HKDF implementation. I'm pretty sure I copied that code from another project, or re-used that code in another project. Which project? Find all the other ones and fix them.

Support API for Encrypting/Decrypting files.

You can't encrypt super-large files with this library because you have to load them into RAM. We could either (1) Have a "stream" sort of API, where you feed it data and it feeds back ciphertext, but there are problems with this, e.g. when decrypting you'll get all of the plaintext and then only the last call will check the MAC, but the damage has already been done, or (2) Support file encryption/decryption only, so that we can encapsulate all of that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.