Hsnopi Posted November 29, 2012 Share Posted November 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271376-php-aes-cbc-issue/ Share on other sites More sharing options...
requinix Posted November 29, 2012 Share Posted November 29, 2012 You bin2hex'd the return value from encrypt(). Quote Link to comment https://forums.phpfreaks.com/topic/271376-php-aes-cbc-issue/#findComment-1396305 Share on other sites More sharing options...
Hsnopi Posted November 30, 2012 Author Share Posted November 30, 2012 yeah. i saw that. but that led me to my actual problem. Which, what you mentioned IS an issue. you'll note the commented out //$code = $this->hex2bin($code); That is what I was trying to get to work. I was so friend I never realized I never actually WROTE hex2bin! Sleep is a good thing! So, if I take out the bin2hex in the encrypt, it works, if I add hex2bin in, instead, and write the function, it also works! go me. thanks for the second pair of eyes. Quote Link to comment https://forums.phpfreaks.com/topic/271376-php-aes-cbc-issue/#findComment-1396471 Share on other sites More sharing options...
Hsnopi Posted November 30, 2012 Author Share Posted November 30, 2012 function hex2bin($hexdata) { $bindata = ""; for ($i = 0; $i < strlen($hexdata); $i += 2) { $bindata .= chr(hexdec(substr($hexdata, $i, 2))); } return $bindata; } Quote Link to comment https://forums.phpfreaks.com/topic/271376-php-aes-cbc-issue/#findComment-1396472 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.