WebCrypto 承诺似乎没有被调用
WebCrypto promises don't seem to be getting called
我的代码:
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);
})
});
在 JSFiddle 上(带有完整的私钥和密文):
https://jsfiddle.net/akcq65o7/1/
根据文档 window.crypto.subtle.importKey
returns a Promise,因此 then()
。但是 alert('this far');
行从未被调用过。我在 JS 控制台中也没有收到任何错误。
有什么想法吗?
脚本中有一些小问题,我在下面发布的代码中标出了:
var keyData = `-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQD+80tGPsmRlVBe
+BHRwrBtqKRMzkYBfgmiP6EINNN8NDdXY80KNMb8w+Rx97GgFGPrqVuIPy6RnNRz
Qd77oXmlSqwOPuGIl+x/fga9hL7Z7CfvZz1pqSwBT01OtLNbTMJEMiZtImqOiLIr
1PE8kOVF3B2VssZhwjVy8qppYmix/5XiZM/1nOUvMeiHCq0UAJ4lNA2fLVQkQZSU
pG2HdNIuDHfBBhEGvKdXK4oRwOgeAadpUbOwhUcTU9SSUi4Gvkg6S3WVFdm4seXN
kV2mav9j/oGUF22YO60/y5GDLBEbfsIOpGVLjFnqUJbOu9wBizMTvd5Jt9l7HPAF
GtECNyIHAgMBAAECgg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DwEqr9/46iPP7I4D
OTzDCLLKnwx/2X6OtWKNC3h6mCCCp74tYtkpJlXxmD2XIm1W/onvHGvwrZBU8Gp2
eUBF7ioXhH2HUKBPV6X9y3f70jOoD9iDl4ukXbXMFNI8LPzzEBCgJi0VfRPYJuqG
XWYyiGL0bGW4mhUd5/ZFkbWg
-----END PRIVATE KEY-----`;
var ciphertext = 'Sv99/pCslCtU+yAxV0dR+JyQ7yfiVF7+MCHErQEMSL7CyEndKikU7Al0IiRDx6Bd/PmKR6j0mU7WXISptZA1uE82jdDEAhcs9mQtdX18Or4OceIGcqHcHDCZWFQygHI0VMbywVv5C18ydFDuGvHL/dga9ji8IbFW393sEfjx6JtV8Nl4+yiEbee/+dBSQTg+E98asW1Tg59HUNc/fE1r7Muw6hgl7eiF/GxndYg7O957LNH4T3Vi6qutG4mP0frGJ3za6RNkuRrO5Wud3WnDOndHNAoLW7cuMyvzXzfhh3YMPqmkKtRsChJkV+DfPU8HdALfuyP/botQnUGCmwanKg==';
// 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',
hash: "SHA-256" // Fix 1: Set digest
},
true,
['decrypt'] // Fix 2: Use brackets [...]
).then(function(privateKey) {
console.log('this far');
window.crypto.subtle.decrypt(
{
name: "RSA-OAEP"
},
privateKey,
str2ab(window.atob(ciphertext)) // Fix 3: Base64 decode ciphertext and load into ArrayBuffer
).then(function(plaintext) {
console.log(new TextDecoder().decode(plaintext)); // Fix 4: UTF-8 decode plaintext
})
});
我的代码:
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);
})
});
在 JSFiddle 上(带有完整的私钥和密文):
https://jsfiddle.net/akcq65o7/1/
根据文档 window.crypto.subtle.importKey
returns a Promise,因此 then()
。但是 alert('this far');
行从未被调用过。我在 JS 控制台中也没有收到任何错误。
有什么想法吗?
脚本中有一些小问题,我在下面发布的代码中标出了:
var keyData = `-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQD+80tGPsmRlVBe
+BHRwrBtqKRMzkYBfgmiP6EINNN8NDdXY80KNMb8w+Rx97GgFGPrqVuIPy6RnNRz
Qd77oXmlSqwOPuGIl+x/fga9hL7Z7CfvZz1pqSwBT01OtLNbTMJEMiZtImqOiLIr
1PE8kOVF3B2VssZhwjVy8qppYmix/5XiZM/1nOUvMeiHCq0UAJ4lNA2fLVQkQZSU
pG2HdNIuDHfBBhEGvKdXK4oRwOgeAadpUbOwhUcTU9SSUi4Gvkg6S3WVFdm4seXN
kV2mav9j/oGUF22YO60/y5GDLBEbfsIOpGVLjFnqUJbOu9wBizMTvd5Jt9l7HPAF
GtECNyIHAgMBAAECggEAHXzOTAWfErOOZC2VAgnelhBXUHrbJfkJ7DYnzecuPsNb
U0Q7xt5r1Xhix7u6U6WIGQ2xh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BU8Gp2
eUBF7ioXhH2HUKBPV6X9y3f70jOoD9iDl4ukXbXMFNI8LPzzEBCgJi0VfRPYJuqG
XWYyiGL0bGW4mhUd5/ZFkbWg
-----END PRIVATE KEY-----`;
var ciphertext = 'Sv99/pCslCtU+yAxV0dR+JyQ7yfiVF7+MCHErQEMSL7CyEndKikU7Al0IiRDx6Bd/PmKR6j0mU7WXISptZA1uE82jdDEAhcs9mQtdX18Or4OceIGcqHcHDCZWFQygHI0VMbywVv5C18ydFDuGvHL/dga9ji8IbFW393sEfjx6JtV8Nl4+yiEbee/+dBSQTg+E98asW1Tg59HUNc/fE1r7Muw6hgl7eiF/GxndYg7O957LNH4T3Vi6qutG4mP0frGJ3za6RNkuRrO5Wud3WnDOndHNAoLW7cuMyvzXzfhh3YMPqmkKtRsChJkV+DfPU8HdALfuyP/botQnUGCmwanKg==';
// 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',
hash: "SHA-256" // Fix 1: Set digest
},
true,
['decrypt'] // Fix 2: Use brackets [...]
).then(function(privateKey) {
console.log('this far');
window.crypto.subtle.decrypt(
{
name: "RSA-OAEP"
},
privateKey,
str2ab(window.atob(ciphertext)) // Fix 3: Base64 decode ciphertext and load into ArrayBuffer
).then(function(plaintext) {
console.log(new TextDecoder().decode(plaintext)); // Fix 4: UTF-8 decode plaintext
})
});