> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# crypt.encrypt

> Chiffre les octets d une chaine Lua avec AES CBC, ECB ou CTR avec une key decodee depuis Base64, puis renvoie Base64 ciphertext et le resultat IV.

<div className="real-function-page">
  ```lua theme={null}
  crypt.encrypt(data: string, key: string, iv?: string, mode?: "CBC" | "ECB" | "CTR" | "AES-CBC" | "AES-ECB" | "AES-CTR"): string | string | nil
  ```

  ## Arguments

  <ParamField path={"data"} type={"string"} required>
    Octets plaintext lus depuis une chaine Lua.
  </ParamField>

  <ParamField path={"key"} type={"string"} required>
    Base64 key decodee avant d etre transmise a AES.
  </ParamField>

  <ParamField path={"iv"} type={"string"}>
    Base64 IV optionnel pour CBC ou CTR. S il est omis avec CBC ou CTR, un nouvel IV de taille AES block size est genere.
  </ParamField>

  <ParamField path={"mode"} type={"\"CBC\" | \"ECB\" | \"CTR\" | \"AES-CBC\" | \"AES-ECB\" | \"AES-CTR\""}>
    AES mode. Le prefixe AES- est supprime quand il est present. La valeur par defaut est AES-CBC. CBC et ECB utilisent crypto library DEFAULT\_PADDING; CTR n utilise pas de padding.
  </ParamField>

  ## Retours

  <ResponseField name={"ciphertext"} type={"string"}>
    Base64-encoded ciphertext.
  </ResponseField>

  <ResponseField name={"initializationVector"} type={"string | nil"}>
    Base64-encoded IV pour CBC et CTR; nil pour ECB.
  </ResponseField>

  ## Exemple

  Chiffre une chaine Lua et dechiffre le ciphertext renvoye avec le IV renvoye.

  ```lua theme={null}
  local key = crypt.generatekey()
  local ciphertext, iv = crypt.encrypt("hello world", key)

  local plaintext = crypt.decrypt(ciphertext, key, iv)

  print(ciphertext)
  print(iv)
  print(plaintext)
  ```
</div>
