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
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
$cipher
private
string
$cipher
The cipher method used for encryption and decryption.
$passphrase
private
string
$passphrase
The passphrase used for encryption and decryption.
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
__destruct()
Destructor: clears the passphrase from memory.
public
__destruct() : mixed
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
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
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
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
Return values
bool —True if the file likely contains encrypted content.