import JSEncrypt from "jsencrypt";

/**
 * RSA-encrypt a string using the given PEM public key.
 * Uses PKCS1v1.5 padding to match the backend `rsa::Pkcs1v15Encrypt`.
 */
export function rsaEncrypt(plaintext: string, publicKey: string): string {
    const enc = new JSEncrypt();
    enc.setPublicKey(publicKey);
    const encrypted = enc.encrypt(plaintext);
    if (!encrypted) {
        throw new Error("RSA encryption failed — check public key format");
    }
    return encrypted;
}
