如何使用crypto.subtle.wrapkey使用AES-KW包装RSA-PSS私钥?

发布于 2025-02-07 14:11:23 字数 1402 浏览 2 评论 0 原文

当尝试使用AES-KW包装RSA-PSS密钥时,我会遇到以下错误:

The AES-KW input data length is invalid: not a multiple of 8 bytes

但是有时候它可以工作,但是只有当“ PKCS8”格式中键的输出的长度可被8分组时。还在

那我的问题是:是否不可能使用AES-KW包装RSA-PSS密钥?我找不到任何填充选项。如果没有,我最好选择IV路线的最佳选择?

请参阅下面的代码:

export async function wrapKeyAsync(key: CryptoKey, password: string) {

    let keyMaterial = await crypto.subtle.importKey(
        "raw",
        new TextEncoder().encode(password),
        { name: "PBKDF2" },
        false,
        ["deriveBits", "deriveKey"]
    );

    let salt = crypto.getRandomValues(new Uint8Array(16));

    let wrappingKey = await crypto.subtle.deriveKey(
        {
            "name": "PBKDF2",
            salt: salt,
            "iterations": 100_000,
            "hash": "SHA-256"
        },
        keyMaterial,
        { "name": "AES-KW", "length": 256 },
        true,
        ["wrapKey", "unwrapKey"]
    );

    console.log((await crypto.subtle.exportKey("pkcs8", key)).byteLength / 8); // Only works when input is divisible by 8

    return {
        wrappedKey: new Uint8Array(await crypto.subtle.wrapKey(
            "pkcs8",
            key,
            wrappingKey,
            "AES-KW"
        )),
        salt
    };
}

I'm getting the following error when trying to wrap an RSA-PSS key using AES-KW:

The AES-KW input data length is invalid: not a multiple of 8 bytes

It works sometimes though, but only when the length of the output of the key in 'pkcs8' format is divisible by 8. That is perhaps also stated in https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/wrapKey.

My question then is: Is it not possible to wrap an RSA-PSS key using AES-KW? I can't find any padding option. If not, is my best option to go the IV route?

See below code:

export async function wrapKeyAsync(key: CryptoKey, password: string) {

    let keyMaterial = await crypto.subtle.importKey(
        "raw",
        new TextEncoder().encode(password),
        { name: "PBKDF2" },
        false,
        ["deriveBits", "deriveKey"]
    );

    let salt = crypto.getRandomValues(new Uint8Array(16));

    let wrappingKey = await crypto.subtle.deriveKey(
        {
            "name": "PBKDF2",
            salt: salt,
            "iterations": 100_000,
            "hash": "SHA-256"
        },
        keyMaterial,
        { "name": "AES-KW", "length": 256 },
        true,
        ["wrapKey", "unwrapKey"]
    );

    console.log((await crypto.subtle.exportKey("pkcs8", key)).byteLength / 8); // Only works when input is divisible by 8

    return {
        wrappedKey: new Uint8Array(await crypto.subtle.wrapKey(
            "pkcs8",
            key,
            wrappingKey,
            "AES-KW"
        )),
        salt
    };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文