2

Encrypting in PHP with mcrypt

<?php
$string = 'Secret Message';
$key = 'd4b494e4502a62edd695a903a94c2701';
$iv = '02f30dffbb0d084755f438f7d8be4a7d';

$encrypted = base64_encode(
    mcrypt_encrypt(
        MCRYPT_RIJNDAEL_256,
        $key,
        $string,
        MCRYPT_MODE_CBC,
        $iv
    )
);
//$encrypted results in 'nYoFAiyDARVSI09lH/IPdim5TvE51izVjk6sc2AK9Rg='
?>

Decrypting in Javascript with CryptoJS

<script>
var encrypted = 'nYoFAiyDARVSI09lH/IPdim5TvE51izVjk6sc2AK9Rg=';
var key = CryptoJS.enc.Hex.parse('d4b494e4502a62edd695a903a94c2701');
var iv = CryptoJS.enc.Hex.parse('02f30dffbb0d084755f438f7d8be4a7d');

var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.Pkcs7});

console.log(decrypted.toString(CryptoJS.enc.Utf8)); //prints an empty string
</script>

I can't figure out how to get the Javascript side to spit out the original text.

6
  • 2
    MCRYPT_RIJNDAEL_256 is not AES; MCRYPT_RIJNDAEL_128 is. Oct 29, 2014 at 11:25
  • Thanks, but then is there a way to decrypt RIJNDAEL cipher with 256-bit blocks in javascript? Oct 29, 2014 at 23:14
  • Sure, find a Rijndael javascript implementation. Oct 30, 2014 at 0:07
  • Thanks. Also, when I change the block size in php to MCRYPT_RIJNDAEL_256, it still doesn't work... Oct 30, 2014 at 1:40
  • Did you mean MCRYPT_RIJNDAEL_128 in the previous comment? Anyway, the padding used by mcrypt_encrypt is incompatible with everything else, in particular the PKCS7 padding used by Javascript. See owlstead's answer in this question for more info. Oct 30, 2014 at 11:13

1 Answer 1

6

SOLVED

Note: I found out that "MCRYPT_RIJNDAEL_256" in PHP's mcrypt is NOT included in AES. It uses the Rijndael method, BUT with a 256-bit block size (not included in AES). So, CryptoJS does not handle 256-bit block sizes for Rijndael. Thanks, GregS

Nonetheless, I found an implementation that successfully decrypted the ciphertext I get from running my PHP mcrypt function above, using MCRYPT_RIJNDAEL_256 (Rijndael, 256-bit block size, with 256-bit key/32-byte key).

Here it is: https://code.google.com/p/js-mcrypt/

<script>
var encrypted = 'nYoFAiyDARVSI09lH/IPdim5TvE51izVjk6sc2AK9Rg=';
var key = 'd4b494e4502a62edd695a903a94c2701';
var iv = '02f30dffbb0d084755f438f7d8be4a7d';
var decrypted = mcrypt.Decrypt(atob(encrypted), iv, key, 'rijndael-256', 'cbc');
</script>

I hope this helps someone as I spent a week of my spare time trying to figure this out and almost giving up.

2
  • 1
    It works well for me. but after decryption i am getting extra spaces. could you help me about that.
    – Manish
    Jul 19, 2017 at 16:20
  • 2
    Just Use decrypted.replace(/\0/g, 'your replacement text'); Jul 19, 2017 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.