PHP Classes

PHP OpenSSL Encrypt and Decrypt File: Encrypt and decrypt file data using OpenSSL

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 300 All time: 7,413 This week: 571Up
Version License PHP version Categories
openssl-file-encrypt 0.05GNU General Publi...7.0PHP 5, Cryptography
Description 

Author

This class can encrypt and decrypt file data using OpenSSL.

It takes a given password and encrypts data read from a file using the OpenSSL extension.

The class can also decrypt previously encrypted files using the same password.

Picture of Martin Latter
  Performance   Level  
Name: Martin Latter <contact>
Classes: 8 packages by
Country: United Kingdom
Age: ???
All time rank: 130660 in United Kingdom
Week rank: 411 Up12 in United Kingdom Up
Innovation award
Innovation award
Nominee: 5x

Example

#!/usr/bin/env php
<?php

/**
    * Command-line example usage of OpenSSL-File-Encrypt class.
    *
    * Processes up to 1.8 GB on CLI, subject to memory availability.
    *
    * Usage:
    * php <thisfilename> -e|-d <filename>
    *
    * @author Martin Latter <copysense.co.uk>
    * @copyright Martin Latter 20/02/2018
    * @version 0.05
    * @license GNU GPL v3.0
    * @link https://github.com/Tinram/OpenSSL-File-Encrypt.git
*/


declare(strict_types=1);


define('DUB_EOL', PHP_EOL . PHP_EOL);
define('LINUX', (stripos(php_uname(), 'linux') !== FALSE) ? TRUE : FALSE);


if ( !
extension_loaded('openssl'))
{
    die(
PHP_EOL . ' OpenSSL library not available!' . DUB_EOL);
}


require(
'classes/openssl_file.class.php');


$sUsage =
   
PHP_EOL . ' ' . basename(__FILE__, '.php') .
   
DUB_EOL . "\tusage: php " . basename(__FILE__) . ' -e|-d <file> ' . ( ! LINUX ? '<password>' : '') . DUB_EOL;

$sMode = null;
$aOptions = getopt('h::e::d::', ['help::', 'h::']);

if ( ! empty(
$aOptions))
{
   
$sOpt = key($aOptions);

    switch (
$sOpt)
    {
        case
'h':
            die(
$sUsage);
        break;

        case
'e':
        case
'd':
          
$sMode = $sOpt;
        break;
    }
}
else
{
    die(
$sUsage);
}

if ( ! isset(
$_SERVER['argv'][2]))
{
    echo
PHP_EOL . ' missing filename!' . PHP_EOL;
    die(
$sUsage);
}

$sFilename = $_SERVER['argv'][2];

if ( !
file_exists($sFilename))
{
    die(
PHP_EOL . ' \'' . $sFilename . '\' does not exist in this directory!' . DUB_EOL);
}


if (
LINUX)
{
    echo
' password: ';
    `
/bin/stty -echo`;
   
$sPassword = trim(fgets(STDIN));
    `
/bin/stty echo`;

    if (
$sMode === 'e')
    {
        echo
PHP_EOL . ' re-enter password: ';
        `
/bin/stty -echo`;
       
$sPassword2 = trim(fgets(STDIN));
        `
/bin/stty echo`;

        if (
$sPassword !== $sPassword2)
        {
            die(
PHP_EOL . ' entered passwords do not match!' . DUB_EOL);
        }
    }
}
else
{
    if ( ! isset(
$_SERVER['argv'][3]))
    {
        die(
PHP_EOL . ' missing password!' . DUB_EOL . "\tusage: " . basename(__FILE__) . ' -e|-d <file> <password>' . DUB_EOL);
    }
    else
    {
       
$sPassword = $_SERVER['argv'][3];
    }
}


if (
$sMode === 'e')
{
    echo
OpenSSLFile::encrypt($sFilename, $sPassword);
}
else if (
$sMode === 'd')
{
    echo
OpenSSLFile::decrypt($sFilename, $sPassword);
}


Details

OpenSSL File Encrypt

Simple symmetric file encryption.

Purpose

Provide strong file encryption using OpenSSL, via an easy-to-use PHP wrapper.

One use is to avoid direct OpenSSL interaction, such as:

    openssl enc -e -aes-256-cbc -in abc.txt -out abc.enc -k password -S deadbeef

and offer something simpler:

    php cmdline_example.php -e abc.txt

Example

Encrypt

    php cmdline_example.php -e abc.txt

results in the encrypted file abc.txt.osl

Decrypt

    php cmdline_example.php -d abc.txt.osl

results in abc.txt, with the correct password

-- and overwrites the original file abc.txt if it is present in the same directory.

Max File Size

The maximum file size that can be processed is approximately 1.8GB.

This limit is seemingly dictated by the PHP openssl module (OpenSSL itself on the command-line will process 2GB+ files).

Low Memory Systems

A file-chunking version for limited memory availability works with the non-counter (CTR) mode ciphers.

Appending the HMAC to the final file and decrypting successfully is not yet ready.

License

OpenSSL File Encrypt is released under the GPL v.3.


  Files folder image Files  
File Role Description
Files folder imageclasses (1 file)
Files folder imagefile_examples (1 file)
Accessible without login Plain text file cmdline_example.php Example Example 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  /  classes  
File Role Description
  Plain text file openssl_file.class.php Class Class source

  Files folder image Files  /  file_examples  
File Role Description
  Accessible without login Plain text file test_text.txt Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:300
This week:0
All time:7,413
This week:571Up