> ## 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

> Criptografa bytes de uma string Lua com AES CBC, ECB ou CTR usando uma key decodificada de Base64, depois retorna Base64 ciphertext e o resultado 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
  ```

  ## Argumentos

  <ParamField path={"data"} type={"string"} required>
    Bytes plaintext lidos de uma string Lua.
  </ParamField>

  <ParamField path={"key"} type={"string"} required>
    Base64 key decodificada antes de ser passada para AES.
  </ParamField>

  <ParamField path={"iv"} type={"string"}>
    Base64 IV opcional para CBC ou CTR. Quando omitido para CBC ou CTR, um novo IV de AES block size e gerado.
  </ParamField>

  <ParamField path={"mode"} type={"\"CBC\" | \"ECB\" | \"CTR\" | \"AES-CBC\" | \"AES-ECB\" | \"AES-CTR\""}>
    AES mode. O prefixo AES- e removido quando presente. O padrao e AES-CBC. CBC e ECB usam crypto library DEFAULT\_PADDING; CTR nao usa padding.
  </ParamField>

  ## Retornos

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

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

  ## Exemplo

  Criptografe uma string Lua e descriptografe o ciphertext retornado com o IV retornado.

  ```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>
