WebCrypto:通过 crypto.subtle.importkey(...) 导入用于解密的私钥
WebCrypto: importing private key for decrypt by crypto.subtle.importkey(...)
我想通过网络加密导入私钥。这是我的代码:
//import private key
var keyDataDec = {
kty: 'RSA',
alg: 'RSA-OAEP-256',
e: b64ToB64Url(hexToBase64(rsaJson.e)),
n: b64ToB64Url(hexToBase64(rsaJson.n)),
d: b64ToB64Url(hexToBase64(rsaJson.d)),
p: b64ToB64Url(hexToBase64(rsaJson.p)),
q: b64ToB64Url(hexToBase64(rsaJson.q)),
dp: b64ToB64Url(hexToBase64(rsaJson.dmp1)),
dq: b64ToB64Url(hexToBase64(rsaJson.dmq1)),
qi: b64ToB64Url(hexToBase64(rsaJson.coeff)),
ext: true,
key_ops: ['decrypt']
};
var algoDec = {
name: 'RSA-OAEP',
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: 'SHA-256' //{name:'SHA-256'}
};
importPrivate = crypto.subtle.importKey('jwk', keyDataDec, algoDec, false, ['decrypt'])
.catch(function(err) {
console.log(err);
});
我发现的错误是 "DOMException",PromiseValue 是 "undefined"。
我 运行 在 Mozilla Firefox 和 Chrome。
这是我的钥匙:
{"e": "10001",
"n":"74a258004dd6c338b27688c1dd13d90da3269321d6dfa3cd8d7cfd7521453d0c5a9828bd507cfc6fe004ddcc09c498d2ceea8e05c214a63ab99d33c9060236f02d5f8bd1e7b334c7f74745948664aeba1a5addf7e7d76911ebf382852aabe86e06f83e0f7669be172380069547f542d8b0848b8bcf53e57c04d5e4163820ced3e4078418efe98df9f8c54c0cda66db3262f20b81464162c44216ca8b63c8f0cfe090dfe1d1950428ad6948204f3f44ba0648de44a9c44d44b91bd8f9ff7cccaceb9f20204f3ba1e228f13249fe04a7fc69cfe57d35e8897e16bc7872f585c909fec9b95a5240ab6589c3ebbe3ad614bfbdc966218daf9d9dddb39fdf6c0d3b49",
"d":"6f715f5c8b0395ed6ab84fdbccfdd5fb6dd4c1e92782f0e4cb388e858890bbd03e0ffd7d4696e62d8f8396da0acb4aaac302e3492534cdd076e27a44adf517c14dd969a3db1c24669d24009bf5fde5e5f4a0b64ed5bbc1ad5a2286a7ef29eb20969043dc4a6ca7313cbe6b8be001bd2987205ea5ec2861b1aa7d253f05ca73543d573578733f5e36db4759732386eb222efc93a61565c2c5453889ead0379c03c5023b488b34ba5aa09a1e3a51dc882a2c7b0e98913c4c89df7bd331785af5d778bc22ef128900d0a39252a900638d8b62ecaa399255e5517dbd787bb9c35ecf8ef9aa129ec3fcaafcbf063c9aa4b7c579862d0f01c60f850fb6edd94484e801",
"p":"c82bd0e1f1fb519a5e9dcae56565bb2fc6d8bb320449c522ef3f624227bf567ffb0e0607cb349b97f0643118aaa91ca6ab05569d8a0b1fa0dac7bfe313409e49743fea4f3050b64bf2c67af0aa44e7510db84caf064d6ccdac8b2f9cd72724f591cebe8d362661460afb90a87baee38f6d218a8a9d07c7ad0af91ec95f8382c1",
"q":"952a004d688ce1fff74e5af5ebc802c8993174aabad8a8da1dc0e4b306f27250f05c70019ab8e2d1f0c1a8178dbaf53d9be68d82334f6b32e36410452b7805d2fb03108d729c2705923c7b7c1ceeceb878fcad02120ae65e478fdbdfc61a1f05e5b4e78d93949bfd04a736966396e73bb95053fd2e6a3f1b9efd2adba916c289",
"dmp1":"9ab9d3b272fb92ae4268b206f383d9572adb288796ecf6e6f40c08859e77b603232a49db0865d7c77057e1e371375b20319de4bfcd8e9f9a14c2c63e5e095ad0e87ce73ee2d7e0b1802d8fa62bd72afe6a1e8597dcc5484f0b0a6379568db971e08cf099f2bb64016247e61f75cb4fb6f3e23356c5ded7a7988dcc1058362d81",
"dmq1":"922563c474224caff0cb699db754f13d3fb34ffe933102472a8dbac59991e3e55f8b0206e064337fc4a6c91dc2b4a1e1aa24fb12cd590bcc01352227d34e364e9fb741f9848870472a1f6e518d3b817906b9727d2bd6581d9979ded0e214e7be6c7091ca8a0539cc6e49b2d7285e4ad29e5f90dae8261b0ce1fa648a71a22619",
"coeff":"9886299d00dcd080956b9f454008cdfaee141bd28938dac2cf0ef43094e36b97fa1821baa9c35b0fdb38d01d39ad64652c123b48fe0197dc1b43f074fbbfdb7349c1ee9cf33026611721e523391abff0f099a4f6fcdb4cc08b97401facbbbaa50390f4315025540d6cc2d734c4c634a1df9490711a16e05fd54d5b24e6cfaa4f"}
不知道哪里出了问题。它适用于导入 public 密钥。
function hexToBase64(str) {
return btoa(String.fromCharCode.apply(null,
str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x ").replace(/ +$/, "").split(" "))
);
};
function b64ToB64Url(base64){
return base64.replace(/\=/g,"").replace(/\+/g,"-").replace(/\//g,"_");
};
Ana,提供更多信息会很有帮助,例如,您尝试导入的示例以及您使用的浏览器 运行。那就是说查看 https://github.com/diafygi/webcrypto-examples,它可能会有所帮助。
也许问题出在从十六进制转换为 base64 的方法中,与此
中描述的类似
您的代码基本上可以工作。 hexToBase64
函数转换 "e": "10001"
时出现问题,因为十六进制表示的指数 10001
应该是 010001
。注意前导零。
您的函数似乎无法正常处理未用两个字符(一个字节)表示的十六进制值
我想通过网络加密导入私钥。这是我的代码:
//import private key
var keyDataDec = {
kty: 'RSA',
alg: 'RSA-OAEP-256',
e: b64ToB64Url(hexToBase64(rsaJson.e)),
n: b64ToB64Url(hexToBase64(rsaJson.n)),
d: b64ToB64Url(hexToBase64(rsaJson.d)),
p: b64ToB64Url(hexToBase64(rsaJson.p)),
q: b64ToB64Url(hexToBase64(rsaJson.q)),
dp: b64ToB64Url(hexToBase64(rsaJson.dmp1)),
dq: b64ToB64Url(hexToBase64(rsaJson.dmq1)),
qi: b64ToB64Url(hexToBase64(rsaJson.coeff)),
ext: true,
key_ops: ['decrypt']
};
var algoDec = {
name: 'RSA-OAEP',
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: 'SHA-256' //{name:'SHA-256'}
};
importPrivate = crypto.subtle.importKey('jwk', keyDataDec, algoDec, false, ['decrypt'])
.catch(function(err) {
console.log(err);
});
我发现的错误是 "DOMException",PromiseValue 是 "undefined"。
我 运行 在 Mozilla Firefox 和 Chrome。 这是我的钥匙:
{"e": "10001",
"n":"74a258004dd6c338b27688c1dd13d90da3269321d6dfa3cd8d7cfd7521453d0c5a9828bd507cfc6fe004ddcc09c498d2ceea8e05c214a63ab99d33c9060236f02d5f8bd1e7b334c7f74745948664aeba1a5addf7e7d76911ebf382852aabe86e06f83e0f7669be172380069547f542d8b0848b8bcf53e57c04d5e4163820ced3e4078418efe98df9f8c54c0cda66db3262f20b81464162c44216ca8b63c8f0cfe090dfe1d1950428ad6948204f3f44ba0648de44a9c44d44b91bd8f9ff7cccaceb9f20204f3ba1e228f13249fe04a7fc69cfe57d35e8897e16bc7872f585c909fec9b95a5240ab6589c3ebbe3ad614bfbdc966218daf9d9dddb39fdf6c0d3b49",
"d":"6f715f5c8b0395ed6ab84fdbccfdd5fb6dd4c1e92782f0e4cb388e858890bbd03e0ffd7d4696e62d8f8396da0acb4aaac302e3492534cdd076e27a44adf517c14dd969a3db1c24669d24009bf5fde5e5f4a0b64ed5bbc1ad5a2286a7ef29eb20969043dc4a6ca7313cbe6b8be001bd2987205ea5ec2861b1aa7d253f05ca73543d573578733f5e36db4759732386eb222efc93a61565c2c5453889ead0379c03c5023b488b34ba5aa09a1e3a51dc882a2c7b0e98913c4c89df7bd331785af5d778bc22ef128900d0a39252a900638d8b62ecaa399255e5517dbd787bb9c35ecf8ef9aa129ec3fcaafcbf063c9aa4b7c579862d0f01c60f850fb6edd94484e801",
"p":"c82bd0e1f1fb519a5e9dcae56565bb2fc6d8bb320449c522ef3f624227bf567ffb0e0607cb349b97f0643118aaa91ca6ab05569d8a0b1fa0dac7bfe313409e49743fea4f3050b64bf2c67af0aa44e7510db84caf064d6ccdac8b2f9cd72724f591cebe8d362661460afb90a87baee38f6d218a8a9d07c7ad0af91ec95f8382c1",
"q":"952a004d688ce1fff74e5af5ebc802c8993174aabad8a8da1dc0e4b306f27250f05c70019ab8e2d1f0c1a8178dbaf53d9be68d82334f6b32e36410452b7805d2fb03108d729c2705923c7b7c1ceeceb878fcad02120ae65e478fdbdfc61a1f05e5b4e78d93949bfd04a736966396e73bb95053fd2e6a3f1b9efd2adba916c289",
"dmp1":"9ab9d3b272fb92ae4268b206f383d9572adb288796ecf6e6f40c08859e77b603232a49db0865d7c77057e1e371375b20319de4bfcd8e9f9a14c2c63e5e095ad0e87ce73ee2d7e0b1802d8fa62bd72afe6a1e8597dcc5484f0b0a6379568db971e08cf099f2bb64016247e61f75cb4fb6f3e23356c5ded7a7988dcc1058362d81",
"dmq1":"922563c474224caff0cb699db754f13d3fb34ffe933102472a8dbac59991e3e55f8b0206e064337fc4a6c91dc2b4a1e1aa24fb12cd590bcc01352227d34e364e9fb741f9848870472a1f6e518d3b817906b9727d2bd6581d9979ded0e214e7be6c7091ca8a0539cc6e49b2d7285e4ad29e5f90dae8261b0ce1fa648a71a22619",
"coeff":"9886299d00dcd080956b9f454008cdfaee141bd28938dac2cf0ef43094e36b97fa1821baa9c35b0fdb38d01d39ad64652c123b48fe0197dc1b43f074fbbfdb7349c1ee9cf33026611721e523391abff0f099a4f6fcdb4cc08b97401facbbbaa50390f4315025540d6cc2d734c4c634a1df9490711a16e05fd54d5b24e6cfaa4f"}
不知道哪里出了问题。它适用于导入 public 密钥。
function hexToBase64(str) {
return btoa(String.fromCharCode.apply(null,
str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x ").replace(/ +$/, "").split(" "))
);
};
function b64ToB64Url(base64){
return base64.replace(/\=/g,"").replace(/\+/g,"-").replace(/\//g,"_");
};
Ana,提供更多信息会很有帮助,例如,您尝试导入的示例以及您使用的浏览器 运行。那就是说查看 https://github.com/diafygi/webcrypto-examples,它可能会有所帮助。
也许问题出在从十六进制转换为 base64 的方法中,与此
您的代码基本上可以工作。 hexToBase64
函数转换 "e": "10001"
时出现问题,因为十六进制表示的指数 10001
应该是 010001
。注意前导零。
您的函数似乎无法正常处理未用两个字符(一个字节)表示的十六进制值