|
A PHP User - 07 Sep 2008
@ Glen.Boyer,
From "rolf at winmutt dot com" and "pixelchutes AT gmail DOT com" on www.php.net/mcrypt,
mysql AES_ENCRYPT() compatibly function for PHP :
function mysql_aes_encrypt($val,$ky) {
$mode=MCRYPT_MODE_ECB;
$enc=MCRYPT_RIJNDAEL_128;
$val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
return mcrypt_encrypt($enc, $ky, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
}
Please note that if the strlen($ky)>16 then this function will not be compatible.
For those looking for a mysql_aes_decrypt, I created this method, referencing rolf's aes_encrypt below. Since the aes_encrypt right-pads N * blocksize with any chr( 0 ) to chr( 16 ) (random based on the input string length) we first decrypt the text, then RTrim chr(0 .. 16) depending on its trailing ord() value.
mysql AES_DECRYPT() compatibly function for PHP :
<?
function mysql_aes_decrypt( $val, $ky )
{
$mode = MCRYPT_MODE_ECB;
$enc = MCRYPT_RIJNDAEL_128;
$dec = @mcrypt_decrypt($enc, $ky, $val, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
return rtrim( $dec, ( ( ord(substr( $dec, strlen( $dec )-1, 1 )) >= 0 and ord(substr( $dec, strlen( $dec )-1, 1 ) ) <= 16 ) ? chr(ord(substr( $dec, strlen( $dec )-1, 1 ))): null) );
}
?>
Please note that if the strlen($ky)>16 then this function will not be compatible.
A PHP User - 07 Sep 2008
how about blowfish? which do you think would be better...
well i suppose it wont hurt as i am using multiple encryption with a key separate from the users.
its just that i want to know which is better, blowfish or twofish?
A PHP User - 07 Sep 2008
I want to know the PHP code of cymmetric API
A PHP User - 07 Sep 2008
From wikipedia: http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
In June 2003, the US Government announced that AES may be used for classified information:
"The design and strength of all key lengths of the AES algorithm (i.e., 128, 192 and 256) are sufficient to protect classified information up to the SECRET level. TOP SECRET information will require use of either the 192 or 256 key lengths. The implementation of AES in products intended to protect national security systems and/or information must be reviewed and certified by NSA prior to their acquisition and use."
http://www.cnss.gov/Assets/pdf/cnssp_15_fs.pdf
Also:
Key sizes of 128, 160, 192, 224, and 256 bits are supported by the Rijndael algorithm, but only the 128, 192, and 256 bit key sizes are specified in the AES standard.
A PHP User - 07 Sep 2008
By standard you mean FIPS 197 (which is publish by the US govt) not to be confused by IETF standards.
That said, just because one US government entity ratified a crypto method for use within the US govt and anyone contract to the US govt needs to use AES, that doesn't stop other people using RIJNDAEL 256 as it should be stronger then AES.
A PHP User - 07 Sep 2008
AES is not restricted to 128 bit keys.
There are MANY compelling reasons to use key strengths higher than 128 -- i.e. you don't want somebody with a large (but not overly obscene) computing resource to break your key in a few weeks.
This book has so many glaring errors and omissions its painful to flip through.
Glen.Boyer@pacificevents.com - 07 Sep 2008
This is more of a question, and you may not know the answer, but I'll ask just in case.
Using PHP/AES/Rijndael128 vs MySQL AES_ENCRYPT, do you know a way to make these match?
Currently I'm having to make a command decision whether to have PHP do the encrypting vs MySQL doing it. At present using PHP & MCrypt I'm unable to get the same results. I'm wondering if there is some other codes or salt or something that one is doing verses the other.
Any Idea?
Potentially one or the other (PHP or MySQL) may or may not be replaced in the future. It would be nice if both systems code encrypt and decrypt the other.
A PHP User - 07 Sep 2008
Why use MCRYPT_RIJNDAEL_256 in the example in the last section, when the standard and recommended is MCRYPT_RIJNDAEL_128?
Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.
If you are reporting an error in the content, please tell me directly.
|