{"id":3107,"date":"2014-03-16T20:28:25","date_gmt":"2014-03-16T20:28:25","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/16\/mcrypt-js-encryption-value-is-different-than-that-produced-by-php-mcrypt-mcrypt-js-decrypt-doesnt-work-for-utf-8-chars-collection-of-common-programming-errors\/"},"modified":"2014-03-16T20:28:25","modified_gmt":"2014-03-16T20:28:25","slug":"mcrypt-js-encryption-value-is-different-than-that-produced-by-php-mcrypt-mcrypt-js-decrypt-doesnt-work-for-utf-8-chars-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/16\/mcrypt-js-encryption-value-is-different-than-that-produced-by-php-mcrypt-mcrypt-js-decrypt-doesnt-work-for-utf-8-chars-collection-of-common-programming-errors\/","title":{"rendered":"Mcrypt js encryption value is different than that produced by PHP mcrypt \/ Mcrypt JS decrypt doesn&#39;t work for UTF-8 chars-Collection of common programming errors"},"content":{"rendered":"<p>The main issue appears to be that your <code>string_encrypt<\/code> and <code>string_decrypt<\/code> PHP functions don&#8217;t have access to the <code>$key<\/code> variable, so for the encryption key <code>mcrypt_encrypt<\/code> is using <code>\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0<\/code>. See this question for an explanation. PHP should report a notice that <code>key<\/code> is undefined, have you turned off error reporting perhaps? Echo the key from inside the encrypt function to confirm this.<\/p>\n<p>Another issue is a bug in the Mcrypt JS library. This library pads the encryption key with <code>\\0<\/code> if the key length is less than 32 bytes, the problem is that this <strong>is not<\/strong> how the PHP <code>mcrypt_encrypt<\/code> function pads the key. The <code>mcrypt_encrypt<\/code> function pads the key up to the nearest valid key length (16, 24, or 32 bytes). The issue in mcrypt.js is at lines 63 and 64, change this:<\/p>\n<pre><code>if(key.length\n\n\n\n\n\n    function toHex(str) {\n        var hex = '';\n        for(var i=0;i<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The main issue appears to be that your string_encrypt and string_decrypt PHP functions don&#8217;t have access to the $key variable, so for the encryption key mcrypt_encrypt is using \\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0. See this question for an explanation. PHP should report a notice that key is undefined, have you turned off error reporting perhaps? Echo the key from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3107","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3107"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3107\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}