note 90938 added to function.mcrypt-cbc
| From: | Othman at osu1 dot php dot net | Date: | Sun, 17 May 2009 04:56:12 +0000 |
| Subject: | note 90938 added to function.mcrypt-cbc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154694@lists.php.net to get a copy of this message | ||
The following code is to
encrypt-->encode
decode-->decrypt
<?php
$stuff="String to enc/enc/dec/dec =,=,";
$key="XiTo74dOO09N48YeUmuvbL0E";
function nl() {
echo "<br/> \n";
}
$iv = mcrypt_create_iv (mcrypt_get_block_size (MCRYPT_TripleDES, MCRYPT_MODE_CBC),
MCRYPT_DEV_RANDOM);
// Encrypting
function encrypt($string, $key) {
$enc = "";
global $iv;
$enc=mcrypt_cbc (MCRYPT_TripleDES, $key, $string, MCRYPT_ENCRYPT, $iv);
return base64_encode($enc);
}
// Decrypting
function decrypt($string, $key) {
$dec = "";
$string = trim(base64_decode($string));
global $iv;
$dec = mcrypt_cbc (MCRYPT_TripleDES, $key, $string, MCRYPT_DECRYPT, $iv);
return $dec;
}
$encrypted = encrypt($stuff, $key);
$decrypted = decrypt($encrypted, $key);
echo "Encrypted is ".$encrypted . nl();
echo "Decrypted is ".$decrypted . nl();
?>
Notes on the result
-o running the script from the command line 1 time
php script.php works correct
-o if i ran the script in a loop i.e.
#!/bin/bash
for x in
seq 100
do
echo $x
php script.php >> LOG.text
sleep 1
done
it gets slower after the 10th time
+ inconsistent output
sometimes correct some with encrypted characters at the end of the decrypted string
-o Firefox on Linux it decrypts to the original string but appends
some encryption at the end of the decrypted sting
-o running the script on the browser from windows on Firefox,
Google Chrome, IE7 works fine for just few times
"refresh every few seconds"
if refresh fast it doesn't work correctly,
some times returns the decrypted as encrypted!
some times returns mix of encrypted & decrypted
I thought if sharing that test for those functions might be
useful for some body
----
Server IP: 72.55.140.6
Probable Submitter: 99.254.176.172
----
Manual Page -- http://www.php.net/manual/en/function.mcrypt-cbc.php
Edit -- https://master.php.net/note/edit/90938
Del: integrated -- https://master.php.net/note/delete/90938/integrated
Del: useless -- https://master.php.net/note/delete/90938/useless
Del: bad code -- https://master.php.net/note/delete/90938/bad+code
Del: spam -- https://master.php.net/note/delete/90938/spam
Del: non-english -- https://master.php.net/note/delete/90938/non-english
Del: in docs -- https://master.php.net/note/delete/90938/in+docs
Del: other reasons-- https://master.php.net/note/delete/90938
Reject -- https://master.php.net/note/reject/90938
Search -- https://master.php.net/manage/user-notes.php