MCRYPT_MODE_ECB means you are using ECB, block encryption mode. Block ciphers can be processed for either block cipher mode or stream encryption modes. The common block encryption modes are ECB and CBC, the common stream encryption mode is CTR, better known as the counter mode.
MCRYPT_RIJNDAEL_128 is an implementation of AES. AES is a Rijndael cipher with a block size of 128 bits and three possible key sizes, 128, 192 and 256 bits. Therefore, if you use block cipher encryption mode, then you need to split up plain text of 128 bits - 16 bytes each. Of course, this leaves you with the question of what to do if the last block is not 16 bytes.
PHP mcrypt_encrypt more or less leaves this to the user. It fills with 00 if the block is not filled to the size of the block. This is normal if the input is a string; you can just trim from characters 00 from the returned string. If the input data, however, the binary data ends with the character 00 , than this character is lost (+ any other character that is taken from the beginning and end of the line, of course). You can also send the length of the string encrypted along with the plaintext, of course.
For a better scheme, you only need to see the PKCS # 7 add-on. Several code snippets for implementing the add-on can be found in the mcrypt_encrypt comments mcrypt_encrypt .
mcrypt_encrypt does not currently seem to support streaming modes for AES, so the option is disabled if you want to keep the PHP mcrypt library.
source share