2

I have a js script which encrypts data which is 'Blader'. If I encrypt it it returns an output of

JS-SCRIPT RESULT

uqnOrevjCc2YCvY3uKNjzA==

Now, being this answer as a base for comparison, I wrote or rather say searched for a equivalent script in PHP similar to my JS script. What confuses me is that the logic is fine and correct but the answer is different. On my php script which is by the uses mcrypt, I have this result in

mcrypt RESULT

HzfWFNKcAmkO6zJEYjbG4Q==

If you notice, the length are the same which means that the logic/modification on the code I did was correct. Now As i have said before I copied the script over some posts here.

Here's the JS Script which i think uses the crypto-JS

function crypto_encrypt(text) {                                                                        //This is for JS
var keyBase64 = CryptoJS.enc.Base64.parse("ITU2NjNhI0tOc2FmZExOTQ==");
var iv = CryptoJS.enc.Base64.parse('AAAAAAAAAAAAAAAAAAAAAA==');

var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(text), keyBase64,
    {
        keySize: 128 / 8,
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
        //padding: CryptoJS.pad.ZeroPadding
    });

// Returns a Base64 encoded string.
return encrypted;
}

And here's the code I found in mcrypt/mycrypt

<?php
$encrypted = "Blader";
$iv        = "0000000000000000";   // iv_base64 from JS
$key       = hexdec("213536363361234b4e736166644c4e4d");  // key_base64 from JS
$plaintext = mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $iv );
echo base64_encode($plaintext);
?>

Now the question is, I tried from UTF8_encode upto everything base64_encode and decode but still can't find what's wrong and I'm curios that is this attainable or not since I notice that the IV from JS-Script is different from the IV in mcryp(PHP) everything I tried to echo it. Any advice, comments and suggestion will be highly be thanked.

Peace out.

  • In the past I've tried to implement certain hashing and crypto functionality into a webpage. What I've learned is that there are very few use cases where any crypto functionality on the client side of a webpage is beneficial. May I ask what your intended purpose is? – Goldfish Sandwich Jun 24 '14 at 15:17
  • We'll Im just trying to check If I can duplicate a Javascript function with my own php function. I want to be more flexible when it comes to coding function so I want to reproduce what cryptoJS has by using mcrypt in php. Thanks for the answer by the way. – user3771496 Jun 24 '14 at 15:19
  • According to [this comment](http://www.php.net//manual/en/function.mcrypt-decrypt.php#105985) on the mcrypt_decrypt php doc page, AES and Rijndael don't act the same in different implementations. – Goldfish Sandwich Jun 24 '14 at 15:39
  • So to my understanding, The output that those 2 produces will never be similar? Am I correct? – user3771496 Jun 24 '14 at 15:49
  • I'm not that good at reading crypto-speak but [this answer](http://stackoverflow.com/a/18830303/1147880) from a similar question may provide some additional information. – Goldfish Sandwich Jun 24 '14 at 16:03
  • Does this line have a corresponding mcrypt version `CryptoJS.enc.Base64.parse`. I have a feeling that this is the center of everything why the answers are different. – user3771496 Jun 24 '14 at 16:10
  • @GoldfishSandwich AES is Rijndael-128, i.e. MCRYPT_RIJNDAEL_128. The 128 here is the blocksize, not the keysize. – Jim Flood Jun 24 '14 at 17:45

1 Answers1

0

First, you have to use exactly the same key and IV in PHP as you do in CryptoJS, or it's just not going to work. Did you compare values of the key and IV? They don't match.

Second, you have to use the same padding on each side. Did you check how MCrypt pads? It uses zero-padding. Your two plaintexts are different, because padding is part of the plaintext.

Finally, don't you want to use mcrypt_encrypt instead of mcrypt_decrypt here?

If you match the key and IV, and the padding, and encrypt in PHP, you'll get the same result (I've manually padded with \x0a -- 10 -- to match your PKCS#7 padding):

$encrypted = "Blader\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a";
$iv = base64_decode('AAAAAAAAAAAAAAAAAAAAAA==');
$key = base64_decode('ITU2NjNhI0tOc2FmZExOTQ==');
$plaintext = mcrypt_encrypt( MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $iv );
echo base64_encode($plaintext);

uqnOrevjCc2YCvY3uKNjzA==
Jim Flood
  • 8,144
  • 3
  • 36
  • 48
  • Hi sir. Thanks. It worked but there a small error on it. It only works in `Blader`. What if I wanted to use CROW or let's say a name sample is `Patrick`. It gives out different output. My next question like this one [Crypto-Js different output from mcrypt Upon chage of data to encrypt](http://stackoverflow.com/questions/24393820/crypto-js-different-output-from-mcrypt-upon-chage-of-data-to-encrypt) – user3771496 Jun 24 '14 at 18:57