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

> Шифрует байты Lua string через AES CBC, ECB или CTR с key, декодированным из Base64, затем возвращает Base64 ciphertext и результат 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
  ```

  ## Аргументы

  <ParamField path={"data"} type={"string"} required>
    Байты plaintext, считанные из Lua string.
  </ParamField>

  <ParamField path={"key"} type={"string"} required>
    Base64 key, декодируемый перед передачей в AES.
  </ParamField>

  <ParamField path={"iv"} type={"string"}>
    Необязательный Base64 IV для CBC или CTR. Если он опущен для CBC или CTR, генерируется новый IV размера AES block size.
  </ParamField>

  <ParamField path={"mode"} type={"\"CBC\" | \"ECB\" | \"CTR\" | \"AES-CBC\" | \"AES-ECB\" | \"AES-CTR\""}>
    AES mode. Prefix AES- удаляется, если он присутствует. Значение по умолчанию AES-CBC. CBC и ECB используют crypto library DEFAULT\_PADDING; CTR не использует padding.
  </ParamField>

  ## Возвраты

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

  <ResponseField name={"initializationVector"} type={"string | nil"}>
    Base64-encoded IV для CBC и CTR; nil для ECB.
  </ResponseField>

  ## Пример

  Зашифруйте Lua string и расшифруйте возвращенный ciphertext с возвращенным IV.

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