我还在学习JavaScript并练习从键盘输入.我刚学会了String.fromCodePoint,而且(对我而言)似乎可以选择所有的东西String.fromCharCode.
是否String.fromCodePoint受到浏览器和设备的广泛支持,如果是,它是否String.fromCharCode过时,或者String.fromCharCode有时候你会使用它?
我将 CryptoKey 导出为 PEM 样式,现在我想将其导入回来。我使用以下代码生成了密钥:
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);
});
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下代码导入字符串(pem 样式)的私钥:
function importPrivateKey(pemKey) {
return crypto.subtle.importKey("pkcs8", convertPemToBinary(pemKey), {name:"RSA-OAEP", hash:{name:"SHA-256"}}, true, ["encrypt", "decrypt"]);}
Run Code Online (Sandbox Code Playgroud)
不幸的是,它返回这个错误:
SyntaxError: Cannot create a key using the specified key usages.
Run Code Online (Sandbox Code Playgroud)
更新
ConvertPemToBinary 函数
function convertPemToBinary(pem) {
var lines = pem.split('\n');
var encoded = '';
for (var i …Run Code Online (Sandbox Code Playgroud)