【问题标题】:JavaScript WebCrypto importKey error : AES key data must be 128 or 256 bitsJavaScript WebCrypto importKey 错误:AES 密钥数据必须是 128 或 256 位
【发布时间】:2017-11-27 14:30:45
【问题描述】:

我正在尝试导入现有密钥,但我得到了什么: "AES 密钥数据必须是 128 或 256 位"

我有一个从 0 到 255 的 128 int 的 ArrayBuffer,即使我用 Uint8Array 包装它也无法正常工作。即使是新的 Uint8Array(128) 也会返回相同的错误。

crypto.subtle.importKey("raw", new Uint8Array(128), { name: "AES-CBC" }, true, ["encrypt", "decrypt"]).then(cryptoKey => {
            console.log(cryptoKey);

        }).catch(err => {
            console.log(err);
            });

【问题讨论】:

  • 一个 128 元素的 Uint8 数组中有 1024 位。

标签: javascript encryption cryptography aes webcrypto-api


【解决方案1】:

错误很明显;您使用的密钥缓冲区太大(1024 位)。如果您使用 16 或 32 元素 Uint8 数组,它可以工作:

crypto.subtle.importKey("raw", new Uint8Array(16), { name: "AES-CBC" }, true, ["encrypt", "decrypt"]).then(cryptoKey => {
    console.log(cryptoKey);

}).catch(err => {
    console.log(err);
});

【讨论】:

    猜你喜欢
    • 2011-05-23
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 2015-04-10
    • 2013-08-23
    • 1970-01-01
    相关资源
    最近更新 更多