encrypt

fun String.encrypt(key: KEncryptKeyType, salt: String? = null): String(source)

Encrypts a String and returns a Base64-encoded ciphertext.

Classical (AES-GCM) — no salt

val cipher = "Hello Kindling!".encrypt(
key = KEncryptKeyType.Classical("my-secret")
)

Classical with salt

val cipher = "Hello Kindling!".encrypt(
key = KEncryptKeyType.Classical("my-secret", KHashAlgorithm.SHA256),
salt = "random-salt"
)

Post-quantum (Kyber-1024)

val pair   = KEncryptKeyPair.generate()
val cipher = "Hello Kindling!".encrypt(
key = KEncryptKeyType.PostQuantum(publicKeyBase64 = pair.publicKeyBase64)
)

Return

Base64-encoded ciphertext (includes IV/nonce prepended).

Parameters

key

The key type and material to use.

salt

Optional salt string mixed into the key derivation (classical only).


Low-level ByteArray encryption. Returns raw ciphertext bytes.

Prefer the String.encrypt extension for everyday use.