本文介绍了Node.js Crypo,AES的默认填充是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遍历了Node.js加密文档,但是仍然找不到 Cipher类使用的默认填充,例如方法 cipher.setAutoPadding(true )没有相关说明。是PKCS#5,PKCS#7 ...吗?



关于此的任何信息都很棒!

解决方案

在文档中(),它说:

因此它使用的是 PKCS 。更具体地说,PKCS7。

PKCS7定义了与PKCS5相同的填充算法,但是PKCS5假定所有密码都具有8字节(64位)的块大小。 PKCS7的版本将其描述为k字节的块。实际上,人们忽略了PKCS5具有固定的块大小,并且 PKCS5填充和 PKCS7填充是同一回事。



PKCS5() :

  4。连接M和填充字符串PS以形成编码的
消息EM:

EM = M || PS,

,其中填充字符串PS由8个(|| M || mod 8)八位位组
组成,每个八位位组的值均为8-(|| M || mod 8)。填充字符串PS将
满足以下语句之一:

PS = 01,如果|| M || mod 8 = 7;如果|| M ||,则
PS = 02 02 mod 8 = 6;
...
PS = 08 08 08 08 08 08 08 08,如果|| M || mod 8 =0。

PKCS7():

 一些内容加密算法假定输入长度是k个八位位组的
倍数,其中k大于1。对于此类
算法,应在尾端用
k-(lth mod k)个八位位组填充输入,所有八位字节均具有值k-(lth mod k),其中lth为
的长度输入的换句话说,输入将在
的末尾用以下字符串之一填充:

01-如果lth mod k = k-1
02 02-如果lth mod k = k-2



k k ... k k-如果lth mod k = 0


I've traversed the Node.js Crypto documentation but still couldn't find the default padding used by the Cipher class, for example the method cipher.setAutoPadding(true) has no specification about it. So is it PKCS#5, PKCS#7...?

Any info on this will be great!

解决方案

In the documentation (https://nodejs.org/api/crypto.html#crypto_cipher_setautopadding_autopadding) it says:

So it's using "PKCS". More specifically, PKCS7.

PKCS7 defined the same padding algorithm that PKCS5 did, but PKCS5 assumed all ciphers would have 8 byte (64 bit) block sizes. PKCS7's version describes it as a k-byte block. In practice, people ignore that PKCS5 had a fixed block size, and "PKCS5 padding" and "PKCS7 padding" are the same thing.

PKCS5 (https://tools.ietf.org/html/rfc2898#section-6.1.1):

4. Concatenate M and a padding string PS to form an encoded
     message EM:

             EM = M || PS ,

     where the padding string PS consists of 8-(||M|| mod 8) octets
     each with value 8-(||M|| mod 8). The padding string PS will
     satisfy one of the following statements:

             PS = 01, if ||M|| mod 8 = 7 ;
             PS = 02 02, if ||M|| mod 8 = 6 ;
             ...
             PS = 08 08 08 08 08 08 08 08, if ||M|| mod 8 = 0.

PKCS7 (https://tools.ietf.org/html/rfc5652#section-6.3):

Some content-encryption algorithms assume the input length is a
multiple of k octets, where k is greater than one.  For such
algorithms, the input shall be padded at the trailing end with
k-(lth mod k) octets all having value k-(lth mod k), where lth is
the length of the input.  In other words, the input is padded at
the trailing end with one of the following strings:

                 01 -- if lth mod k = k-1
              02 02 -- if lth mod k = k-2
                  .
                  .
                  .
        k k ... k k -- if lth mod k = 0

这篇关于Node.js Crypo,AES的默认填充是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 21:16