klyn.cryptography.Cipher.rsaEncrypt
method
public static rsaEncrypt(plainText as String, publicKeyPem as String, padding as String = "OAEP", hashAlgorithm as String = "SHA-256", outputEncoding as String = "base64") as String throws CryptoException:
Description

Encrypts text with an RSA public key.

Supported paddings:

  • OAEP (recommended, default)
  • PKCS1

For OAEP, hashAlgorithm controls both the OAEP digest and MGF1 digest.

Parameters
ParameterDescription
plainTextSource text interpreted as UTF-8.
publicKeyPemPEM-encoded public key.
paddingRSA encryption padding. Defaults to OAEP.
hashAlgorithmHash used by OAEP when applicable.
outputEncodingOutput encoding: base64, base64url or hex.
Returns

Encoded ciphertext.

Throws
  • CryptoException when encryption fails.
Example
import klyn.cryptography

keys = RSAKeyPairGenerator().generateKeyPair()
encrypted = Cipher.rsaEncrypt("hello", keys.publicKeyPem)
print(encrypted)