Skip to content

💭 TypeError: Key for the RSA-OAEP-256 algorithm must be one of type CryptoKey... when using jose + react-native-quick-crypto in Expo app #683

@leleofg

Description

@leleofg

Hi there! 👋

I'm trying to use jose along with react-native-quick-crypto to encrypt a string using a JWK received from my backend.

Environment

  • Expo (Managed Workflow) with Expo Dev Client
  • react-native-quick-crypto installed via:
    expo install react-native-quick-crypto
  • Native build generated using eas build
  • At the entry point (App.js), I’m calling:
    import { install } from 'react-native-quick-crypto';
    install();

Code

import { CompactEncrypt, importJWK } from 'jose';

const ALG = 'RSA-OAEP-256';

export async function encryptValue(value: string): Promise<string> {
  try {
    const response = await fetch('https://example.com/jwks.json');
    const { keys } = await response.json();

    const publicKey = await importJWK(keys[0]); // <- this returns a CryptoKey

    const plaintext = new TextEncoder().encode(value);

    const encryptedValue = await new CompactEncrypt(plaintext)
      .setProtectedHeader({ alg: ALG, enc: 'A256GCM' })
      .encrypt(publicKey); // <- this throws an error

    return encryptedValue;
  } catch (err) {
    console.error('Error encrypting value:', err);
    return '';
  }
}

Error

TypeError: Key for the RSA-OAEP-256 algorithm must be one of type CryptoKey, KeyObject, or JSON Web Key. Received an instance of CryptoKey

It seems like importJWK() is returning a CryptoKey, but CompactEncrypt().encrypt() is rejecting it.

What I’ve tried

  • Confirmed that react-native-quick-crypto is installed and initialized correctly
  • Verified the JWK is valid and works fine in a Node.js environment
  • Tried using importJWK(jwk, ALG) explicitly — same result

Question

Is this a compatibility issue between jose and react-native-quick-crypto in a React Native + Expo environment?
Should I be using a different key format or conversion?

Any help would be greatly appreciated! 🙏

What I tried

QuickCrypto Version

0.7.13

Additional information

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions