/** * PKCS padding works by adding n padding bytes of value n to make the total * length of the data a multiple of the block size. Padding is always added * so if the data is already a multiple of the block size n will equal the * block size. For example if the block size is 8 and 11 bytes are to be * encrypted then 5 padding bytes of value 5 will be added. * * @param $text, The text that you want to encrypt * @param $blocksize, The block size of you algorithm e.g. 128 / 8 = 16 */ function pkcs5_pad ($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr(11), $pad); } /** * Encryption * * @param $text, The text that you want to encrypt. * @param $crypt = 1 if you want to crypt, or 0 if you want to decrypt. */ function bakery_mix($text, $crypt) { $key = variable_get('bakery_key', ''); $td = mcrypt_module_open('rijndael-128', '', 'ecb', ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); $key = substr($key, 0, mcrypt_enc_get_key_size($td)); mcrypt_generic_init($td, $key, $iv); if($crypt) { $encrypted_data = mcrypt_generic($td, pkcs5_pad($text)); } else { $encrypted_data = mdecrypt_generic($td, trim($text)); } mcrypt_generic_deinit($td); mcrypt_module_close($td); return $encrypted_data; }