I am trying to follow the webdocs for subtleCrypto
:
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
To use RSA-OAEP, pass an RsaOaepParams object.
How is the RSA
key supposed to be formatted? The following code is just using plaintext rsaPublicKey
and rsaPrivateKey
: how should they be changed
let rsaPublicKey = "ssh-rsa AAAAB3 ..."
function encrypt(rsaPublicKey, msg) {
let emsg = new TextEncoder().encode(msg)
let encrypted = crypto.subtle.encrypt(
{
name: "RSA-OAEP"
},
rsaPublicKey,
msg
);
return encrypted
}
let rsaPrivateKey = "MIIEvQIBADU ..."
function decrypt(rsaPrivateKey, encrypted) {
return window.crypto.subtle.decrypt(
{
name: "RSA-OAEP"
},
rsaPrivateKey,
encrypted
);
}
Here is the (probably incorrect) code attempting to do the round trip:
let enc = encrypt(rsaKey, "hello world!") // ERROR on this line
console.log(enc)
let dec = decrypt(rsaPrivateKey, enc)
console.log(dec)
The error is:
Uncaught (in promise) TypeError: Failed to execute 'encrypt' on 'SubtleCrypto': parameter 2 is not of type 'CryptoKey'.
So how should the public (and private) keys be encoded/ formatted?