【问题标题】:How to export CryptoKey?如何导出加密密钥?
【发布时间】:2016-11-02 08:06:30
【问题描述】:

我尝试在 Web Cryptography API/SubtleCrypto 中导出生成的密钥。

当我执行 crypto.subtle.exportKey 时,我收到以下错误消息: 在 Chrome 中:

DOMException: 密钥不可提取 (InvalidAccessError)

在火狐中

底层对象不支持参数或操作(InvalidAccessError)

cryptoTestObject = crypto.subtle.generateKey(
            {
                name: "AES-CBC",
                length: 256, //can be  128, 192, or 256
            },
            false, //whether the key is extractable (i.e. can be used in exportKey)
            ["encrypt", "decrypt"] //can "encrypt", "decrypt", "wrapKey", or "unwrapKey"
        )
        .then(function (key) {
            //returns a key object
            saveKeyInLocalStorage(keyName, key);
            console.log('CryptoPromise' + key);
        })
        .catch(function (err) {
            console.log(err);
        });

function saveKeyInLocalStorage(keyName, aesKey) {
    var exportPromise = crypto.subtle.exportKey('raw', aesKey);
    exportPromise.then(function (aesKey_RAW) {
        localStorage.setItem(keyName + 'key', aesKey_RAW);
        console.log("saved.");
    });
}

如何生成可以原始格式导出的密钥。

【问题讨论】:

    标签: javascript html encryption aes


    【解决方案1】:

    您正在生成将可提取设置设置为false 的密钥。改为true

    crypto.subtle.generateKey(
        {
            name: "AES-CBC",
            length: 256
        },
        true, // <-- here
        ["encrypt", "decrypt"]
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      相关资源
      最近更新 更多