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

> Szyfruje bajty Lua string za pomoca AES CBC, ECB albo CTR z key zdekodowanym z Base64, a nastepnie zwraca Base64 ciphertext i wynik 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
  ```

  ## Argumenty

  <ParamField path={"data"} type={"string"} required>
    Bajty plaintext odczytane z Lua string.
  </ParamField>

  <ParamField path={"key"} type={"string"} required>
    Base64 key dekodowany przed przekazaniem do AES.
  </ParamField>

  <ParamField path={"iv"} type={"string"}>
    Opcjonalny Base64 IV dla CBC albo CTR. Gdy zostanie pominiety dla CBC albo CTR, generowany jest nowy IV o AES block size.
  </ParamField>

  <ParamField path={"mode"} type={"\"CBC\" | \"ECB\" | \"CTR\" | \"AES-CBC\" | \"AES-ECB\" | \"AES-CTR\""}>
    AES mode. Prefix AES- jest usuwany, gdy wystepuje. Wartosc domyslna to AES-CBC. CBC i ECB uzywaja crypto library DEFAULT\_PADDING; CTR nie uzywa padding.
  </ParamField>

  ## Zwroty

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

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

  ## Przykład

  Zaszyfruj Lua string i odszyfruj zwrocony ciphertext za pomoca zwroconego 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>
