Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
813 views
in Technique[技术] by (71.8m points)

cryptography - Node.js Crypto, what's the default padding for AES?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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://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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...