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

> Mengenkripsi byte Lua string dengan AES CBC, ECB, atau CTR memakai key yang didekode dari Base64, lalu mengembalikan Base64 ciphertext dan hasil 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
  ```

  ## Argumen

  <ParamField path={"data"} type={"string"} required>
    Byte plaintext yang dibaca dari Lua string.
  </ParamField>

  <ParamField path={"key"} type={"string"} required>
    Base64 key yang didekode sebelum diteruskan ke AES.
  </ParamField>

  <ParamField path={"iv"} type={"string"}>
    Base64 IV opsional untuk CBC atau CTR. Jika dihilangkan untuk CBC atau CTR, IV baru sebesar AES block size dibuat.
  </ParamField>

  <ParamField path={"mode"} type={"\"CBC\" | \"ECB\" | \"CTR\" | \"AES-CBC\" | \"AES-ECB\" | \"AES-CTR\""}>
    AES mode. Prefix AES- dihapus jika ada. Default-nya AES-CBC. CBC dan ECB memakai crypto library DEFAULT\_PADDING; CTR tidak memakai padding.
  </ParamField>

  ## Return

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

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

  ## Contoh

  Enkripsi Lua string dan dekripsi ciphertext yang dikembalikan memakai IV yang dikembalikan.

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