Search the Community
Showing results for tags 'cbc'.
-
So I am trying to get this simple encryption to work. I am using AES-CBC. and I know the iv is supposed to be randomly generated each time but for our purposes here it will be fine to keep it as it. Below is the code. It seems to encrypt fine but does not decrypt correctly. <?php $iv='1234567890123456'; $key='9tmp7ifi6n03yAmu'; qrdecrypt(encrypt("test",$key,$iv),$key,$iv); function encrypt($str, $key,$iv) { echo 'in encrypt<br>key:'.$key.'<br>iv:'.$iv.'<br>string:'.$str.'<br>'; $td = mcrypt_module_open("rijndael-128", "", "cbc",$iv); mcrypt_generic_init($td, $key, $iv); $encrypted = mcrypt_generic($td, $str); mcrypt_generic_deinit($td); mcrypt_module_close($td); echo "result is:".bin2hex($encrypted).'<br>returning and calling decrypt...<br>'; return bin2hex($encrypted); } function qrdecrypt($code, $key, $iv) { echo 'in decrypt<br>key:'.$key.'<br>iv:'.$iv.'<br>payload:'.$code.'<br>'; //$code = $this->hex2bin($code); $td = mcrypt_module_open("rijndael-128", "", "cbc", ""); try { mcrypt_generic_init($td, $key, $iv); } catch (Exception $e) { echo $e->getMessage(); } $decrypted = mdecrypt_generic($td, $code); $t=var_export($decrypted,true); echo '<br>Decrypted is:'.$t.'<br>'; mcrypt_generic_deinit($td); echo "deinitted<br>"; mcrypt_module_close($td); echo utf8_encode(trim($decrypted)); } this results in... in encrypt key:9tmp7ifi6n03yAmu iv:1234567890123456 string:test result is:12bc2512b0faea4e6d21368df17f87a6 returning and calling decrypt... in decrypt key:9tmp7ifi6n03yAmu iv:1234567890123456 payload:12bc2512b0faea4e6d21368df17f87a6 Decrypted is:'ro"?�o��h�ȫ_.���̬<�9ptاـ�' deinitted ro"?Ëoð¶hÊÈ«_.ÈÈø̬<ã9ptا٠Any help at all would be appreciated. I am thoroughly stuck at this point.