encryption - Decrypting AES with Javascript CryptoJS after encrypting with PHP mcrypt - Stack Overflow

Encrypting in PHP with mcrypt<?php$string = 'Secret Message';$key = 'd4b494e4502a62

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.

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.

Share Improve this question edited Oct 29, 2014 at 23:42 Joseph Shih asked Oct 29, 2014 at 6:10 Joseph ShihJoseph Shih 1,30413 silver badges26 bronze badges 6
  • 2 MCRYPT_RIJNDAEL_256 is not AES; MCRYPT_RIJNDAEL_128 is. – President James K. Polk Commented Oct 29, 2014 at 11:25
  • Thanks, but then is there a way to decrypt RIJNDAEL cipher with 256-bit blocks in javascript? – Joseph Shih Commented Oct 29, 2014 at 23:14
  • Sure, find a Rijndael javascript implementation. – President James K. Polk Commented 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... – Joseph Shih Commented Oct 30, 2014 at 1:40
  • Did you mean MCRYPT_RIJNDAEL_128 in the previous ment? Anyway, the padding used by mcrypt_encrypt is inpatible with everything else, in particular the PKCS7 padding used by Javascript. See owlstead's answer in this question for more info. – President James K. Polk Commented Oct 30, 2014 at 11:13
 |  Show 1 more ment

1 Answer 1

Reset to default 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./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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744835160a4596218.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信