klyn.cryptography.Cipher.aesEncrypt
method
public static aesEncrypt(plainText as String, key as String, mode as String = "GCM", keyEncoding as String = "base64", iv as String = null, ivEncoding as String = "base64", aad as String = null) as AESCipherText throws CryptoException:
Description
Encrypts text with AES. Supported modes are `GCM`, `CBC` and `CTR`. Defaults are optimized for the common case: - `mode = "GCM"` - `keyEncoding = "base64"` so it works directly with `KeyGenerator` - random IV generation when `iv` is omitted
import klyn.cryptography

key = AESKeyGenerator().generateKey()
payload = Cipher.aesEncrypt("hello", key)
print(payload.cipherTextBase64)
Parameters
  • plainText Source text interpreted as UTF-8.
  • key AES key material.
  • mode AES mode: `GCM`, `CBC` or `CTR`.
  • keyEncoding Key encoding: `base64`, `base64url`, `hex` or `utf8`.
  • iv Optional IV/nonce.
  • ivEncoding IV encoding when `iv` is provided.
  • aad Optional additional authenticated data for `GCM`.
Returns
Structured AES payload.
Throws
  • CryptoException when the inputs are invalid. ```klyn import klyn.cryptography key = AESKeyGenerator().generateKey() payload = Cipher.aesEncrypt("hello", key) print(payload.cipherTextBase64) ```