* 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
example.enc.php
example.dec.php

View File

@ -9,18 +9,42 @@ require("./lib.php");
* as low as possible.
*/
define("INPUT_FILE", "./example.php");
define("INPUT_FILE", "./test_input_file.txt");
define("OUTPUT_FILE", "./example.enc.php");
define("DEC_OUTPUT_FILE", "./example.dec.php");
// Initialize the class
$lib = new kpcrypt();
$enc_start = round(microtime(true) * 1000);
// Encrypt the file
$lib->encryptFile(INPUT_FILE, OUTPUT_FILE);
// Decrypt the file as well
$lib->encryptFile(OUTPUT_FILE, DEC_OUTPUT_FILE);
$enc_end = round(microtime(true) * 1000) - $enc_start;
echo "Errors: \n";
print_r($lib->getErrors())
$dec_start = round(microtime(true) * 1000);
// 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
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
*/
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 ( empty($key) ) {
$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;
}
@ -71,29 +84,17 @@ class kpcrypt {
/**
* @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.
*/
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
$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
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipherMethod));
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cipherMethod));
// 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
$encryptedData = $iv . $encryptedData;
@ -104,7 +105,7 @@ class kpcrypt {
// Check if we should verify the integrity of the encryption
if ($integrity_check === TRUE) {
// 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
return $encryptedData;
} else {
@ -120,28 +121,16 @@ class kpcrypt {
/**
* @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
$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.
$data = base64_decode($data);
// 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
$iv = substr($data, 0, $iv_length);
@ -149,7 +138,7 @@ class kpcrypt {
$data = substr($data, $iv_length);
// 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
@ -161,19 +150,10 @@ class kpcrypt {
// #region File Encryption
public function encryptFile(string $fileInput, string $fileOutput, string $cipherMethod = 'AES-256-CBC', 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;
}
public function encryptFile(string $fileInput, string $fileOutput, bool $integrity_check = TRUE) {
// 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
if ( $fout = fopen($fileOutput, 'w') ) {
@ -186,7 +166,7 @@ class kpcrypt {
while ( !feof($fin) ) {
// Read in blocks of 16
$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
$iv = substr($ciphertext, 0, 16);
fwrite($fout, $ciphertext);
@ -213,7 +193,7 @@ class kpcrypt {
return 1;
}
public function decryptFile(string $fileInput, string $fileOutput, string $cipherMethod = 'AES-256-CBC') {
public function decryptFile(string $fileInput, string $fileOutput) {
}