gpt4 book ai didi

JavaScript,加密.subtle : how to import RSA private key?

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

我使用 python 生成了 RSA key 对,我想在 javascript 中导入它。我成功导入了公钥,但在导入私钥时遇到了困难。

Python:

from Cryptodome.PublicKey import RSA

key = RSA.generate(1024)
private_key = key.export_key().decode("ascii")
public_key = key.publickey().export_key().decode("ascii")

Javascript:

function str2ab(str) {
const buf = new ArrayBuffer(str.length);
const bufView = new Uint8Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}

async function importRsaPublicKey(pem) {
// fetch the part of the PEM string between header and footer
const pemHeader = "-----BEGIN PUBLIC KEY-----";
const pemFooter = "-----END PUBLIC KEY-----";
const pemContents = pem.substring(pemHeader.length, pem.length - pemFooter.length);
// base64 decode the string to get the binary data
const binaryDerString = window.atob(pemContents);
// convert from a binary string to an ArrayBuffer
const binaryDer = str2ab(binaryDerString);

return await window.crypto.subtle.importKey(
"spki",
binaryDer,
{
name: "RSA-OAEP",
hash: "SHA-256"
},
true,
["encrypt"]
);
}

async function importRsaPrivateKey(pem) {
// Same logic as previous
const pemHeader = "-----BEGIN RSA PRIVATE KEY-----";
const pemFooter = "-----END RSA PRIVATE KEY-----";
const pemContents = pem.substring(pemHeader.length, pem.length - pemFooter.length);

const binaryDerString = window.atob(pemContents);
const binaryDer = str2ab(binaryDerString);

return await window.crypto.subtle.importKey(
"spki",
binaryDer,
{
name: "RSA-OAEP",
hash: "SHA-256"
},
true,
["decrypt"]
);
);
}

当我尝试像这样导入私钥时:

var pem = "-----BEGIN RSA PRIVATE KEY-----MIICWwIBAAKBgQC1rayx8fyYUbyV5h/HtkIq3TBw5CZY8qKL4Kx5+gCLJj4ZklZ+6J6HU+OLGclLVoE7fwLPIJTRIG47kRG7eUG4rfmkXQJx4gOyBjOpfnE5GRDlXzUcymk5XOh87N1o0PGsfYm844PYZaU2MWiyvob717LlEELO4l7nxc11/oucTQIDAQABAoGARGd5W+CMZj90PY5RTe0qOaBhgkfsxlXI7NixqBWAyeOiwxcNuSfVtIdZ58BUQaj27JNUV+9hCOJojsX+wrMTkpaD1bmDEFibxuHOCuZQ/DszTmPNwx5INcR7wKhibbJA4rtzoHt2B8G/6mc0O+bJz6p0C4IJULTmiTvhuQULesMCQQDTZ/2zzO5sv4Z2Y2GD3RAoF+MhoYyR3Rt/36LsAmAWVQz12UIhqd0VzljEKNDUIFNHRU5GTcGSPvrBSNZbfTYPAkEA3ABfQ54KKoVQEcHNG6OI4QrA8PaQfCfVq1hMnbLoxJYRB9Fjfbs38uljJ6j6CqtQMfrrE7ZplpOXcW6FeXWD4wJAE0VJdRhbK4KR6TzJ6NE/5ce3ppspSyqSlSd3nHfi9mYuVkLFqnfndVNn+AmYb526uaZxqirwWDpxdSkEkTZqtQJAGU3ppytcW/utc/1ojA9JRSkpfA3AHKewSd8EIPddEo94MgABg4qvKr9xajRjXirKNJV5yHCowGsFdkSSEaBUpQJARQPMkQ7OWrlXLwICP9zUkPVJWkq2LSDZA1Rx3ySgbDD+VjrOo+1wtKmHuGf9QFxbqc50QT58OqrCDRWzq8BExw==-----END RSA PRIVATE KEY-----";

var private_key = await importRsaPrivateKey(pem);

我收到以下错误:

Syntax error

Cannot create a key using the specified key usages.

你知道如何纠正这个问题吗?

编辑:

我需要将 key 导出为 pkcs8:

python :

from Cryptodome.PublicKey import RSA

key = RSA.generate(1024)
private_key = key.export_key(pkcs=8).decode("ascii")
public_key = key.publickey(pkcs=8).export_key().decode("ascii")

并将 key 导入为 pkcs8 而不是 spki

javacript:

async function importRsaPrivateKey(pem) {
const pemHeader = "-----BEGIN PRIVATE KEY-----";
const pemFooter = "-----END PRIVATE KEY-----";

const pemContents = pem.substring(pemHeader.length, pem.length - pemFooter.length);

const binaryDerString = window.atob(pemContents);
const binaryDer = str2ab(binaryDerString);

return await window.crypto.subtle.importKey(
"pkcs8",
binaryDer,
{
name: "RSA-OAEP",
hash: "SHA-256"
},
true,
["decrypt"]
);
}

最佳答案

spki 用于导入公钥。将其更改为pkcs8

仅供引用, key 中的 header -----BEGIN PRIVATE KEY----- 表示您的 key 采用 PKCS #8 格式。这就是 webcrypto 可以导入的 key 形式。

如果您有-----BEGIN RSA PRIVATE KEY-----,则意味着您的 key 是以 PKCS#1 格式序列化的。在调用 importKey() 之前,您需要将其转换为 PKCS#8。请参阅How can I import an RSA private key in PEM format for use with WebCrypto?

关于JavaScript,加密.subtle : how to import RSA private key?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56152138/

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