Mcrypt js encryption value is different than that produced by PHP mcrypt / Mcrypt JS decrypt doesn't work for UTF-8 chars-Collection of common programming errors
The main issue appears to be that your string_encrypt
and string_decrypt
PHP functions don’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 inside the encrypt function to confirm this.
Another issue is a bug in the Mcrypt JS library. This library pads the encryption key with \0
if the key length is less than 32 bytes, the problem is that this is not how the PHP mcrypt_encrypt
function pads the key. The mcrypt_encrypt
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:
if(key.length
function toHex(str) {
var hex = '';
for(var i=0;i