Behaviour change: Since 5.2.x mcrypt_generic will issue a warning when the datastring is empty.
![]() |
|
||||||||||
|
mcrypt_genericDescriptionstring mcrypt_generic ( resource td, string data )This function encrypts data. The data is padded with "\0" to make sure the length of the data is n * blocksize. This function returns the encrypted data. Note that the length of the returned string can in fact be longer then the input, due to the padding of the data. If you want to store the encrypted data in a database make sure to store the entire string as returned by mcrypt_generic, or the string will not entirely decrypt properly. If your original string is 10 characters long and the block size is 8 (use mcrypt_enc_get_block_size() to determine the blocksize), you would need at least 16 characters in your database field. Note the string returned by mdecrypt_generic() will be 16 characters as well...use rtrim()($str, "\0") to remove the padding. If you are for example storing the data in a MySQL database remember that varchar fields automatically have trailing spaces removed during insertion. As encrypted data can end in a space (ASCII 32), the data will be damaged by this removal. Store data in a tinyblob/tinytext (or larger) field instead. The encryption handle should always be initialized with mcrypt_generic_init() with a key and an IV before calling this function. Where the encryption is done, you should free the encryption buffers by calling mcrypt_generic_deinit(). See mcrypt_module_open() for an example. See also mdecrypt_generic(), mcrypt_generic_init(), and mcrypt_generic_deinit(). mcrypt_generic
maxximus007 at gmail dot com
16-Aug-2007 08:21
Behaviour change: Since 5.2.x mcrypt_generic will issue a warning when the datastring is empty.
tmacedo at linux dot ime dot usp dot br
13-Nov-2006 09:22
completing the post from Ryan Thomas, ryanrst at gmail dot com, if u post a cookie w/ HTTP method, its may be encoded;
chad 0x40 herballure 0x2e com
12-Jul-2006 08:28
Addendum to my previous note: apparently there was some sort of character encoding breakage; PHP does not pad if no padding is needed, and the extra padding I saw was the result of chr(X) returning multiple bytes or something.
chad 0x40 herballure 0x2e com
12-Jul-2006 07:36
If the data is already n*blocksize long, PHP pads with another full block of "\0", so there will be between 1 and mcrypt_enc_get_block_size($td) bytes of padding.
eric at ez-llc dot com
11-Mar-2006 12:55
I was able get php and perl to play together with blowfish using cipher block chaining. The blowfish key needs to be atleast 8 chars (even though blowfish min is 8 bits, perl didn't like keys smaller than 8 chars) and max 56. The iv must be exactly 8 chars and padding needs to be null because php pads with nulls. Also, php needs libmcrypt >= 2.4.9 to be compatible with perl.
Ryan Thomas, ryanrst at gmail dot com
05-Jan-2006 02:15
If you wish to store encrypted data in a cookie variable on the browser you will encounter problems when decrypting the data. This is because cookies will only store US-ASCII characters and your encrypted data may contain non-US-ASCII characters.
pauls at sellingsource dot com
15-May-2003 11:02
If you are encrypting binary and there is a null terminator partway through your encryption, you will loose the rest of the string. A workaround is to base64_encode your binary string first. |