AES encrypt in Node.js Decrypt in PHP. Fail. (Node.js 中的 AES 加密 PHP 中的解密.失败.)
问题描述
在 node.js 中,我使用内置函数来加密数据:
In node.js, I use the build in function to encrypt data like that:
输出(加密文本)为:OnNINwXf6U8XmlgKJj48iA==
the output (encrypted text) is: OnNINwXf6U8XmlgKJj48iA==
然后我在 PHP 中使用解密:
Then I use decrypt it in PHP:
我会得到一些我无法解密的有趣字符.我尝试使用/不使用 base64_decode 或 MCRYPT_RIJNDAEL_128 .. 都失败了.
I will get some funny characters, which I can't decrypted it. I tried with/without base64_decode or MCRYPT_RIJNDAEL_128.. all fail.
然后我检查了PHP中的加密方式,它看起来与node.js的输出非常不同.
Then I check how the encryption in PHP, it looks very different from the output from node.js.
它可以加密和解密.加密数据为:njCE/fk3pLD1/JfiQuyVa6w5H+Qb/utBIT3m7LAcetM=
It can encrypt and decrypt. and the encrypted data is : njCE/fk3pLD1/JfiQuyVa6w5H+Qb/utBIT3m7LAcetM=
这与 node.js 的输出非常不同,请告知我如何在 node.js & 之间进行加密和解密.php.ini谢谢.:)
which is very different from the output from node.js please advise how I can encrypt and decrypt between node.js & php. thanks. :)
@Mel 这是我在 PHP 中所拥有的:
@Mel here is what I have in PHP:
然而,它仍然不起作用.
However, it still doesn't work.
PHP 中加密的 base 64 为:80022AGM4/4qQtiGU5oJDQ==nodejs中加密的base 64为:EoYRm5SCK7EPe847CwkffQ==
The encrypted base 64 in PHP is: 80022AGM4/4qQtiGU5oJDQ== The encrypted base 64 in nodejs is: EoYRm5SCK7EPe847CwkffQ==
因此,我无法在 PHP 中解密 nodejs 之一.
thus, i can't decrypt the nodejs one in PHP.
不知道是不是因为nodejs不需要$iv?
I wonder if it is because nodejs doesn't require $iv?
推荐答案
晚了七个月,但我也在为此苦苦挣扎,并找到了解决方案.显然,PHP 用零字节填充输入以使其大小成为块大小的倍数.例如,使用 AES-128,14 字节输入contrabassists"将填充两个零字节,如下所示:
Seven months late, but I was struggling with this as well, and found a solution. Apparently, PHP pads the input with zero bytes to make its size a multiple of the block size. For example, using AES-128, the 14 byte input "contrabassists" will be padded with two zero bytes, like this:
一个 N*blocksize 字节的输入被单独留下.
A N*blocksize byte input is left alone.
然而,标准的 Node 加密函数使用一种不同的填充方案,称为 PKCS5.PKCS5 不添加零,但会添加填充的长度,因此再次使用 AES-128,低音提琴手"将变为:
The standard Node crypto functions, however, use a different padding scheme called PKCS5. PKCS5 doesn't add zeros, but adds the length of the padding, so again using AES-128, "contrabassists" would become:
即使是 N*blocksize 字节输入也会在 PKCS5 中填充.否则,解码后无法去除填充.输入的光谱日光图"将变为:
Even a N*blocksize byte input gets padded in PKCS5. Otherwise, it's impossible to remove the padding after decoding. The input "spectroheliogram" would then become:
要使 PHP m_crypt 加密与 Node 解密兼容,您必须自己填充输入:
To make PHP m_crypt encryption compatible with Node decryption, you'll have to pad the input yourself:
反之,您必须读取解码数据的最后一个字节并自行切断填充.
The other way around, you'll have to read the last byte of the decoded data and cut off the padding yourself.
示例函数:(2012 年 1 月 14 日添加)
Example functions: (added 01-14-2012)
在 PHP 中,此函数将返回 AES-128 加密的十六进制编码数据,节点可以解密:
In PHP, this function would return AES-128 encrypted, hex encoded data that can be decrypted by Node:
在 Node 中,以下将解密数据:
In Node, the following would decrypt the data:
我还没有做过相反的事情,但是一旦你理解了填充方案,它应该很简单.我没有对 key/iv 生成做任何假设.
I haven't done the reverse yet, but it should be straightforward once you understand the padding scheme. I haven't made any assumptions about key/iv generation.
这篇关于Node.js 中的 AES 加密 PHP 中的解密.失败.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!