note 62846 added to ref.mcrypt
| From: | rolf at winmutt dot com | Date: | Fri, 10 Mar 2006 19:30:27 +0000 |
| Subject: | note 62846 added to ref.mcrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-105235@lists.php.net to get a copy of this message | ||
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).
----
Server IP: 216.194.113.175
Probable Submitter: 66.115.128.132
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/ref.mcrypt.php
Edit -- http://master.php.net/note/edit/62846
Del: integrated -- http://master.php.net/note/delete/62846/integrated
Del: useless -- http://master.php.net/note/delete/62846/useless
Del: bad code -- http://master.php.net/note/delete/62846/bad+code
Del: spam -- http://master.php.net/note/delete/62846/spam
Del: non-english -- http://master.php.net/note/delete/62846/non-english
Del: in docs -- http://master.php.net/note/delete/62846/in+docs
Del: other reasons-- http://master.php.net/note/delete/62846
Reject -- http://master.php.net/note/reject/62846
Search -- http://master.php.net/manage/user-notes.php