简体   繁体   English

在.NET中使用零填充进行AES加密并使用Node.js解密

[英]AES encrypt in .NET with zero padding and decrypt with Node.js

I'm trying to decrypt some data with a NodeJS. 我正在尝试使用NodeJS解密一些数据。

This data was created with C# and the AES-CBC-256 algorithm. 该数据是使用C#和AES-CBC-256算法创建的。 The keySize and blockSize are 256 and the Padding is ZeroPadding . keySize和blockSize为256,Padding为ZeroPadding

I cant' decrypt it with Node.js, the error is: 我无法使用Node.js对其进行解密,错误是:

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

Here is my javascript code: 这是我的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;

I use the library "crypto". 我使用库“ crypto”。 I read somewhere that node.js decryption works only with PKSC7 padding. 我在某处读到node.js解密仅适用于PKSC7填充。 Is it true ? 是真的吗 I can't change anything in the C# project, I must find a solution on the node side. 我无法在C#项目中进行任何更改,必须在节点侧找到解决方案。

Can you help me please ? 你能帮我吗 ?

Edit: I tried to disable autoPadding with this: 编辑:我试图禁用自动填充与此:

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

But I received this error: 但我收到此错误:

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

A look at the Crypto Node.js doc finds this: 看看Crypto Node.js文档会发现:

decipher.setAutoPadding(auto_padding=true) 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. 如果数据已加密而没有标准块填充,则可以禁用自动填充,以防止decipher.final检查和删除数据。 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.update。

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

There is a difference between AES and Rijndael. AES和Rijndael之间有区别。 Both specify key sizes of 128, 192 and 256 bits, but only Rijndael provides all three block sizes: 128, 192 and 256 bits. 两者都指定128、192和256位的密钥大小,但是只有Rijndael提供所有三种块大小:128、192和256位。 AES is actually Rijndael with a fixed block size of 128 bit. AES实际上是Rijndael,具有128位的固定块大小。

Node.js uses OpenSSL to provide all the available ciphers ( crypto.getCiphers() ). Node.js使用OpenSSL提供所有可用的密码( crypto.getCiphers() )。 If there is no rijndael implementation available for your version, then you can't use node.js' Crypto module. 如果您的版本没有可用的rijndael实现,则不能使用node.js的Crypto模块。 You'll either need to find a node module that implements Rijndael or simply set the block size to 128 bit in your C# code. 您要么需要找到一个实现Rijndael的节点模块,要么只需在C#代码中将块大小设置为128位即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM