PHP code example of ercsctt / laravel-file-encryption
1. Go to this page and download the library: Download ercsctt/laravel-file-encryption library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ercsctt / laravel-file-encryption example snippets
use Ercsctt\FileEncryption\Facades\FileEncrypter;
// Encrypt a file
FileEncrypter::encryptFile('/path/to/source.txt', '/path/to/encrypted.enc');
// Decrypt a file
FileEncrypter::decryptFile('/path/to/encrypted.enc', '/path/to/decrypted.txt');
// Auto-generate destination path (appends .enc for encryption)
FileEncrypter::encryptFile('/path/to/source.txt'); // Creates source.txt.enc
// Auto-generate destination path (removes .enc for decryption)
FileEncrypter::decryptFile('/path/to/source.txt.enc'); // Creates source.txt
// Get decrypted contents as string
$contents = FileEncrypter::decryptedContents('/path/to/encrypted.enc');
// Check if a file is encrypted
if (FileEncrypter::isEncrypted('/path/to/file.enc')) {
// File has encryption magic bytes
}
// Decrypt and get file contents
$contents = decrypt_file('/path/to/encrypted.enc');
use Ercsctt\FileEncryption\FileEncrypter;
// Key must be exactly 32 bytes for AES-256
$key = random_bytes(32);
$encrypter = new FileEncrypter($key);
// With custom chunk size (minimum 1024 bytes)
$encrypter = new FileEncrypter($key, 131072); // 128KB chunks
// Encrypt and decrypt
$encrypter->encryptFile('/path/to/source.txt', '/path/to/encrypted.enc');
$encrypter->decryptFile('/path/to/encrypted.enc', '/path/to/decrypted.txt');
use Ercsctt\FileEncryption\Facades\FileEncrypter;
// Stream decrypted content through a callback
FileEncrypter::decryptedStream('/path/to/large-file.enc', function ($chunk) {
echo $chunk; // Process each chunk
});
// Stream to a file handle
$output = fopen('/path/to/output.txt', 'wb');
FileEncrypter::decryptedStream('/path/to/encrypted.enc', function ($chunk) use ($output) {
fwrite($output, $chunk);
});
fclose($output);
// Stream for hashing
$context = hash_init('sha256');
FileEncrypter::decryptedStream('/path/to/file.enc', function ($chunk) use ($context) {
hash_update($context, $chunk);
});
$hash = hash_final($context);
use Ercsctt\FileEncryption\Facades\FileEncrypter;
// Progress callback receives current chunk and total chunks
FileEncrypter::encryptFile(
'/path/to/large-file.zip',
'/path/to/encrypted.enc',
function ($currentChunk, $totalChunks) {
$percent = round(($currentChunk / $totalChunks) * 100);
echo "Encrypting: {$percent}%\n";
}
);
FileEncrypter::decryptFile(
'/path/to/encrypted.enc',
'/path/to/decrypted.zip',
function ($currentChunk, $totalChunks) {
echo "Decrypting chunk {$currentChunk} of {$totalChunks}\n";
}
);
use Ercsctt\FileEncryption\FileEncrypter;
$currentKey = random_bytes(32);
$oldKey = '...'; // Your previous 32-byte key
$encrypter = (new FileEncrypter($currentKey))->previousKeys([$oldKey]);
// Decryption will try current key first, then fall back to old keys
$encrypter->decryptFile('/path/to/old-encrypted.enc', '/path/to/decrypted.txt');
// Get all configured keys
$allKeys = $encrypter->getAllKeys(); // Returns [currentKey, oldKey]
use Ercsctt\FileEncryption\Facades\FileEncrypter;
use Illuminate\Contracts\Encryption\EncryptException;
use Illuminate\Contracts\Encryption\DecryptException;
try {
FileEncrypter::encryptFile('/path/to/source.txt', '/path/to/encrypted.enc');
} catch (EncryptException $e) {
// Handle encryption failure (file not found, not readable, etc.)
}
try {
FileEncrypter::decryptFile('/path/to/encrypted.enc', '/path/to/decrypted.txt');
} catch (DecryptException $e) {
// Handle decryption failure (wrong key, corrupted file, invalid format, etc.)
}
use Ercsctt\FileEncryption\FileEncrypter;
// Keys must be exactly 32 bytes
try {
new FileEncrypter(str_repeat('a', 16)); // Too short!
} catch (RuntimeException $e) {
echo $e->getMessage(); // "File encryption