> ## 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.
  </ParamField>

  <ParamField path={"mode"} type={"\"CBC\" | \"ECB\" | \"CTR\" | \"AES-CBC\" | \"AES-ECB\" | \"AES-CTR\""}>
    AES mode. تتم إزالة بادئة 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>
