Encryption
To protect sensitive data, our API support certain fields to be encrypted before transmission. This page explains how to encrypt data using RSA 2048-bit with OAEP padding and SHA-256.
Last updated
Was this helpful?
Was this helpful?
import { webcrypto } from "crypto";
const { subtle } = webcrypto;
async function encryptData(plaintext, publicKeyPem) {
// Convert PEM to CryptoKey
const binaryDer = Buffer.from(
publicKeyPem.replace(/-----(BEGIN|END) PUBLIC KEY-----/g, ""),
"base64"
);
const key = await subtle.importKey(
"spki",
binaryDer,
{ name: "RSA-OAEP", hash: "SHA-256" },
false,
["encrypt"]
);
// Encrypt
const encoded = new TextEncoder().encode(plaintext);
const ciphertext = await subtle.encrypt({ name: "RSA-OAEP" }, key, encoded);
// Return Base64
return Buffer.from(ciphertext).toString("base64");
}using System.Security.Cryptography;
using System.Text;
public static string EncryptData(string plaintext, string publicKeyPem)
{
using var rsa = RSA.Create();
rsa.ImportFromPem(publicKeyPem);
byte[] data = Encoding.UTF8.GetBytes(plaintext);
byte[] encrypted = rsa.Encrypt(
data,
RSAEncryptionPadding.OaepSHA256
);
return Convert.ToBase64String(encrypted);
}