> For the complete documentation index, see [llms.txt](https://docs.envector.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.envector.io/1.4.x/api-reference/cipher.md).

# Cipher

## module `cipher`

Cipher Module

This module provides encryption and decryption functionalities for vectors and scores using the pyenvector/enVector framework.

* `Cipher` — Handles encryption and decryption operations.

### class `Cipher`

```python
class Cipher()
```

Cipher class for handling encryption and decryption operations.

***

#### `encrypt()`

```python
def encrypt(vector,
            encode_type: str = "item",
            enc_key_path: Optional[str] = None,
            enc_key: Optional[str] = None,
            centroids_idx: Optional[Sequence[int]] = None,
            use_row_encrypt: bool = False,
            split_batch_size: int = 4096)
```

Encrypts one or multiple vectors.

**ARGUMENTS**

* `vector` (`Union[list[float], list[list[float]], list[np.ndarray], np.ndarray]`): Input vector(s) to encrypt.
* 1D list of floats or 1-D `np.ndarray`: single vector.
* 2D `np.ndarray` of shape `(N, dim)`: batch of N vectors.
* list of lists or list of 1-D `np.ndarray`: batch of N vectors.
* `encode_type` (`str`): Encoding type. `"item"` (default) or `"query"`. When `"query"`, delegates to :meth:`encrypt_query` and returns a single :class:`CipherBlock`; batch input is rejected.
* `enc_key_path` (`str`): Path to the encryption key file.
* `enc_key` (`Union[str, bytes]`): Raw key bytes or serialized key string.
* `centroids_idx` (`Sequence[int]`): Centroid IDs for each input vector, stored in the returned :class:`CipherBlock` for IVF insert.
* `use_row_encrypt` (`bool`): If `True`, uses :meth:`encrypt_row` instead of :meth:`encrypt_multiple`.
* `split_batch_size` (`int`): Maximum number of vectors per :class:`CipherBlock`. When the total exceeds this value the result is a `list` of :class:`CipherBlock` objects. Defaults to `4096`.

**RETURNS**

`Union[CipherBlock, list[CipherBlock]]`: A single :class:`CipherBlock` for single-vector input or when the total vector count does not exceed *split\_batch\_size*. A `list` of :class:`CipherBlock` when the batch is split.

***

#### `encrypt_query()`

```python
def encrypt_query(vector,
                  enc_key_path: Optional[str] = None,
                  enc_key: Optional[str] = None)
```

Encrypts a single query vector.

***

#### `encrypt_multiple()`

```python
def encrypt_multiple(vectors,
                     encode_type: str = "item",
                     enc_key_path: Optional[str] = None,
                     enc_key: Optional[str] = None,
                     centroids_idx: Optional[Sequence[int]] = None)
```

Encrypts multiple vectors.

**ARGUMENTS**

* `vectors` (`Sequence[Union[list, np.ndarray]]`): The vectors to encrypt.
* `encode_type` (`str`): The encoding type used during encryption. Only `"item"` is supported.
* `enc_key_path` (`str`): Path to the encryption key file.
* `enc_key` (`Union[str, bytes]`): Raw key bytes or serialized key string to use instead of loading from disk.

**RETURNS**

`CipherBlock`: Batched encrypted vectors.

***

#### `encrypt_row()`

```python
def encrypt_row(vectors,
                encode_type: str = "item",
                enc_key_path: Optional[str] = None,
                enc_key: Optional[str] = None,
                centroids_idx: Optional[Sequence[int]] = None)
```

Encrypts multiple vectors.

**ARGUMENTS**

* `vectors` (`Sequence[Union[list, np.ndarray]]`): The vectors to encrypt.
* `encode_type` (`str`): The encoding type used during encryption. Only `"item"` is supported.
* `enc_key_path` (`str`): Path to the encryption key file.
* `enc_key` (`Union[str, bytes]`): Raw key bytes or serialized key string to use instead of loading from disk.

**RETURNS**

`CipherBlock`: Batched encrypted vectors.

***

#### `decrypt()`

```python
def decrypt(encrypted_vector,
            sec_key_path: Optional[str] = None,
            idx: int = 0,
            seal_mode: Optional[str] = None,
            seal_kek_path: Optional[str] = None,
            seal_kek: Optional[str] = None,
            sec_key: Optional[str] = None)
```

Decrypts an encrypted vector.

**PARAMETERS**

* `encrypted_vector` (CipherBlock): The vector to be decrypted.
* `sec_key_path` (str, optional): The path to the secret key file for decryption (defaults to the configured path).
* `idx` (int, optional): The index of the vector to decrypt when the ciphertext contains multiple vectors.
* `seal_mode` (str, optional): Seal mode used for the secret key.
* `seal_kek_path` (str, optional): Path to the KEK file if the key is sealed.
* `seal_kek` (Union\[str, bytes], optional): Raw KEK bytes or string to unseal the key.
* `sec_key` (Union\[str, bytes], optional): Raw secret-key bytes/string.

**RETURNS**

`list` : Decrypted vector.

**EXAMPLES**

```pycon
>>> cipher = Cipher(dim=512, enc_key_path="./temp/keys/EncKey.bin")
>>> enc_vec = cipher.encrypt([0.0] * 512, "item")
>>> dec_vec = cipher.decrypt(enc_vec, sec_key_path="./temp/keys/SecKey.bin")
```

***

#### `decrypt_score()`

```python
def decrypt_score(encrypted_score,
                  sec_key_path: Optional[str] = None,
                  sec_key: Optional[str] = None,
                  seal_mode: Optional[str] = None,
                  seal_kek_path: Optional[str] = None,
                  seal_kek: Optional[str] = None)
```

Decrypts an encrypted score.

**ARGUMENTS**

* `encrypted_score` (`CipherBlock`): The score to be decrypted.
* `sec_key_path` (`str`): The path to the secret key file.
* `sec_key` (`Union[str, bytes]`): Raw secret-key bytes or serialized string.
* `seal_mode` (`str`): Seal mode used for the secret key.
* `seal_kek_path` (`str`): Path to the KEK when unsealing the key.
* `seal_kek` (`Union[str, bytes]`): Raw KEK bytes/string when available.

**RETURNS**

`list` : Decrypted score.

***

#### `encryptor()`

```python
@property
def encryptor()
```

Returns the encryptor object.

**RETURNS**

`Encryptor`: The encryptor object for encryption operations.

***

#### `decryptor()`

```python
@property
def decryptor()
```

Returns the decryptor object.

**RETURNS**

`Decryptor`: The decryptor object for decryption operations.

***

#### `sec_key_path()`

```python
@property
def sec_key_path()
```

Returns the path to the secret key file.

**RETURNS**

`str`: The path to the secret key file used for decryption.

***

#### `sec_key()`

```python
@property
def sec_key()
```

Returns the configured secret key source (path or bytes).

**RETURNS**

`Union[str, bytes]`: The secret key material used for decryption.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.envector.io/1.4.x/api-reference/cipher.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
