note 79135 added to function.mcrypt-module-open
| From: | dinamic at gmail dot com | Date: | Tue, 13 Nov 2007 11:05:45 +0000 |
| Subject: | note 79135 added to function.mcrypt-module-open | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132221@lists.php.net to get a copy of this message | ||
Also it should be pointed that md5() and/or sha1() should not be used while forming your key for the
mcrypt. This is so because hex encoding uses a set of only 16 characters [0-9a-f], which is
equivalent to 4 bits, and thus halve the strength of your encryption: 4 x 32 = 128-bit.
I have re-wrote the example shown, so here is my suggestion to get real 256-bit encryption:
<?php
$key1 = "this is a secret key";
$key2 = "this is the second secret key";
$input = "Let us meet at 9 o'clock at the secret place.";
$length = strlen($input);
/* Open the cipher */
$td = mcrypt_module_open('rijndael-256', '', 'cbc', '');
/* Create the IV and determine the keysize length, use MCRYPT_RAND
* on Windows instead */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$ks = mcrypt_enc_get_key_size($td);
/* Create key */
$key1 = md5($key1);
$key2 = md5($key2);
$key = substr($key1, 0, $ks/2) . substr(strtoupper($key2), (round(strlen($key2) / 2)), $ks/2);
$key = substr($key.$key1.$key2.strtoupper($key1),0,$ks);
/* Intialize encryption */
mcrypt_generic_init($td, $key, $iv);
/* Encrypt data */
$encrypted = mcrypt_generic($td, $input);
/* Terminate encryption handler */
mcrypt_generic_deinit($td);
/* Initialize encryption module for decryption */
mcrypt_generic_init($td, $key, $iv);
/* Decrypt encrypted string */
$decrypted = mdecrypt_generic($td, $encrypted);
/* Terminate decryption handle and close module */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
/* Show string */
echo "Text: ".substr($decrypted,0,$length) . "<br>";
echo "Encoded: ".$encrypted ."<br>";
echo "<br>key1: $key1 <br>key2: $key2<br>created key: $key";
?>
----
Server IP: 195.149.248.161
Probable Submitter: 212.73.143.141
----
Manual Page -- http://www.php.net/manual/en/function.mcrypt-module-open.php
Edit -- https://master.php.net/note/edit/79135
Del: integrated -- https://master.php.net/note/delete/79135/integrated
Del: useless -- https://master.php.net/note/delete/79135/useless
Del: bad code -- https://master.php.net/note/delete/79135/bad+code
Del: spam -- https://master.php.net/note/delete/79135/spam
Del: non-english -- https://master.php.net/note/delete/79135/non-english
Del: in docs -- https://master.php.net/note/delete/79135/in+docs
Del: other reasons-- https://master.php.net/note/delete/79135
Reject -- https://master.php.net/note/reject/79135
Search -- https://master.php.net/manage/user-notes.php