* Moved $cipherMethod to OOP

+ Working file Encryption
This commit is contained in:
Daniel Legt 2021-02-28 13:55:29 +02:00
parent a5923c43e4
commit 752dfd0c23
3 changed files with 55 additions and 50 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
test_input_file.txt
config.inc.php config.inc.php
example.enc.php example.enc.php
example.dec.php example.dec.php

View File

@ -9,18 +9,42 @@ require("./lib.php");
* as low as possible. * as low as possible.
*/ */
define("INPUT_FILE", "./example.php"); define("INPUT_FILE", "./test_input_file.txt");
define("OUTPUT_FILE", "./example.enc.php"); define("OUTPUT_FILE", "./example.enc.php");
define("DEC_OUTPUT_FILE", "./example.dec.php"); define("DEC_OUTPUT_FILE", "./example.dec.php");
// Initialize the class // Initialize the class
$lib = new kpcrypt(); $lib = new kpcrypt();
$enc_start = round(microtime(true) * 1000);
// Encrypt the file // Encrypt the file
$lib->encryptFile(INPUT_FILE, OUTPUT_FILE); $lib->encryptFile(INPUT_FILE, OUTPUT_FILE);
// Decrypt the file as well $enc_end = round(microtime(true) * 1000) - $enc_start;
$lib->encryptFile(OUTPUT_FILE, DEC_OUTPUT_FILE);
echo "Errors: \n"; $dec_start = round(microtime(true) * 1000);
print_r($lib->getErrors())
// Decrypt the file as well
$lib->decryptFile(OUTPUT_FILE, DEC_OUTPUT_FILE);
$dec_end = round(microtime(true) * 1000) - $dec_start;
/**
* NOTE: You can also compress the output using gzip
*/
echo "\n\n";
echo "Input File Size: " . filesize(INPUT_FILE) / 1024 / 1024 . "Mb";
echo "\n";
echo "Output File Size: " . filesize(OUTPUT_FILE) / 1024 / 1024 . "Mb";
echo "\n";
echo "Peak Memory: " . memory_get_peak_usage() / 1024 / 1024 . "Mb"; // Check the memory in kb
echo "\n";
echo "Encryption Time: $enc_end ms\n"; // 9700k = ~210ms
echo "\n";
echo "Decryption Time: $dec_end ms\n";
echo "\n\n";
$errors = $lib->getErrors();
echo "Errors: " . ( !empty($errors) ? "\n" . json_encode($errors, JSON_PRETTY_PRINT) : "No Errors!");

70
lib.php
View File

@ -25,14 +25,27 @@ class kpcrypt {
// A log of all of the errors, rather nice for debugging // A log of all of the errors, rather nice for debugging
private $errorLog = []; private $errorLog = [];
// The encryption cipher to use
private $cipherMethod = "AES-256-CBC";
/** /**
* @param string $key [Optional] The key to use for encryption, if none is mentioned a random one will be generated * @param string $key [Optional] The key to use for encryption, if none is mentioned a random one will be generated
*/ */
public function __construct( string $key = null) { public function __construct( string $key = null, string $cipherMethod = "AES-256-CBC" ) {
// If no key is mentioned, generate one // If no key is mentioned, generate one
if ( empty($key) ) { if ( empty($key) ) {
$key = bin2hex(openssl_random_pseudo_bytes("64")); $key = bin2hex(openssl_random_pseudo_bytes("64"));
} }
// Lowercase the cipher
$this->cipherMethod = strtolower($cipherMethod);
// Check if the encryption method is valid
if (!in_array($this->cipherMethod, openssl_get_cipher_methods())) {
// Cypher was not in the available ciphers list.
$this->errorLog[] = "[" . __LINE__ . "]" . $this->cipherMethod . " isn't a valid cipherMethod.";
return FALSE;
}
$this->key = $key; $this->key = $key;
} }
@ -71,29 +84,17 @@ class kpcrypt {
/** /**
* @param string $data The data to be encrypted, this can only encrypt strings. * @param string $data The data to be encrypted, this can only encrypt strings.
* @param string $cipherMethod The cypher method to use in the encryption process, these can be checked using
* @param bool $integrity_check Check if the encrypted data can also be decypted, this will take 2x more time to process the data, but will make 100% sure that the data is safe and can be decrypted. * @param bool $integrity_check Check if the encrypted data can also be decypted, this will take 2x more time to process the data, but will make 100% sure that the data is safe and can be decrypted.
*/ */
function encryptData(string $data, string $cipherMethod = 'AES-256-CBC', bool $integrity_check = TRUE) { function encryptData(string $data, bool $integrity_check = TRUE) {
// Grab the key from self-reference // Grab the key from self-reference
$key = $this->key; $key = $this->key;
// Lowercase the cipher
$cipherMethod = strtolower($cipherMethod);
// Check if the encryption method is valid
if (!in_array($cipherMethod, openssl_get_cipher_methods())) {
// Cypher was not in the available ciphers list.
$this->errorLog[] = "[" . __LINE__ . "]" . $cipherMethod . " isn't a valid cipherMethod.";
return FALSE;
}
// To encrypt a string // To encrypt a string
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipherMethod)); $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cipherMethod));
// Encrypt the data // Encrypt the data
$encryptedData = openssl_encrypt($data, $cipherMethod, $key, OPENSSL_RAW_DATA, $iv); $encryptedData = openssl_encrypt($data, $this->cipherMethod, $key, OPENSSL_RAW_DATA, $iv);
// Append the iv to the encrypted data // Append the iv to the encrypted data
$encryptedData = $iv . $encryptedData; $encryptedData = $iv . $encryptedData;
@ -104,7 +105,7 @@ class kpcrypt {
// Check if we should verify the integrity of the encryption // Check if we should verify the integrity of the encryption
if ($integrity_check === TRUE) { if ($integrity_check === TRUE) {
// Test for decryption validity // Test for decryption validity
if (md5($this->decryptData($encryptedData, $cipherMethod)) === md5($data)) { if (md5($this->decryptData($encryptedData)) === md5($data)) {
// Put the output in the result // Put the output in the result
return $encryptedData; return $encryptedData;
} else { } else {
@ -120,28 +121,16 @@ class kpcrypt {
/** /**
* @param string $data The data to be encrypted, this can only encrypt strings. * @param string $data The data to be encrypted, this can only encrypt strings.
* @param string $cipherMethod The cypher method to use in the encryption process, these can be checked using
*/ */
function decryptData(string $data, string $cipherMethod = 'AES-256-CBC') { function decryptData(string $data) {
// Grab the key from self-reference // Grab the key from self-reference
$key = $this->key; $key = $this->key;
// Lowercase the cipher
$cipherMethod = strtolower($cipherMethod);
// Check if the encryption method is valid
if (!in_array($cipherMethod, openssl_get_cipher_methods())) {
// Cypher was not in the available ciphers list.
$this->errorLog[] = "[" . __LINE__ . "]" . $cipherMethod . " isn't a valid cipherMethod.";
return FALSE;
}
// base64 decode the data. // base64 decode the data.
$data = base64_decode($data); $data = base64_decode($data);
// Get the length of the IV // Get the length of the IV
$iv_length = openssl_cipher_iv_length($cipherMethod); $iv_length = openssl_cipher_iv_length($this->cipherMethod);
// Get the IV from the decoded data // Get the IV from the decoded data
$iv = substr($data, 0, $iv_length); $iv = substr($data, 0, $iv_length);
@ -149,7 +138,7 @@ class kpcrypt {
$data = substr($data, $iv_length); $data = substr($data, $iv_length);
// Decrypt the data // Decrypt the data
$data = openssl_decrypt($data, $cipherMethod, $key, OPENSSL_RAW_DATA, $iv); $data = openssl_decrypt($data, $this->cipherMethod, $key, OPENSSL_RAW_DATA, $iv);
// Return the data // Return the data
@ -161,19 +150,10 @@ class kpcrypt {
// #region File Encryption // #region File Encryption
public function encryptFile(string $fileInput, string $fileOutput, string $cipherMethod = 'AES-256-CBC', bool $integrity_check = TRUE) { public function encryptFile(string $fileInput, string $fileOutput, bool $integrity_check = TRUE) {
// Lowercase the cipher
$cipherMethod = strtolower($cipherMethod);
// Check if the encryption method is valid
if (!in_array($cipherMethod, openssl_get_cipher_methods())) {
// Cypher was not in the available ciphers list.
$this->errorLog[] = "[" . __LINE__ . "]" . $cipherMethod . " isn't a valid cipherMethod.";
return FALSE;
}
// To encrypt a string // To encrypt a string
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipherMethod)); $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cipherMethod));
// Try and open the destionation // Try and open the destionation
if ( $fout = fopen($fileOutput, 'w') ) { if ( $fout = fopen($fileOutput, 'w') ) {
@ -186,7 +166,7 @@ class kpcrypt {
while ( !feof($fin) ) { while ( !feof($fin) ) {
// Read in blocks of 16 // Read in blocks of 16
$plaintext = fread($fin, 16 * $this->blocks); $plaintext = fread($fin, 16 * $this->blocks);
$ciphertext = openssl_encrypt($plaintext, $cipherMethod, $this->key, OPENSSL_RAW_DATA, $iv); $ciphertext = openssl_encrypt($plaintext, $this->cipherMethod, $this->key, OPENSSL_RAW_DATA, $iv);
// Use the first 16 bytes of the ciphertext as the next initialization vector // Use the first 16 bytes of the ciphertext as the next initialization vector
$iv = substr($ciphertext, 0, 16); $iv = substr($ciphertext, 0, 16);
fwrite($fout, $ciphertext); fwrite($fout, $ciphertext);
@ -213,7 +193,7 @@ class kpcrypt {
return 1; return 1;
} }
public function decryptFile(string $fileInput, string $fileOutput, string $cipherMethod = 'AES-256-CBC') { public function decryptFile(string $fileInput, string $fileOutput) {
} }