YASAI
YASAI

Reputation: 43

Encrypto in JavaScript and Decrypto in PHP

I want to encrypt in the javascript. decrypt in the PHP.
JavaScript AES crypto library is CryptoJS.
PHP AES crypto library is mcrypt.

I was get incorrect result.
get result like 'I4��L$�"�"E̹_��zHe����V���:�'.

encrypt = CryptoJS.AES.encrypt('HelloWorld', 'test', {mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding });

$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
mcrypt_decrypt(MCRYPT_RIJNDAEL_256, 'test', $encrypt, MCRYPT_MODE_CBC, $iv);

is wrong somewhere?
Thank you.

Upvotes: 2

Views: 411

Answers (1)

Reid Johnson
Reid Johnson

Reputation: 1394

As noted in the comments above, SSL is the only way you will acutally protect any data because

  1. Without SSL, anyone could make changes to or sniff your Javascript or webpage and so it cannot be trusted.
  2. Javascript really doesn't allow for non-character based data very well. (i.e. hashes, encoded video, etc.) as all data is usually silently converted to utf-16 or utf-8 internally.

This last point is likly what is causing your problem. The AES library on each end encrypts as if it had access to binary data, however, Javascript is telling the transfer that the encrypted data is in a certain characterset. You may want to try utf8decode or play with iconv() to get it converted to true binary data.

Upvotes: 0

Related Questions