From e5f72648034bbeef613cf2dbdfe924b09292b66e Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Sun, 28 Feb 2021 11:07:51 +0200 Subject: [PATCH] * Fixed a bug in the library * Fixed the example.php to work with the new OOP style --- example.php | 8 ++++++-- lib.php | 11 +++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/example.php b/example.php index 6687122..c6e8a26 100644 --- a/example.php +++ b/example.php @@ -16,12 +16,16 @@ function limitStringSize($str, $amount = 20) { return $str; } +$kpc = new kpcrypt($config['key']); + +echo "Key: " . $kpc->getKey() . " \n"; + // Echo out the results -$encryptedData = encryptData($config['input'], $config['key'], 'AES-256-CBC', TRUE); +$encryptedData = $kpc->encryptData($config['input'], 'AES-256-CBC', TRUE); echo "Encrypted: " . limitStringSize($encryptedData); echo "\n"; -echo "Decrypted: " . limitStringSize(decryptData($encryptedData, $config['key'])); +echo "Decrypted: " . limitStringSize($kpc->decryptData($encryptedData)); echo "\n"; echo "Peak Memory: " . memory_get_peak_usage() / 1024 / 1024 . "Mb"; // Check the memory in kb echo "\n\n"; diff --git a/lib.php b/lib.php index e8fa27d..f7aa8c3 100644 --- a/lib.php +++ b/lib.php @@ -22,6 +22,8 @@ class kpcrypt { if ( empty($key) ) { $key = bin2hex(openssl_random_pseudo_bytes("64")); } + + $this->key = $key; } /** @@ -34,11 +36,10 @@ class kpcrypt { /** * @param string $data The data to be encrypted, this can only encrypt strings. - * @param string $key The key to use to encrypt the data, this key should be generated using the openssl_random_pseudo_bytes * @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 $key, string $cipherMethod = 'AES-256-CBC', bool $integrity_check = TRUE) { + function encryptData(string $data, string $cipherMethod = 'AES-256-CBC', bool $integrity_check = TRUE) { // Grab the key from self-reference $key = $this->key; @@ -51,6 +52,7 @@ class kpcrypt { return FALSE; } + // To encrypt a string $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipherMethod)); @@ -66,7 +68,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, $key, $cipherMethod)) === md5($data)) { + if (md5($this->decryptData($encryptedData, $cipherMethod)) === md5($data)) { // Put the output in the result return $encryptedData; } else { @@ -81,7 +83,6 @@ class kpcrypt { /** * @param string $data The data to be encrypted, this can only encrypt strings. - * @param string $key The key to use to encrypt the data, this key should be generated using the openssl_random_pseudo_bytes * @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') { @@ -91,6 +92,7 @@ class kpcrypt { // 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. @@ -110,6 +112,7 @@ class kpcrypt { // Decrypt the data $data = openssl_decrypt($data, $cipherMethod, $key, OPENSSL_RAW_DATA, $iv); + // Return the data return $data;