本帖最后由 tansuo 于 2023-12-8 19:02 编辑
下面油猴脚本exportKey参数为jwk时 公钥私钥都有值,参数为pkcs8和spki时公钥私钥的值为{}
// ==UserScript==
// @name New RSA
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match http://127.0.0.1/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=0.1
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.crypto.subtle.generateKey(
{
name: 'RSA-OAEP',
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: {
name: 'SHA-512'
}
},
true,
['encrypt', 'decrypt']
).then(function(keyPair) {
window.crypto.subtle.exportKey('pkcs8', keyPair.privateKey).then(function(privateKey) {
window.crypto.subtle.exportKey('spki', keyPair.publicKey).then(function(publicKey) {
var privateKeyStr = JSON.stringify(privateKey);
var publicKeyStr = JSON.stringify(publicKey);
console.log("Private Key:", privateKeyStr);
console.log("Public Key:", publicKeyStr);
debugger;
// 调用其他函数或操作
});
});
});
})();