这是我到目前为止从 key 对中导出公钥和私钥的方法:
let pub = await crypto.subtle.exportKey("spki", keyPair.publicKey);
let prv = await crypto.subtle.exportKey("spki", keyPair.privateKey);
这导致两个单独的数组缓冲区保存我的公钥和私钥。
我想了解如何将整个 key 对一次导出到单个数组缓冲区中?
像这样:
let pair = await crypto.subtle.exportKeyPair("spki", keyPair);
是否有网络 API 和格式?
否则有没有一种安全的方法可以将两个数组缓冲区(导出的公钥和私钥)连接在一起,以便我可以在导入时再次拆分它们?然后,在导入导出的 key 对时,我需要一些机制来处理格式错误的输入。
我必须这样做,因为我的界面要求我返回单个数组缓冲区。
最佳答案
我发现我可以使用以下代码导出公钥。我还没有弄清楚私钥。我还从以下代码构建了这个示例:
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey#subjectpublickeyinfo_export
const exported = pub;
const exportedAsString = String.fromCharCode.apply(null, new Uint8Array(exported));
const exportedAsBase64 = window.btoa(exportedAsString);
const pemExported = '-----BEGIN PUBLIC KEY-----\n'+exportedAsBase64+'\n-----END PUBLIC KEY-----`;
console.log("Public Exported Key: ", pemExported);
关于javascript - web crypto api - 如何导出 key 对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62404985/