2015-06-29 69 views
0

我试图使用NodeJS解密一些数据。在.NET中使用零填充进行AES加密并使用Node.js解密

此数据是用C#和AES-CBC-256算法创建的。 keySize和blockSize是256,填充是ZeroPadding

我不能”与Node.js的解密,错误的是:

Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length 

这里是我的javascript代码:

decipher = crypto.createDecipheriv('aes-256-cbc', key, iv.slice(0, 16)); 
decrypted = decipher.update(encryptedPayloadBuffer, 'base64', 'ascii'); 
decrypted += decipher.final('ascii'); 
decipher = null; 

return decrypted; 

我使用库 “加密”。我在某处读到node.js解密只适用于PKSC7填充。这是真的吗?我不能在C#项目中改变任何东西,我必须在节点端找到解决方案。

你能帮助我吗?

编辑:我试图用这个来禁用autoPadding:

decipher.setAutoPadding(false); 
//next line of code: 
//decrypted = decipher.update(encryptedPayloadBuffer, 'base64', 'ascii'); 

但我收到此错误:

Error: error:0606508A:digital envelope routines:EVP_DecryptFinal_ex:data not multiple of block length 
+1

如果您的块大小为256位,则不是AES。请明确你正在使用什么。 –

+0

@ ArtjomB。很可能您可以强制执行C#AES实现来执行Rijndael-256,但我不确定。 –

+0

@ArtjomB我相信这是AES。这是实施:https://gist.github.com/VivienAdnot/e9a96fea04bccfa613ec –

回答

0

AES和Rijndael之间有区别。两者都指定了128位,192位和256位的密钥大小,但只有Rijndael提供了所有三种块大小:128,192和256位。 AES实际上是固定块大小为128位的Rijndael。

Node.js使用OpenSSL提供所有可用的密码(crypto.getCiphers())。如果你的版本没有可用的rijndael实现,那么你不能使用node.js的Crypto模块。您需要找到一个实现Rijndael的节点模块,或者只需在您的C#代码中将块大小设置为128位。

+0

谢谢,我现在明白了这个问题。我搜索了另一个节点模块,而不是加密,但目前我找不到一个模块。我希望有人知道它,并会写在这里。 –

0

一看Crypto Node.js doc发现这一点:

decipher.setAutoPadding(auto_padding=true)

You can disable auto padding if the data has been encrypted without standard block padding to prevent decipher.final from checking and removing it. Can only work if the input data's length is a multiple of the ciphers block size. You must call this before streaming data to decipher.update.

尝试decipher.setAutoPadding(false)之前decipher.update.

+0

不要忘记解密后删除零填充。 –

+2

@Jim洪水。感谢您的回答。我已经试过了,但是我遇到了这个错误:数据不是块长度的多倍 –

相关问题