> ## 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 のバイトを Base64 からデコードされた key で AES CBC、ECB、または CTR を使って暗号化し、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>
    Lua string から読み取られる plaintext バイト。
  </ParamField>

  <ParamField path={"key"} type={"string"} required>
    AES に渡される前にデコードされる Base64 key。
  </ParamField>

  <ParamField path={"iv"} type={"string"}>
    CBC または CTR 用の任意の Base64 IV。CBC または CTR で省略した場合、新しい AES block size の IV が生成されます。
  </ParamField>

  <ParamField path={"mode"} type={"\"CBC\" | \"ECB\" | \"CTR\" | \"AES-CBC\" | \"AES-ECB\" | \"AES-CTR\""}>
    AES mode。存在する場合は AES- prefix が削除されます。デフォルトは 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"}>
    CBC と CTR では Base64-encoded IV、ECB では nil。
  </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>
