vmelnik-ukraine / DoctrineEncryptBundle

Bundle allows you to create doctrine entities with fields that will be protected by encryption algorithms such as AES
MIT License
71 stars 227 forks source link

This is not AES #13

Open binarious opened 11 years ago

binarious commented 11 years ago

I'm having problems with this part:

mcrypt_encrypt(
    MCRYPT_RIJNDAEL_256, 
    $this->secretKey, 
    $data, 
    MCRYPT_MODE_ECB,     
    mcrypt_create_iv(
        mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB),
        MCRYPT_RAND
    )
)

MCRYPT_RIJNDAEL_256 is not AES (http://stackoverflow.com/questions/4537099/problem-with-aes-256-between-java-and-php/4539318#4539318).

The 256 in that constant refers to the blocksize, not the keysize. Use MCRYPT_RIJNDAEL_128 to get the same algorithm as AES. The keysize is set just by the number of bytes in the key argument you supply. So supply 32 bytes and you get AES with a 256-bit key.

MCRYPT_MODE_ECB doesn't use an IV (http://stackoverflow.com/questions/1789709/is-it-possible-to-use-aes-with-an-iv-in-ecb-mode). So why are you setting one?

westinpigott commented 11 years ago

I believe this is addressed by #18. It is switched to CBC mode and is using a sha256 hash on the "secret key" as the encryption key. Thus makes use of MCRYPT_RIJNDAEL_128 and uses a 256-bit key.

ezimuel commented 8 years ago

This is not AES and the ECB is not considered secure. You should use CBC mode and MCRYPT_RIJNDAEL_128 using 256-bit key, as suggested by @westinpigott.
Moreover, you cannot have only encryption, you need also authentication for security reason (for instance, using HMAC SHA-256). See this slides for more information: http://www.zimuel.it/slides/midwestphp2016/encryption You can see how I implemented it in this project: https://github.com/ezimuel/phpcrypto/blob/master/src/Symmetric.php Finally, the MCrypt extension is going to be deprecated from PHP 7.1, I suggest to use OpenSSL instead.