MySQL Forums
Forum List  »  Security

Re: AES_ENCRYPT() in php
Posted by: Rolf Martin-Hoster
Date: March 10, 2006 01:21PM

This was driving me up the wall. Here ya go :

$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 value. There is a bug (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)



Edited 1 time(s). Last edit at 03/10/2006 01:22PM by Rolf Martin-Hoster.

Options: ReplyQuote


Subject
Views
Written By
Posted
21608
December 19, 2005 08:48AM
Re: AES_ENCRYPT() in php
9782
March 10, 2006 01:21PM
7185
April 19, 2006 08:57PM
6881
May 08, 2006 02:04PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.