note 93543 added to mcrypt.examples
| From: | borvik at osu1 dot php dot net | Date: | Tue, 15 Sep 2009 15:43:39 +0000 |
| Subject: | note 93543 added to mcrypt.examples | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160579@lists.php.net to get a copy of this message | ||
jizz - while helpful it isn't accurate.
The PHP doesn't match the VB encrypted text when the lengths are 8, 16, 24, 32, 40, 48, 56,
etc...
The bit check you use here really depends on the length of the input string (string to be
encrypted). Which can be done with the following equation:
$n = strlen($text);
$bit_check = $n + (8 - ($n % 8));
I've encapsulated those functions into a singleton class with modifications to the
encrypt/decrypt functions.
Hope it helps.
class Encryption{
static private $key;
static private $iv;
static private $bit_check;
static function init(){
Encryption::$key = "xluFr8ax25Fl7WrIUyi1wRia"; // 24 bit Key
Encryption::$iv = "mI9Goe8R"; // 8 bit IV
Encryption::$bit_check = 8;
}
static function encrypt($text){
Encryption::init();
$n = strlen($text);
Encryption::$bit_check = $n + (8 - ($n % 8));
$text_num = str_split($text, Encryption::$bit_check);
$text_num = Encryption::$bit_check - strlen($text_num[count($text_num)-1]);
for($i = 0; $i < $text_num; $i++)
$text = $text.chr($text_num);
$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'cbc', '');
mcrypt_generic_init($cipher, Encryption::$key, Encryption::$iv);
$encrypted = mcrypt_generic($cipher, $text);
mcrypt_generic_deinit($cipher);
return base64_encode($encrypted);
}
static function decrypt($text){
Encryption::init();
$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'cbc', '');
mcrypt_generic_init($cipher, Encryption::$key, Encryption::$iv);
$decrypted = mdecrypt_generic($cipher, base64_decode($text));
mcrypt_generic_deinit($cipher);
$last_char = substr($decrypted, -1);
for($i = 0; $i <= Encryption::$bit_check; $i++){
if( chr($i) == $last_char ){
$decrypted = substr($decrypted, 0, strlen($decrypted) - $i);
break;
}
}
return $decrypted;
}
}
$input = "Text to encrypt"; // text to encrypt
$str = Encryption::encrypt($input);
echo "Start: $input - Excrypted: $str - Decrypted: ".Encryption::decrypt($str);
----
Server IP: 64.71.164.2
Probable Submitter: 64.141.146.11
----
Manual Page -- http://www.php.net/manual/en/mcrypt.examples.php
Edit -- https://master.php.net/note/edit/93543
Del: integrated -- https://master.php.net/note/delete/93543/integrated
Del: useless -- https://master.php.net/note/delete/93543/useless
Del: bad code -- https://master.php.net/note/delete/93543/bad+code
Del: spam -- https://master.php.net/note/delete/93543/spam
Del: non-english -- https://master.php.net/note/delete/93543/non-english
Del: in docs -- https://master.php.net/note/delete/93543/in+docs
Del: other reasons-- https://master.php.net/note/delete/93543
Reject -- https://master.php.net/note/reject/93543
Search -- https://master.php.net/manage/user-notes.php