> ## 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을 암호화하고 반환된 IV로 반환된 ciphertext를 복호화합니다.

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