> 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.md).

# Cipher

The enVector Python SDK provides the `Cipher` class as the primary interface for handling Fully Homomorphic Encryption (FHE) related features. This class enables you to perform core cryptographic operations, including the [encryption](/1.4.x/sdk-user-guide/advanced-user-guide/cipher/encryption.md) of data into ciphertext and its subsequent [decryption](/1.4.x/sdk-user-guide/advanced-user-guide/cipher/decryption.md) back to plaintext.

Additionally, all encrypted data is managed through the specialized CipherBlock class, which provides a structured way to handle and interact with ciphertexts within the SDK.

***

### Initialize Cipher Class

To perform encryption or decryption, you must first initialize an instance of the `Cipher` class. The constructor configures the FHE (Fully Homomorphic Encryption) context and loads the necessary keys for the operations you intend to perform.

You can flexibly initialize the class for encryption only, decryption only, or both, depending on which key paths you provide.

```python
from pyenvector import Cipher

# Example: Initialize the Cipher for both encryption and decryption
cipher = Cipher(
    enc_key_path="keys/EncKey.json",
    sec_key_path="keys/SecKey.json",
    preset="ip2",
    dim=512,
    seal_mode=None,
    seal_kek_path=None,
)
```

***

#### Parameters

* `enc_key_path` (str, optional): Path to the encryption key file.\
  You can specify this path when initializing the Cipher, or provide it directly in[ `cipher.encrypt()`](/1.4.x/sdk-user-guide/advanced-user-guide/cipher/encryption.md) if you prefer not to store the path within the class.
* `sec_key_path` (str, optional): Path to the Secret key file.\
  You can specify this path when initializing the Cipher, or provide it directly in [`cipher.decrypt()`](/1.4.x/sdk-user-guide/advanced-user-guide/cipher/decryption.md) if you prefer not to store the path within the class.
* `preset` (str, optional, default: `"ip2"`): A pre-configured set of cryptographic parameters that simplifies the FHE context setup. See [Presets](/1.4.x/sdk-user-guide/initialize/index-configuration/2.-presets.md) for the full table.
* `dim` (int): Required. The dimension of the data vectors you will be working with. This value is critical for setting up the FHE context and must be between 32 and 4096.
* `eval_mode` (str, optional, default: `"mm32"`): The FHE evaluation mode used on the server. See [Presets](/1.4.x/sdk-user-guide/initialize/index-configuration/2.-presets.md) for the full table.
* `seal_mode` (str, optional): An advanced option for securely loading the secret key. Use this if your secret key is "sealed" or encrypted.
* `seal_kek_path` (str, optional): The path to the Key Encryption Key (KEK) required when using a `seal_mode`. This parameter is only necessary if `seal_mode` is active.

***

Once you initialize Cipher, you can encrypt plaintext vector to CipherBlock.

The SDK offers two flexible ways to provide the required encryption and decryption keys:

1. Pre-loading Keys: You can provide key paths when you initialize the `Cipher` object. This is convenient when you plan to use the same key for multiple operations.
2. On-the-fly: You can initialize the `Cipher` object without any keys and then provide the key path directly to the `encrypt()` or `decrypt()` method. This is ideal if you need to manage multiple keys with a single `Cipher`instance.

***

#### Method 1: Pre-loading Keys at Initialization

Provide the `enc_key_path` and/or `sec_key_path` arguments when creating the `Cipher` object. The object will then "hold on" to these keys for all subsequent method calls.

**Example**

```python
from pyenvector import Cipher

# Initialize the Cipher with pre-loaded keys
cipher = Cipher(
    dim=1024,
    enc_key_path="keys/user_A/EncKey.json",
    sec_key_path="keys/user_A/SecKey.json"
)
encode_type="item" # this determines db type encoding and query type encoding
# Now, encrypt() and decrypt() use the keys provided during initialization
encrypted_data = cipher.encrypt(my_data, encode_type=encode_type)
decrypted_data = cipher.decrypt(encrypted_data)
```

***

#### Method 2: Providing Keys Per-Operation

Initialize the `Cipher` object with only the context parameters (like `dim`). Then, pass the key path as an argument to each `encrypt()` or `decrypt()` call.

**Example**

```python
from pyenvector import Cipher

# Initialize a general-purpose Cipher object without specific keys
cipher = Cipher(dim=1024)

# Provide the key path directly to the method
encrypted_data = cipher.encrypt(data_A, encode_type=encode_type, enc_key_path="keys/user_A/EncKey.json")

# The same pattern applies to decryption
decrypted_data = cipher.decrypt(encrypted_data, sec_key_path="keys/user_A/SecKey.json")
```


---

# 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.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.
