Take the 2-minute tour ×
Information Security Stack Exchange is a question and answer site for Information security professionals. It's 100% free, no registration required.

Which options are available in the default installation and which ones by third-parties like extensions or frameworks?

P.S. By Cryptography I mean algorithms which could be decrypted, not one-way encryption like md5.

share|improve this question
A bit of googling can do magic: "cryprographic function in php" First hit is php.net/crypto which seems to be exactly what you're looking for. As for "What's the strongest" - just go with something normal like AES or RC4. –  Luc Jan 1 '13 at 20:19
If you're talking about symmetric encryption probably AES-CBC with random IV together with HMAC-SHA2 in an encrypt-then-mac scheme. –  CodesInChaos Jan 1 '13 at 20:20
@Luc Using php's crypto function to build something strong isn't that simple, and almost all guides you'll find suck(as in, describe a weak scheme). –  CodesInChaos Jan 1 '13 at 20:21
@CodesInChaos If you simply encrypt something with AES I don't see how you could break it. Also, why the HMAC-SHA2? –  Luc Jan 1 '13 at 20:24
@Luc Because else you can probably use a padding oracle(or something similar) to convince the server to decrypt a message for you. Unauthenticated encryption is often vulnerable to active attacks. –  CodesInChaos Jan 1 '13 at 20:27

2 Answers 2

up vote 4 down vote accepted

As encryption primitive I'd use AES. Both AES-128 and AES-256 are strong enough. Numerically AES-256 is stronger, but it's very unlikely that AES-128 is the weakest point in your system.

Since php doesn't seem to support an authenticated encryption mode, you need to build that yourself. I'd use HMAC-SHA-256, possibly truncated to 128 bits.

To combine these operations I'd:

  1. Generate a new random IV with mcrypt_create_iv
  2. Encrypt the message with AES in CBC mode with that IV
  3. Concat the IV and the ciphertext
  4. Calculate HMAC-SHA-256 on the result of step 3 (mac must include both IV and padding)
  5. Concat the result of step 3 with the MAC

To decrypt:

  1. Verify the MAC in a way that's resistant to timing attacks
  2. Decrypt the message

This is an encrypt-then-mac scheme. It's recommended to use different keys for MAC and encryption. I recommend deriving both from a master key.

share|improve this answer
Thanks for the explanation, didn't test that yet but seems like a working solution! :) –  Mahdi Jan 3 '13 at 10:43

There are cryptographic standards, which are language independent, therefore I advice to use AES.


A php library that supports this is MYCRYPT. You should be using AES with a 128 or 256 bit block size (since you want the strongest possibility, use the 256) and you should also use OCB mode when possible (there are patents on it unforunately) and otherwise CBC. More info on modes can be found here.

share|improve this answer
The library is a standard php library that's in active use for over 8 years (so it's not something untested). I checked on Stackoverflow if it's common to show up in php crypto questions and it seems to be the most used. –  Lucas Kauffman Jan 2 '13 at 10:43
Thanks Lucas for the information! The stackoverflow link was awesome as well! –  Mahdi Jan 3 '13 at 10:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.