来自mcrypt的Crypto-Js不同输出在加密数据时 [英] Crypto-Js different output from mcrypt Upon chage of data to encrypt

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

问题描述

我的问题现在与这个问题有关联来自mcrypt的Crypto-Js不同输出这就是为什么我使用相同的问题,但添加了一些额外的行来更好地解释它。

My question now has connection to this one Crypto-Js different output from mcrypt that's why I used the same question but added a few extra lines to explain it better.

基于我之前的问题,由SIr Jim解决(非常感谢对于提示也是如此)。它工作部分正确,因为它确实显示相同的结果,但只有当我使用'Blader'这个词时,如果我使用另一个词'CROW'那么两个脚本之间的输出是不同的。

Based on my previous question which is solved by SIr Jim(many thanks for the tips as well). It worked partially correct since it does show the same result but only when I use the word 'Blader' and If ever I use another word like 'CROW' then the output between the 2 scripts are different.

以下是jim爵士提供的代码,其功能类似魅力 (我在这里使用过Blader)

Here's the code which is given by Sir jim that works like a charm (I used Blader here)

$encrypted = "Blader\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);

示例是当我使用 CROW 时要加密的数据

Sample is When I use CROW as data to be ecnrypted

加密输出

dxt3uyk27U3wRRrzaFGiwQ==

以mcrypt输出

x9/oeyLZkLkXM7B1Zo+ezg==

为了解决这个问题,我在cryptoJS中删除了填充:CryptoJS.pad.Pkcs7 ,但我的问题是如果我不想删除cryptoJS中的填充?

To solve this I removed the padding: CryptoJS.pad.Pkcs7 in cryptoJS but my question is what if I don't want to remove the padding in cryptoJS?

我们将非常感谢所有答案。

All answers will be greatly appreciated.

推荐答案

你需要了解 PKCS#7填充的工作原理。它将明文带到16个字节的倍数。如果已经是16的倍数,则额外增加16个字节。

You need to look into how PKCS#7 padding works. It brings the plaintext up to a multiple of 16 bytes. If already a multiple of 16, it adds an extra 16 bytes.

对于UTF-8中的CROW(四个字节),您将增加12个字节。每个字节现在为12,即12个12字节,而不是Blader的10个10字节。例如,试试这个:

For "CROW" in UTF-8 (four bytes) you would add 12 more bytes. Each byte would be now 12, that is 12 bytes of 12, instead of 10 bytes of 10 for Blader. For example, try this:

$encrypted = "CROW\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c";

要使用PKCS#7填充,你可以尝试这样的事情:

To pad with PKCS#7 you can try something like this:

$encrypted = "CROW";

// Add PKCS#7 padding
$pad = 16 - (strlen($encrypted) % 16);
$encrypted = $encrypted . str_repeat(chr($pad), $pad);

(我会将变量命名为$ encrypted以外的变量,因为它实际上从未保存加密数据。)

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

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