note 62846 deleted from ref.mcrypt by derick
| From: | derick@php.net | Date: | Mon, 13 Mar 2006 18:31:46 +0000 |
| Subject: | note 62846 deleted from ref.mcrypt by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-105731@lists.php.net to get a copy of this message | ||
Note Submitter: rolf at winmutt dot com
----
To encrypt data with mysql's AES_ENCRYPT() was driving me up the wall. Here is the way to do it
:
$val='data to encrypt';
$ky='asdf';
$mode=MCRYPT_MODE_ECB;
$enc=MCRYPT_RIJNDAEL_128;
$numblock=floor(strlen($val) / 16);
$pad_len= 16-(strlen($val) % 16);
$val=str_pad($val, (16*(floor(strlen($val) / 16)+1)), chr($pad_len));
$encrypted_val=mcrypt_encrypt($enc, $ky, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc,
$mode), MCRYPT_DEV_URANDOM));
Please note that the key ($ky) has to be 16 characters or less. MYSQL splits keys longer than 16
into blocks of 16 and then XORS the blocks against each other. There is a bug in AES_ENCRYPT()
(which I have duplicated in the code to prevent problems with mysql's bug) in that if the
length of the $val % 16 == 0 an extra padded block is added to value to be encrypted and the value
that is added it the length of th block (or length to be padded for $val % 16 != 0).