mcrypt_generic This function encrypts data


mcrypt_generic

(PHP 4 >= 4.0.2, PHP 5)

mcrypt_genericThis function encrypts data

说明

string mcrypt_generic ( resource $td , string $data )

This function encrypts data. The data is padded with "" 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 than 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, "") 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.

参数

td

The encryption descriptor.

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.

data

The data to encrypt.

返回值

Returns the encrypted data.

参见


«  mcrypt_enc_self_test
» mcrypt_generic_deinit
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3