> For the complete documentation index, see [llms.txt](https://quantumphp.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quantumphp.gitbook.io/docs/packages/encryption/helpers.md).

# Helpers

The package exposes two global helpers.

## `crypto_encode()`

```php
function crypto_encode($data, string $type = CryptorType::SYMMETRIC): string
```

Behavior:

1. serializes the input with PHP `serialize(...)`
2. resolves a cryptor through `CryptorFactory::get($type)`
3. calls `encrypt(...)` on the resolved cryptor

### Implications

* scalar values, arrays, and objects all go through serialization first
* the output format is helper-level ciphertext, not the raw representation of the original value
* the default cryptor type is symmetric

## `crypto_decode()`

```php
function crypto_decode(string $encryptedData, string $type = CryptorType::SYMMETRIC)
```

Behavior:

1. resolves a cryptor through `CryptorFactory::get($type)`
2. decrypts the string
3. runs `@unserialize(...)` on the decrypted result
4. returns the unserialized value when that result is not `false`
5. otherwise returns the decrypted string

## Type and lifecycle caveats

### Asymmetric helper calls depend on shared instance reuse

Because the asymmetric adapter generates a key pair at construction time, this works only while the same factory-managed adapter instance is reused:

```php
$encrypted = crypto_encode('secret', CryptorType::ASYMMETRIC);
$value = crypto_decode($encrypted, CryptorType::ASYMMETRIC);
```

Within one live DI container, `CryptorFactory` memoization preserves that key pair.

Across a container reset, process restart, or any new asymmetric adapter instance, the same ciphertext is no longer expected to decrypt correctly.

### `false` does not round-trip cleanly

As noted in the package contract, helper decoding cannot distinguish:

* "unserialize failed"
* "unserialize succeeded and the actual value is boolean false"

So helper consumers should not rely on exact `false` round-tripping.

## Where helpers are used in core

Current framework code uses these helpers in at least two places:

* Cookie storage encrypts values before writing and decrypts on read
* Session storage encrypts values before storing and decrypts on read

That makes symmetric mode part of the framework's default data-at-rest behavior for those packages.


---

# 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://quantumphp.gitbook.io/docs/packages/encryption/helpers.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.
