我的代码:
var keyData = `-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----`;
var ciphertext = '...';
// from https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey#PKCS_8_import
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;
}
keyData = keyData
.replace(/-+[^-]+-+/g, '')
.replace("\n", '')
.replace("\r", '');
keyData = str2ab(window.atob(keyData));
window.crypto.subtle.importKey(
'pkcs8',
keyData,
{name: 'RSA-OAEP'},
true,
'decrypt'
).then(function(privateKey) {
alert('this far');
window.crypto.subtle.decrypt(
{
name: "RSA-OAEP"
},
privateKey,
ciphertext
).then(function(plaintext) {
alert(plaintext);
})
});
这里它位于JSFIDLE上(带有完整的私钥和密文):
https://jsfiddle.net/akcq65o7/1/
根据文件
window.crypto.subtle.importKey
returns a Promise
then()
. 但是
alert('this far');
电话线从未被呼叫过。我在JS控制台中也没有收到任何错误。
有什么想法吗?