* Moved $cipherMethod to OOP
+ Working file Encryption
This commit is contained in:
70
lib.php
70
lib.php
@@ -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) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user