You have a JWK (JSON Web Key) for an RSA public key, which is typically used for encrypting data (e.g., with RSA-OAEP). You want to use this key to encrypt a message on Android using C++.
Here’s a step-by-step guide:
1. Extract the Key Parameters
From your JWK:
"n": The modulus (base64url-encoded)
"e": The exponent (base64url-encoded)
You need to decode these and use them to construct an RSA public key.
2. Base64url Decode
Android C++ (NDK) does not have built-in base64url decoding. You can use a library like cppcodec or write your own.
Example (pseudo-code):
#include <cppcodec/base64_url_unpadded.hpp>
using base64url = cppcodec::base64_url_unpadded;
std::vector<uint8_t> n_bytes = base64url::decode("qH2KwwF8sG91CCyu-FvWYPpNgFzmQlOt6NgIDkxLZ0RlGxWnVi0TPd3C1wmua1nYqm54sDrlBmZjMn3VIvboJFo4m2qqKXaLv-AwMy0_QxUuMTCvyExd_632W-uPGq2KgFM1a0-L-akCNNUJGlulOHUxeORg_G34kZSXPfxBa_M4YcPxiJSuI35FLSIfCplDi2qPN0cqHNvLcmdEKgb9DbXqwNhHhhSK5yWLBZlTKyLhIU0HewUJWG-J5GdUZv7dnUzE5Cl2SF4u91LKCLw3nSWoHrHwE-CGoNFNLC3ScBJbkWbLeVeFXNx5YTf2pUR4bVHTrglBwHtMuGq0NDnDiQ");
std::vector<uint8_t> e_bytes = base64url::decode("AQAB");
3. Construct the RSA Public Key
You need a crypto library. OpenSSL is the most common and works on Android NDK.
Example:
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/bn.h>
// Convert n_bytes and e_bytes to BIGNUM
BIGNUM* n = BN_bin2bn(n_bytes.data(), n_bytes.size(), NULL);
BIGNUM* e = BN_bin2bn(e_bytes.data(), e_bytes.size(), NULL);
// Create RSA key
RSA* rsa = RSA_new();
RSA_set0_key(rsa, n, e, NULL); // RSA takes ownership of n and e
4. Encrypt the Message
For "alg": "RSA-OAEP-256", you need to use RSA-OAEP with SHA-256.
Example:
#include <openssl/evp.h>
EVP_PKEY* pkey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(pkey, rsa);
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(pkey, NULL);
EVP_PKEY_encrypt_init(ctx);
EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING);
EVP_PKEY_CTX_set_rsa_oaep_md(ctx, EVP_sha256());
size_t outlen;
EVP_PKEY_encrypt(ctx, NULL, &outlen, (const unsigned char*)plaintext, plaintext_len);
std::vector<unsigned char> ciphertext(outlen);
EVP_PKEY_encrypt(ctx, ciphertext.data(), &outlen, (const unsigned char*)plaintext, plaintext_len);
// ciphertext now contains the encrypted data
5. Build for Android
- Add OpenSSL to your Android NDK project (e.g., via openssl-android).
- Link your C++ code with OpenSSL.
- Use JNI to call your C++ encryption function from Java/Kotlin if needed.
6. Full Example (Simplified)
// 1. Decode n and e from base64url
// 2. Construct RSA key as above
// 3. Encrypt using RSA-OAEP-256 as above
// Don't forget to free resources:
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(pkey);
// RSA is freed by EVP_PKEY_free
7. Notes
- For
"enc": "A256GCM", this is for the content encryption (AES-GCM). Usually, you encrypt a random AES key with RSA, then use that AES key to encrypt your message.
- The above only covers the RSA part (encrypting the AES key).
- For full JWE (JSON Web Encryption) support, use a library like nlohmann/jose or jwt-cpp, but these may not support all JWE features out of the box.
Summary
- Decode the modulus and exponent from base64url.
- Construct an RSA public key using OpenSSL.
- Encrypt your data using RSA-OAEP-256.
- For full JWE, you need to also handle AES-GCM encryption of your message.
If you want a full working code snippet for a specific step, let me know!