I generated rsa key by jsbn:
{
"e": "10001",
"n": "74a258004dd6c338b27688c1dd13d90da3269321d6dfa3cd8d7cfd7521453d0c5a9828bd507cfc6fe004ddcc09c498d2ceea8e05c214a63ab99d33c9060236f02d5f8bd1e7b334c7f74745948664aeba1a5addf7e7d76911ebf382852aabe86e06f83e0f7669be172380069547f542d8b0848b8bcf53e57c04d5e4163820ced3e4078418efe98df9f8c54c0cda66db3262f20b81464162c44216ca8b63c8f0cfe090dfe1d1950428ad6948204f3f44ba0648de44a9c44d44b91bd8f9ff7cccaceb9f20204f3ba1e228f13249fe04a7fc69cfe57d35e8897e16bc7872f585c909fec9b95a5240ab6589c3ebbe3ad614bfbdc966218daf9d9dddb39fdf6c0d3b49",
...}
"e" is exponent and "n" is the modulus
And I want to import it using crypto.subtle.importKey
.
This is the error that I find:
DOMException: The JWK member "n" could not be base64url decoded or contained padding
Does anybody know where is the problem?
Look at the my code below.
var keyData = {
kty: 'RSA',
e: hexToBase64(rsaJson.e),
n: hexToBase64(rsaJson.n),
alg: 'RSA-OAEP-256',
ext: true
};
var algo = {
name: 'RSA-OAEP',
hash: {name: 'SHA-256'}
};
var importedKey = crypto.subtle.importKey('jwk', keyData, algo, false, ['encrypt']).catch(function(err) {
console.log(err);
});
You need to provide a JWK key with values encoded in base64url which is slightly different from base64. Convert the result of
hexToBase64
to base64url encodingThis code will work
Note also WebCryptographyApi works with promises and is async. I changed
with