note 93543 deleted from mcrypt.examples by danbrown

From: Date: Tue, 15 Sep 2009 17:28:22 +0000
Subject: note 93543 deleted from mcrypt.examples by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160582@lists.php.net to get a copy of this message
Note Submitter: borvik ---- 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);

« previous php.notes (#160582) next »