PHP Classes

Learn How Can PHP Encrypt Large Files on Server Side with a Script of Example Code from the Package PHP Cypher File: Symmetric encryption of large files using RSA keys

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-06-24 (3 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
phpcypherfile 1.0MIT/X Consortium ...5PHP 5, Files and Folders, Cryptography
Description 

Author

This package provides symmetric encryption of large files using RSA keys.

It provides a class that takes the path of one file and generates a random encryption key.

The class can create an output file to store the encryption key, reads the input file in chunks of a small size, encrypts the file data using the AES-256-CBC cypher, and then uses a given RSA key to encrypt the resulting data with a public key before storing the data in a file.

It can also do the opposite by reading a previously encrypted file, decrypting the file data using a private RSA key, reading the encryption key from the file, reading the file chunks, decrypting the file chunks using an AES-256-CBC cipher, and storing the decrypted chunks in the output file.

Picture of Bigb06
Name: Bigb06 <contact>
Classes: 1 package by
Country: France France
Innovation award
Innovation award
Nominee: 1x

Instructions

Installation

To install this library, you can use Composer. Run the following command:

composer require bigb06/phpcypherfile

Example

Basic code to use PHPCypherFile (see example.php for full example and keys generation):

se PHPCypherFile\PHPCypherFile;

// Encrypt the file
PHPCypherFile::encryptFile($inputFile, $encryptedFile,$publicKey);

// Decrypt the file
PHPCypherFile::decryptFile($encryptedFile, $decryptedFile,$privateKey);
Keys Generation
$config = [
    "private_key_bits" => 4096,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
];
$res = openssl_pkey_new($config);
openssl_pkey_export($res, $privateKeyString);
$details = openssl_pkey_get_details($res);
$publicKeyString = $details["key"];
$publicKey = openssl_pkey_get_public($publicKeyString);
$privateKey = openssl_pkey_get_private($privateKeyString);

You can also refer to example.php for detailed steps on how to generate the required RSA public/private keys for encryption and decryption.

Documentation

PHPCypherFile

PHPCypherFile provides a robust solution for encrypting large files securely without significant memory overhead. It combines the power of RSA for public/private key encryption and AES-256-CBC for symmetric encryption, ensuring both performance and security.

Features

  1. Large File Support:

    Files are encrypted in chunks, minimizing memory usage during the encryption/decryption process. This ensures scalability for handling large files.

  2. Hybrid Encryption Scheme:

    - random symmetric key is generated for AES-256-CBC encryption of the file content. - The symmetric key and IV (Initialization Vector) are encrypted with a public RSA key.

  3. Output Structure: The encrypted file includes the following components: - RSA-encrypted symmetric key. - RSA-encrypted IV. - AES-encrypted file data.
  4. Security Standards: - AES-256-CBC ensures high-speed, secure encryption for the data. - RSA encryption secures the transmission of the symmetric key, leveraging a public/private key pair.
  5. Minimal Memory Footprint:

    By processing files in small chunks, the class avoids loading the entire file into memory.

Installation

To install this library, you can use Composer. Run the following command:

composer require bigb06/phpcypherfile

Example

Basic code to use PHPCypherFile (see example.php for full example and keys generation):


use PHPCypherFile\PHPCypherFile;

// Encrypt the file
PHPCypherFile::encryptFile($inputFile, $encryptedFile,$publicKey);

// Decrypt the file
PHPCypherFile::decryptFile($encryptedFile, $decryptedFile,$privateKey);

Keys Generation

$config = [
    "private_key_bits" => 4096,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
];
$res = openssl_pkey_new($config);
openssl_pkey_export($res, $privateKeyString);
$details = openssl_pkey_get_details($res);
$publicKeyString = $details["key"];
$publicKey = openssl_pkey_get_public($publicKeyString);
$privateKey = openssl_pkey_get_private($privateKeyString);

You can also refer to example.php for detailed steps on how to generate the required RSA public/private keys for encryption and decryption.

? Author

? Acknowledgements

License

This project is open source and available under the MIT License.


  Files folder image Files (5)  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file example.php Aux. Configuration script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (5)  /  src  
File Role Description
  Plain text file PHPCypherFile.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0