Current Location: Home> Latest Articles> List all available encryption filters using stream_get_filters

List all available encryption filters using stream_get_filters

gitbox 2025-05-20

In PHP, the stream_get_filters() function is a very useful tool that is able to list all available stream filters. Stream filters allow you to process data in or out, such as encryption, compression, character encoding conversion, etc.

If you want to see all the encryption filters available, the stream_get_filters() function provides an easy way to implement it. This article will provide a detailed introduction to how to use stream_get_filters() to list all available encryption filters on the system and give a brief introduction to some of them.

What is a flow filter?

Stream filters are a feature provided by PHP to process data when opening a file stream or other type of stream. For example, when you read a file or get data from a network, you can use a stream filter to encrypt, compress or otherwise process the data. Common encrypted flow filters include mcrypt.* and openssl.* related filters.

List all filters using stream_get_filters()

To list all available filters, you just need to use the stream_get_filters() function. This returns an array containing all available filter names. You can implement it through the following code:

 <?php
// Get all available filters
$filters = stream_get_filters();

// Output filter list
echo "All available stream filters:\n";
foreach ($filters as $filter) {
    echo $filter . "\n";
}
?>

Output result

After executing the above code, stream_get_filters() returns an array containing all available filter names. If your PHP environment supports encryption filters, you will see some output similar to the following:

 all
convert.iconv.*
mcrypt.*
openssl.*
zlib.*

Encryption-related stream filters

In PHP, the most common encryption-related stream filters are provided via mcrypt or openssl . These filters can help you implement encryption and decryption operations in your data stream. For example:

  • mcrypt.* : This is an encrypted stream filter based on the mcrypt library. Common filters include mcrypt.rijndael-128 and mcrypt.des .

  • openssl.* : This is an encrypted stream filter based on the OpenSSL library. Common filters include openssl.cipher and openssl.decrypt .

Through these filters, you can encrypt and decrypt the data to ensure the security of the data. For example:

 <?php
// Example:use OpenSSL Stream filter for data encryption
$data = "Hello, world!";
$encryptedData = stream_filter_append($data, "openssl.cipher", STREAM_FILTER_WRITE, [
    'cipher' => 'aes-256-cbc',
    'key' => 'secretkey'
]);

echo "Encrypted data: " . $encryptedData;
?>

Use stream_get_filters() with URL operations

If you want to transfer data to a URL address through an encrypted filter, you can directly operate on the filter returned by stream_get_filters() . For example, suppose you want to encrypt the data and send it to a URL, you can use the following code:

 <?php
$url = "https://gitbox.net/api/secure/upload";

// Get all available filters
$filters = stream_get_filters();

// Check and apply encryption filters
if (in_array('openssl.cipher', $filters)) {
    $data = "Sensitive data that needs encryption";

    // Encrypt and send data
    $encryptedData = stream_filter_append($data, 'openssl.cipher', STREAM_FILTER_WRITE, [
        'cipher' => 'aes-256-cbc',
        'key' => 'secretkey'
    ]);

    // 将Encrypted data通过 URL send
    file_put_contents($url, $encryptedData);
}
?>

In this example, we first list all available filters and confirm whether there is an openssl.cipher filter available. If available, we encrypt the data and send the encrypted data to https://gitbox.net/api/secure/upload .

Summarize

Through the stream_get_filters() function, you can conveniently list all PHP supported stream filters, including various encryption, compression, encoding and conversion functions. If you need to use encrypted stream filters, such as mcrypt or openssl , just apply it to the data stream through the stream_filter_append() function.

Stream filters in PHP are very powerful, not only helping you encrypt or decrypt data while doing file operations, but also ensures data security when interacting with remote servers.