> ## Documentation Index
> Fetch the complete documentation index at: https://docsuncv2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# crypt.encrypt

Encrypts a string with AES using the given key and mode.

```luau theme={null}
crypt.encrypt(data: string, key: string, iv: string?, mode: string?): (string, string)
```

`key` is a base64-encoded 256-bit [AES key](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard) (call `crypt.generatekey()` to make one).

`iv` is a base64-encoded 16-byte [initialization vector](https://en.wikipedia.org/wiki/Initialization_vector).

Default `mode` is `"CBC"`. Other accepted modes: `"ECB"`, `"CTR"`, `"CFB"`, `"OFB"`, `"GCM"`. <br /> [Learn more here](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation).

## Parameters

| Parameter | Type      | Description                                   |
| --------- | --------- | --------------------------------------------- |
| `data`    | `string`  | The plaintext to encrypt.                     |
| `key`     | `string`  | Base64-encoded 32-byte AES key.               |
| `iv`      | `string?` | Base64-encoded 16-byte IV. Random if omitted. |
| `mode`    | `string?` | One of the modes. Defaults to `"CBC"`.        |

## Returns

`(string, string)` - a tuple of (ciphertext, IV) where both are base64-encoded. The IV is returned even when the caller provided one, so the value can always be stored or transmitted alongside the ciphertext.

## Example

Cache some sensitive data to disk so the next script run can reuse it without re-fetching.<br />Encrypt it first so anyone who copies or shares the file can't read the value.

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  local encrypt = crypt.encrypt("Hello, world!", "test", "test", "test")
  =======
  -- crypt.encrypt(data: string, key: string, iv: string?, mode: string?): (string, string)
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  -- run crypt.generatekey() once, paste the 44-char result here
  local KEY = "your-base64-key"

  local cipher, iv = crypt.encrypt("LIC-A1B2C3D4-EXP-2026", KEY)
  writefile("license.cache", `{cipher}|{iv}`)

  -- best practice is to do that in a queue_on_teleport call (runs faster than autoexec),
  -- or on your server

  -- because what if someone hooks crypt.encrypt()?
  ```
</CodeGroup>

<Tip>
  Always save the generated IV, as it is required to decrypt the data later.
</Tip>
