gpt4 book ai didi

javascript - 使用 Web Crypto 导入 PEM key

转载 作者:行者123 更新时间:2023-12-01 00:35:34 33 4
gpt4 key购买 nike

我将 CryptoKey 导出为 PEM 样式,现在我想将其导入回来。
我使用以下代码生成了我的 key :

function generate() {
return window.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: "SHA-256"},
},
true,
["encrypt", "decrypt"]
).then(function (key) {
return key;
})
.catch(function (err) {
console.error(err);
});
}

我正在尝试使用以下代码导入字符串(pem 样式)的私钥:
function importPrivateKey(pemKey) {
return crypto.subtle.importKey("pkcs8", convertPemToBinary(pemKey), {name:"RSA-OAEP", hash:{name:"SHA-256"}}, true, ["encrypt", "decrypt"]);}

不幸的是,它返回此错误:
SyntaxError: Cannot create a key using the specified key usages.

更新

convertPemToBinary 函数
function convertPemToBinary(pem) {
var lines = pem.split('\n');
var encoded = '';
for (var i = 0; i < lines.length; i++) {
if (lines[i].trim().length > 0 &&
lines[i].indexOf('-----BEGIN RSA PRIVATE KEY-----') < 0 &&
lines[i].indexOf('-----BEGIN RSA PUBLIC KEY-----') < 0 &&
lines[i].indexOf('-----BEGIN PUBLIC KEY-----') < 0 &&
lines[i].indexOf('-----END PUBLIC KEY-----') < 0 &&
lines[i].indexOf('-----BEGIN PRIVATE KEY-----') < 0 &&
lines[i].indexOf('-----END PRIVATE KEY-----') < 0 &&
lines[i].indexOf('-----END RSA PRIVATE KEY-----') < 0 &&
lines[i].indexOf('-----END RSA PUBLIC KEY-----') < 0) {
encoded += lines[i].trim();
}
}
return base64StringToArrayBuffer(encoded);
}

convertPemToBinary 函数中使用的子函数:
function base64StringToArrayBuffer(b64str) {
b64str = b64EncodeUnicode(b64str);
var byteStr = atob(b64str);
var bytes = new Uint8Array(byteStr.length);
for (var i = 0; i < byteStr.length; i++) {
bytes[i] = byteStr.charCodeAt(i);
}
return bytes.buffer;
}


function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}

最佳答案

您不能使用 RSA-OAEP 私钥进行加密。该问题可能是由于您设置了 encrypt导入时在 key 用法中标记。

参见 webcrypto 规范,第 22.4 节 https://w3c.github.io/webcrypto/Overview.html#rsa-oaep

If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.



更新

函数 base64StringToArrayBuffer是不正确的
 b64str = b64EncodeUnicode(b64str);
var byteStr = atob(b64str);

PEM 是 base64 编码的,函数 b64EncodeUnicode正在对内容进行两次编码。

在这里查看我的答案 https://stackoverflow.com/a/38714970/6371459包含生成 RSA-OAEP key 、导出、编码为 PEM、解码和再次导入的完整示例(注意:不使用 header )

将这两行替换为
function b64DecodeUnicode(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}

关于javascript - 使用 Web Crypto 导入 PEM key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41529138/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com