Node.js Crypto,AES 的默认填充是什么? [英] Node.js Crypto, what's the default padding for AES?

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

问题描述

我浏览了 Node.js Crypto 文档,但仍然找不到 Cipher 类 使用的默认填充,例如方法 cipher.setAutoPadding(true) 没有关于它的规范.那么是 PKCS#5、PKCS#7 吗?

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!

推荐答案

在文档中(https://nodejs.org/api/crypto.html#crypto_cipher_setautopadding_autopadding)它说:

禁用自动填充对于非标准填充很有用,例如使用 0x0 而不是 PKCS 填充.

Disabling automatic padding is useful for non-standard padding, for instance using 0x0 instead of PKCS padding.

所以它使用的是PKCS".更具体地说,PKCS7.

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

PKCS7 定义了与 PKCS5 相同的填充算法,但 PKCS5 假设所有密码都具有 8 字节(64 位)块大小.PKCS7 的版本将其描述为一个 k 字节块.在实践中,人们忽略了 PKCS5 具有固定的块大小,并且PKCS5 填充"是固定的.和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://www.rfc-editor.org/rfc/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://www.rfc-editor.org/rfc/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 Crypto,AES 的默认填充是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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