> 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/sdk-user-guide/advanced-user-guide/cipher/encryption.md).

# Encryption

The core of the cryptographic functionality is the `cipher.encrypt()` method. This method takes a plaintext numerical vector and converts it into an encrypted `CipherBlock` object, which can then be securely stored or used for searching.

The most critical parameter in this process is the `encoding_type`, which tells the SDK how to prepare the vector for its specific purpose.

***

## Encoding Type

When encrypting data, you must specify its role by setting the `encoding_type` parameter.\
This determines how the data will be processed and ensures it is encrypted in the correct format for its intended purpose.

There are two possible values:

* `"item"`: Choose this when encrypting a vector that represents a record to be inserted and stored in the index on the server.\
  Use this for your dataset entries that will be part of the searchable index.
* `"query"`: Choose this when encrypting a vector that will be used as a search input against the stored index.\
  Use this for the vector you want to look up or compare against the dataset.

***

#### Usage and Examples

Below are examples demonstrating how to use `cipher.encrypt()` for both index entries and search queries.

#### 1. Encrypting an Index Entry

When you want to add a new encrypted vector to your index, you must set `encode_type="item"`.

```python
from pyenvector import Cipher

# Initialize the Cipher class with the FHE context
# (No keys needed here if providing them on-the-fly)
cipher = Cipher(dim=512)

# This is the vector you want to insert into the index
item_vector = [0.1, 0.2, 0.3] * (512 // 3) + [0.0] * (512 % 3)

# Encrypt the vector for index insertion
# The result is a CipherBlock object ready to be stored
encrypted_item = cipher.encrypt(
    item_vector, 
    encode_type="item", 
    enc_key_path="keys/example/EncKey.json"
)

# Now you can insert 'encrypted_item' into your index
# index.insert(encrypted_item)
```

#### 2. Encrypting a Search Query

When you want to search for a vector in your index, use `cipher.encrypt_query()` to encrypt the search vector.

```python
from pyenvector import Cipher

# Initialize the Cipher class
cipher = Cipher(dim=512)

# This is the vector you want to search with
query_vector = [0.9, 0.8, 0.7] * (512 // 3) + [0.0] * (512 % 3)

# Encrypt the vector for querying
encrypted_query = cipher.encrypt_query(
    query_vector,
    enc_key_path="keys/example/EncKey.json",
)

# Now you can use 'encrypted_query' to perform a search
# search_results = index.search(encrypted_query)
```

***

## Bulk Encryption with `encrypt_multiple`

For situations where you need to encrypt many vectors at once, such as during a bulk index insertion, the SDK provides the highly efficient `cipher.encrypt_multiple()` method. Using this method is significantly faster than calling `cipher.encrypt()` in a loop.

`encrypt_multiple()` is specifically designed for preparing large batches of data for storage. Because of this, it only supports the `"item"` encoding type. You must always set `encoding_type="item"` when using this method.

***

#### Usage and Example

The method takes a list of vectors and returns a `CipherBlock` object.

**Encrypting a Batch of Index Entries**

When you need to insert a large number of vectors into your index, prepare them in a list and pass them to `encrypt_multiple`.

```python
from pyenvector import Cipher

# Initialize the Cipher class
cipher = Cipher(dim=512)

# Create a list of vectors that you want to bulk insert
list_of_item_vectors = [
    [0.1, 0.2, 0.3] * (512 // 3) + [0.0] * (512 % 3),  # Vector 1
    [0.4, 0.5, 0.6] * (512 // 3) + [0.0] * (512 % 3),  # Vector 2
    [0.7, 0.8, 0.9] * (512 // 3) + [0.0] * (512 % 3)   # Vector 3
]

# Encrypt the entire list of vectors in a single, efficient operation
encrypted_items = cipher.encrypt_multiple(
    list_of_item_vectors, 
    encode_type="item",
    enc_key_path="keys/example/EncKey.json",
)

# The result is a CipherBlock object, ready for a bulk insert operation
# index.insert(encrypted_items) ✅
```


---

# 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/sdk-user-guide/advanced-user-guide/cipher/encryption.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.
