WebCrypto AES-CBC 输出 256 位而不是 128 位

WebCrypto AES-CBC outputting 256bit instead of 128bits

我正在玩 WebCrypto,我得到了一个令人困惑的输出。

以下测试用例使用新生成的 128 位密钥和 128 位随机 IV 加密随机 16 字节(128 位)纯文本,但输出 32 字节(256 位)输出。

如果我记得 AES-CBC 的细节,它应该输出 128 位块。

function test() { 

    var data = new Uint8Array(16);
    window.crypto.getRandomValues(data);
    console.log(data)


    window.crypto.subtle.generateKey(
        {
            name: "AES-CBC",
            length: 128, 
        },
        false, 
        ["encrypt", "decrypt"] 
    )
    .then(function(key){
        //returns a key object
        console.log(key);

        window.crypto.subtle.encrypt(
            {
                name: "AES-CBC",
                iv: window.crypto.getRandomValues(new Uint8Array(16)),
            },
            key, 
            data 
        )
        .then(function(encrypted){
            console.log(new Uint8Array(encrypted));
        })
        .catch(function(err){
            console.error(err);
        });
    })
    .catch(function(err){
        console.error(err);
    });

}

示例输出:

Uint8Array(16) [146, 207, 22, 56, 56, 151, 125, 174, 137, 69, 133, 36, 218, 114, 143, 174]
CryptoKey {
   algorithm: {name: "AES-CBC", length: 128}
   extractable: false
   type: "secret"
   usages: (2) ["encrypt", "decrypt"]
   __proto__: CryptoKey
Uint8Array(32) [81, 218, 52, 158, 115, 105, 57, 230, 45, 253, 153, 54, 183, 19, 137, 240, 183, 229, 241, 75, 182, 19, 237, 8, 238, 5, 108, 107, 123, 84, 230, 209]

知道我错了什么吗。

(如果更合适,可以移至 crypto.stackexchange.com)

我目前正在 MacOS 上 Chrome 71 上进行测试。

是的。额外的 16 个字节是填充。即使消息文本是块大小的倍数,也会添加填充,否则解密逻辑不知道何时寻找填充。

Web Cryptography API Specification 说:

When operating in CBC mode, messages that are not exact multiples of the AES block size (16 bytes) can be padded under a variety of padding schemes. In the Web Crypto API, the only padding mode that is supported is that of PKCS#7, as described by Section 10.3, step 2, of [RFC2315].

这意味着与其他语言实现(如 Java)不同,您可以指定 NoPadding 当您知道您的输入消息文本始终是块大小的倍数(对于 128 位AES), Web Cryptography API 强制您使用 PKCS#7 填充。

如果我们查看 RFC2315

Some content-encryption algorithms assume the input length is a multiple of k octets, where k > 1, and let the application define a method for handling inputs whose lengths are not a multiple of k octets. For such algorithms, the method shall be to pad the input at the trailing end with k - (l mod k) octets all having value k - (l mod k), where l 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 l mod k = k-1
    02 02 -- if l mod k = k-2
                .
                .
                .
  k k ... k k -- if l mod k = 0

The padding can be removed unambiguously since all input is padded and no padding string is a suffix of another. This padding method is well-defined if and only if k < 256; methods for larger k are an open issue for further study.

注:k k ... k k -- if l mod k = 0

如果引用subtle.encrypt签名,则无法指定填充模式。这意味着,解密逻辑总是需要填充。

但是,在您的情况下,如果您仅使用 Web Cryptography API 进行加密,而您的 Python 应用程序(使用 NoPadding)仅用于解密,我认为您可以简单地在将其提供给 Python 应用程序之前,从密文中剥离最后 16 个字节。这是仅用于演示目的的代码示例:

function test() { 

    let plaintext = 'GoodWorkGoodWork';
    let encoder = new TextEncoder('utf8');
    let dataBytes = encoder.encode(plaintext);

    window.crypto.subtle.generateKey(
        {
            name: "AES-CBC",
            length: 128, 
        },
        true, 
        ["encrypt", "decrypt"] 
    )
    .then(function(key){
        crypto.subtle.exportKey('raw', key)
        .then(function(expKey) {
            console.log('Key = ' + btoa(String.
                fromCharCode(...new Uint8Array(expKey))));
        });
                
        let iv = new Uint8Array(16);
        window.crypto.getRandomValues(iv);
        let ivb64 = btoa(String.fromCharCode(...new Uint8Array(iv)));
        console.log('IV = ' + ivb64);

        window.crypto.subtle.encrypt(
            {
                name: "AES-CBC",
                iv: iv,
            },
            key, 
            dataBytes 
        )
        .then(function(encrypted){
            console.log('Cipher text = ' + 
                btoa(String.fromCharCode(...new Uint8Array(encrypted))));
        })
        .catch(function(err){
            console.error(err);
        });
    })
    .catch(function(err){
        console.error(err);
    });

}

上面的输出是:

IV = qW2lanfRo2H/3aSLzxIecA==
Key = 0LDBq5iz243HBTUE/lrM+A==
Cipher text = Wa4nIF0tt4PEBUChiH1KCkSOg6L2daoYdboEEf+Oh6U=

现在,我将这些作为输入,去掉密文的最后 16 个字节,使用以下 Java 代码解密后仍然得到相同的消息文本:

package com.sapbasu.javastudy;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class EncryptCBC {
  public static void main(String[] arg) throws Exception {
    
    SecretKey key = new SecretKeySpec(Base64.getDecoder().decode(
        "0LDBq5iz243HBTUE/lrM+A=="),
        "AES");
    
    IvParameterSpec ivSpec = new IvParameterSpec(Base64.getDecoder().decode(
        "qW2lanfRo2H/3aSLzxIecA=="));
        
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
    
    byte[] cipherTextWoPadding = new byte[16];
    System.arraycopy(Base64.getDecoder().decode(
        "Wa4nIF0tt4PEBUChiH1KCkSOg6L2daoYdboEEf+Oh6U="),
        0, cipherTextWoPadding, 0, 16);
    
    byte[] decryptedMessage = cipher.doFinal(cipherTextWoPadding);
    System.out.println(new String(decryptedMessage, StandardCharsets.UTF_8));
  }
}