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

> Ma hoa cac byte cua Lua string bang AES CBC, ECB hoac CTR voi key da decode tu Base64, sau do tra ve Base64 ciphertext va ket qua 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
  ```

  ## Đối số

  <ParamField path={"data"} type={"string"} required>
    Cac byte plaintext duoc doc tu Lua string.
  </ParamField>

  <ParamField path={"key"} type={"string"} required>
    Base64 key duoc decode truoc khi truyen vao AES.
  </ParamField>

  <ParamField path={"iv"} type={"string"}>
    Base64 IV optional cho CBC hoac CTR. Khi bo qua voi CBC hoac CTR, mot IV moi co AES block size se duoc tao.
  </ParamField>

  <ParamField path={"mode"} type={"\"CBC\" | \"ECB\" | \"CTR\" | \"AES-CBC\" | \"AES-ECB\" | \"AES-CTR\""}>
    AES mode. Prefix AES- se bi xoa khi co. Mac dinh la AES-CBC. CBC va ECB dung crypto library DEFAULT\_PADDING; CTR khong dung padding.
  </ParamField>

  ## Giá trị trả về

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

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

  ## Ví dụ

  Ma hoa mot Lua string va giai ma ciphertext duoc tra ve bang IV duoc tra ve.

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