Oihana PHP

OpenSSLFileEncryption

Class OpenSSLFileEncryption

This class provides functionality to encrypt and decrypt files using OpenSSL. It prepends the IV (Initialization Vector) to the encrypted data so that decryption is self-contained.

Tags
example
use oihana\files\openssl\OpenSSLFileEncryption;

$crypto = new OpenSSLFileEncryption('my-secret-passphrase');
$encryptedPath = $crypto->encrypt('/path/to/file.txt');
$decryptedPath = $crypto->decrypt($encryptedPath);
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Properties

$ivLength  : int
$cipher  : string
$passphrase  : string

Methods

__construct()  : mixed
Constructor.
__destruct()  : mixed
Destructor: clears the passphrase from memory.
decrypt()  : string
Decrypts a previously encrypted file.
encrypt()  : string
Encrypts a file using OpenSSL.
hasEncryptedFileSize()  : bool
Checks if a file is large enough to be encrypted.
isEncryptedFile()  : bool
Heuristically checks whether a file appears to be encrypted.

Properties

$ivLength

public int $ivLength

The length of the initialization vector (IV) used for encryption and decryption.

Hooks
public int get

Methods

__construct()

Constructor.

public __construct(string $passphrase[, string $cipher = 'aes-256-cbc' ]) : mixed
Parameters
$passphrase : string

Secret key for encryption/decryption.

$cipher : string = 'aes-256-cbc'

OpenSSL cipher algorithm. Default is 'aes-256-cbc'.

Tags
throws
InvalidArgumentException

If the passphrase is empty or the cipher is unsupported.

example
$crypto = new OpenSSLFileEncryption('my-passphrase', 'aes-256-cbc');

decrypt()

Decrypts a previously encrypted file.

public decrypt(string $inputFile[, string|null $outputFile = null ]) : string

Extracts the IV from the start of the file, decrypts the remaining data, and writes the decrypted content to the output file.

Parameters
$inputFile : string

Path to the encrypted file.

$outputFile : string|null = null

Optional output path. If null, .enc is stripped.

Tags
throws
RuntimeException

On read/write/decryption failure.

throws
FileException

If the input file is not valid.

example
$crypto = new OpenSSLFileEncryption('secret');
$decryptedPath = $crypto->decrypt('/path/to/file.txt.enc');
Return values
string

Path to the decrypted file.

encrypt()

Encrypts a file using OpenSSL.

public encrypt(string $inputFile[, string|null $outputFile = null ]) : string

Reads the input file, generates a secure IV, encrypts the content using OpenSSL, prepends the IV to the encrypted data, and writes it to the output file.

Parameters
$inputFile : string

Path to the file to encrypt.

$outputFile : string|null = null

Optional output file path. If null, appends .enc.

Tags
throws
RuntimeException

On read/write/encryption failure.

throws
FileException

If input file is not valid.

throws
DirectoryException

If output directory is not writable.

example
$crypto = new OpenSSLFileEncryption('secret');
$encryptedPath = $crypto->encrypt('/path/to/plain.txt');
Return values
string

Path to the encrypted file.

hasEncryptedFileSize()

Checks if a file is large enough to be encrypted.

public hasEncryptedFileSize(string $filePath) : bool

Verifies that the file has at least enough bytes to contain an IV. This does not confirm it was encrypted or can be decrypted.

Parameters
$filePath : string

Path to the file to check.

Tags
example
if ( $crypto->hasEncryptedFileSize('/file') )
{
    echo "Looks like an encrypted file (size).";
}
Return values
bool

True if the file is at least as long as the IV size.

isEncryptedFile()

Heuristically checks whether a file appears to be encrypted.

public isEncryptedFile(string $filePath) : bool

Validates that the file has:

  • at least IV length
  • an IV not composed only of null bytes
  • an IV not dominated by printable characters (indicating possible plaintext)

This method gives a best-effort verification that a file was encrypted by this class.

Parameters
$filePath : string

Path to the file to check.

Tags
example
if ($crypto->isEncryptedFile('/file'))
{
    echo "Likely encrypted.";
}
Return values
bool

True if the file likely contains encrypted content.


        
On this page

Search results