Skip to content

Asymmetric

The /asymmetric endpoint performs RSA asymmetric key generation, encryption, and decryption.

Parameters

The request body must be JSON:

ParameterRequiredDescription
actionYesAction to perform: keygen, encrypt, or decrypt
textCond.Text to encrypt, or base64 ciphertext to decrypt
publicKeyCond.PEM public key (required for encrypt)
privateKeyCond.PEM private key (required for decrypt)
modulusLengthNoRSA key size for keygen: 2048 (default) or 4096. Ignored for encrypt/decrypt — the size limit is derived from the actual key
algorithmNoPadding algorithm: rsa-oaep-sha256 (default) or rsa-oaep-sha1

Available Actions

ActionDescription
keygenGenerates an RSA key pair and returns both PEM-encoded public and private keys
encryptEncrypts text with the public key using OAEP padding and returns the result as base64
decryptDecrypts a base64 ciphertext with the private key and returns the original plaintext

Plaintext Size Limit

OAEP padding reserves space inside each RSA block, so the maximum plaintext size depends on the key size and hash algorithm:

Key SizeAlgorithmMax Plaintext
2048rsa-oaep-sha256190 bytes
2048rsa-oaep-sha1214 bytes
4096rsa-oaep-sha256446 bytes
4096rsa-oaep-sha1470 bytes

The formula is: modulusLength / 8 − 2 × hashLength − 2.

Code Examples

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "action": "keygen"
  }' \
  "https://api.sylvain.sh/v5/asymmetric"

Response Fields

keygen action:

FieldTypeDescription
actionstringkeygen
algorithmstringPadding algorithm used
modulusLengthnumberRSA key size in bits
publicKeystringPEM-encoded public key (SPKI)
privateKeystringPEM-encoded private key (PKCS#8)

encrypt / decrypt action:

FieldTypeDescription
actionstringAction performed (encrypt or decrypt)
algorithmstringPadding algorithm used
resultstringBase64 ciphertext, or decrypted plaintext

Try It

Error Handling

If parameters are missing or invalid, the API will return an error:

Error MessageDescription
Action must be "keygen", "encrypt", or "decrypt"The action value is not valid
Unsupported algorithm. Use one of: rsa-oaep-sha256, rsa-oaep-sha1The algorithm value is not supported
Modulus length must be 2048 or 4096The modulusLength is not 2048 or 4096
Text is requiredThe text parameter is missing for encrypt
Public key is requiredThe publicKey is missing for encrypt
Invalid public key formatThe publicKey does not start with the PEM header, or cannot be parsed as a valid key
Text exceeds maximum length for this key size and algorithm (N bytes)The plaintext is too large for the key/algorithm
Encryption failed: invalid key or data too largeThe encryption failed
Encrypted data is requiredThe text parameter is missing for decrypt
Private key is requiredThe privateKey is missing for decrypt
Invalid private key formatThe privateKey does not start with the PEM header
Invalid key or corrupted dataThe decryption failed