encryptRsaOaepOrThrow
Encrypt a small message with an RSA-OAEP public key.
The key must have been generated or imported with { kind: 'rsa', scheme: 'oaep' }. RSA-OAEP encrypts at most modulus bytes − 2 × hash bytes − 2 per call (190 bytes for a 2048-bit key with SHA-256) — encrypt a symmetric key, not bulk data.
ts
function encryptRsaOaepOrThrow(
publicKey: CryptoKey,
plaintext: Uint8Array,
_: unknown,
): Promise<Uint8Array>Parameters
publicKey:CryptoKey— RSA-OAEP publicCryptoKeywithencryptusageplaintext:Uint8Array— Message bytes, at most the OAEP capacity of the key_:unknown
Throws
Error— If the key is not an RSA-OAEP public encryption key, or the plaintext exceeds the key's OAEP capacity
See also
decryptRsaOaepOrThrowfor the inverse operationencryptRsaOaepfor the Result-returning variant
Examples
ts
const keys = await generateKeyPair({ kind: 'rsa', scheme: 'oaep' });
const ciphertext = await encryptRsaOaepOrThrow(
keys.publicKey,
new TextEncoder().encode('session key'),
);