Last active
April 24, 2020 00:47
-
-
Save antic183/c44160caa7acb5e036064b9062d17ec6 to your computer and use it in GitHub Desktop.
encrypt and decrypt files and folders with php (AES-256) trought command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
> composer install | |
# encript: | |
php _encript-decript-from-console.php enc "password" file.pdf | |
=> result = timestamp-file.pdf.enc | |
php _encript-decript-from-console.php enc "password" image.jpg | |
=> result = timestamp-mage.jpg.enc | |
php _encript-decript-from-console.php enc "password" folderA | |
=> result = timestamp-folderA.zip.enc | |
php _encript-decript-from-console.php enc "password" C:\\Users\username\folderB | |
=> result = timestamp-folderB.zip.enc | |
# decript: | |
php _encript-decript-from-console.php enc "password" timestamp-file.enc | |
=> file.enc.dec | |
php _encript-decript-from-console.php enc "password" timestamp-image.enc | |
=> image.enc.dec | |
php _encript-decript-from-console.php enc "password" C:\\Users\username\folderC.enc | |
=> timestamp.folderC.enc.dec-folder | |
php _encript-decript-from-console.php enc "password" folderC.zip.enc | |
=> timestamp.folderC.zip.enc.dec-zip-folder | |
*/ | |
require_once 'vendor/autoload.php'; | |
use \GibberishAES\GibberishAES; | |
ini_set('memory_limit','5000M'); | |
error_reporting(E_ALL); | |
ini_set("display_errors", 1); | |
set_error_handler(function(int $number, string $message) { | |
echo "---------------------------------------"; | |
echo "Handler captured error $number: '$message'" . PHP_EOL; | |
echo "---------------------------------------"; | |
exit; | |
}, E_ALL); | |
$mode = $argv[1]; // enc = encrypt, dec = decrypt | |
$pass = $argv[2]; // password | |
$userInput = $argv[3]; // input file or folder | |
GibberishAES::size(256); | |
if (!realpath($userInput)) { | |
exit('path "' . $userInput . '" doesn\'t exist!'); | |
} | |
$timeStamp = (new DateTime())->getTimestamp(); | |
if (is_dir($userInput)) { | |
// dir | |
if ($mode === 'enc') { | |
$zipFileName = $timeStamp . '-tmp.zip'; | |
$phar = new PharData($zipFileName); | |
$folder = realpath($userInput); | |
$phar->buildFromDirectory($folder); | |
$input = file_get_contents($zipFileName); | |
$encryptedContent = GibberishAES::enc($input, $pass); | |
unlink($zipFileName); | |
$fileOutput = $timeStamp . "-" . basename($userInput) . ".zip.enc"; | |
file_put_contents($fileOutput, $encryptedContent); | |
echo "encripted! See " . $fileOutput; | |
} else if ($mode === 'dec') { | |
$input = file_get_contents($userInput); | |
$decryptedContent = GibberishAES::dec($input, $pass); | |
$fileOutput = $timeStamp . "-" . basename($userInput) . (preg_match('/.zip/i', $userInput, $matches) > 0? 'dec-zip-folder': '.dec-folder' ); | |
file_put_contents($fileOutput, $decryptedContent); | |
echo "decripted! See " . $fileOutput; | |
} | |
} else if (is_file($userInput)) { | |
// file | |
if ($mode === 'enc') { | |
$input = file_get_contents($userInput); | |
$encryptedContent = GibberishAES::enc($input, $pass); | |
$fileOutput = $timeStamp . "-" . $userInput . ".enc"; | |
file_put_contents($fileOutput, $encryptedContent); | |
echo "encripted! See " . $fileOutput; | |
} else if ($mode === 'dec') { | |
$input = file_get_contents($userInput); | |
$decryptedContent = GibberishAES::dec($input, $pass); | |
$fileOutput = $timeStamp . "-" . $userInput . ".dec"; | |
file_put_contents($fileOutput, $decryptedContent); | |
echo "decripted! See " . $fileOutput; | |
} | |
} else { | |
echo 'Error: ' . $userInput . ' is not a file and not a folder!'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"dittertp/gibberish-aes-php" : "1.1.*" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment