Descrição

This function opens the module of the algorithm and the mode to be used.
The name of the algorithm is specified in algorithm, e.g. "twofish" or is
one of the MCRYPT_ciphername constants. The module is closed by calling
mcrypt_module_close().

Parâmetros

The algorithm_directory parameter is used to locate
the encryption module. When you supply a directory name, it is used. When
you set it to an empty string (""), the value set by the
mcrypt.algorithms_dirphp.ini directive is used. When
it is not set, the default directory that is used is the one that was compiled
into libmcrypt (usually /usr/local/lib/libmcrypt).

The mode_directory parameter is used to locate
the encryption module. When you supply a directory name, it is used. When
you set it to an empty string (""), the value set by the
mcrypt.modes_dirphp.ini directive is used. When
it is not set, the default directory that is used is the one that was compiled-in
into libmcrypt (usually /usr/local/lib/libmcrypt).

Valor Retornado

Normally it returns an encryption descriptor, or FALSE on error.

Exemplos

The first line in the example above will try to open the DES cipher from
the default directory and the ECB mode from the directory
/usr/lib/mcrypt-modes. The second example uses
strings as name for the cipher and mode, this only works when the
extension is linked against libmcrypt 2.4.x or 2.5.x.

User Contributed Notes 7 notes

Also it should be pointed that md5() and/or sha1() should not be used while forming your key for the mcrypt. This is so because hex encoding uses a set of only 16 characters [0-9a-f], which is equivalent to 4 bits, and thus halve the strength of your encryption: 4 x 32 = 128-bit.

I have re-wrote the example shown, so here is my suggestion to get real 256-bit encryption:

<?php$key1 = "this is a secret key";$key2 = "this is the second secret key";$input = "Let us meet at 9 o'clock at the secret place.";$length = strlen($input);

about the previous comments on hex formatting and capitalization as a way to improve the key:

this would seem pretty obvious, but it is a choice to be limited to only hex characters ([0-9a-z]); you can get the original RAW output from md5() or sha1() and not the default readable hex formatting.

the result of a raw output will be 16 o 20 (depending on the hash function being used) series of chars in the range 0-255. way better than [0-9a-z] and even [0-9a-zA-Z].

16 or 20 is generally lower than the maximum key lenght ($ks in the example), but you can append two or more keys together:

// then just use it as you would (extract taken from the example)$key = substr($bigger_binary_key, 0, $ks);?>

... or you can automatically split one large "human key" into two or more parts, hash those parts with sha1 (raw output!) and merge them together again (in original order or rearrange, salt, transform them as you like) to get a binary key of 40, 60, 80 or more chars depending on the number of parts the secret key has been splitted =)

Doing a trim($decrypted) will remove the null padding that may occur as a result of decryption.

The problem is if you're encrypting something like a MSWord document which can commonly end with nulls. The result $decrypted will be smaller than the original cleartext - which will then fail to open in MSOffice.

To get around this, make sure you store the length of the original cleartext, and when you decrypt it, do: