* Fixed a bug in the library

* Fixed the example.php to work with the new OOP style
This commit is contained in:
Daniel Legt 2021-02-28 11:07:51 +02:00
parent 5ae3d8294b
commit e5f7264803
2 changed files with 13 additions and 6 deletions

View File

@ -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";

11
lib.php
View File

@ -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;