Crypto-Js与mcrypt的不同输出 [英] Crypto-Js different output from mcrypt

查看:304
本文介绍了Crypto-Js与mcrypt的不同输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个js脚本来加密Blader的数据。如果我加密,它会返回一个输出



JS-SCRIPT RESULT

  uqnOrevjCc2YCvY3uKNjzA == 

现在,作为这个答案的基础比较,我写了甚至说,搜索类似于我的JS脚本的PHP中的等效脚本。令我困惑的是,逻辑是正确的,但答案是不同的。在我的php脚本是使用mcrypt,我有这个结果在



mcrypt RESULT

  HzfWFNKcAmkO6zJEYjbG4Q == 

如果您注意到,长度是相同的,这意味着我所做的代码的逻辑/修改是正确的。现在正如我之前说过的,我在这里复制了一些帖子。



这里是我认为使用crypto-JS的JS脚本 / p>

  function crypto_encrypt(text){//这适用于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,
模式:CryptoJS.mode.CBC,
padding:CryptoJS.pad.Pkcs7
// padding:CryptoJS.pad.ZeroPadding
});

//返回一个Base64编码的字符串。
返回加密;
}

这里是我在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);
?>

现在的问题是,我尝试从UTF8_encode到所有的base64_encode和解码,但仍然找不到什么错了,我是这个可以达到或不可以的好奇心,因为我注意到JS脚本中的IV与mcryp(PHP)中的IV不同,我试图回应它。



平安。

解决方案

div>

首先,您必须像在CryptoJS中一样使用PHP中的相同的键和IV ,否则它将无法正常工作。你比较了键和IV的值吗?它们不匹配。



其次,您必须在每一边使用相同的填充。你检查过MCrypt垫?它使用零填充。您的两个明文是不同,因为填充是明文的一部分。



最后,不想使用mcrypt_ 加密而不是mcrypt_decrypt这里?



如果你匹配键和IV,填充和PHP加密,你会得到相同的结果(我手动填充\x0a - 10 - 以匹配您的PKCS#7填充):

  $ 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 ==


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.

解决方案

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==

这篇关于Crypto-Js与mcrypt的不同输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆